Skip to content

Performance of maybe_box_datetimelike #30520 #30531

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 19 commits into from
Closed
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ def consensus_name_attr(objs):


def maybe_box(indexer, values, obj, key):

# if we have multiples coming back, box em
if isinstance(values, np.ndarray):
return obj[indexer.get_loc(key)]
Expand All @@ -84,9 +83,13 @@ def maybe_box(indexer, values, obj, key):
def maybe_box_datetimelike(value):
# turn a datetime like into a Timestamp/timedelta as needed

if isinstance(value, (np.datetime64, datetime)):
if isinstance(value, (np.datetime64, datetime)) and (
not isinstance(value, tslibs.Timestamp)) and (
notnull(value)):
value = tslibs.Timestamp(value)
elif isinstance(value, (np.timedelta64, timedelta)):
elif isinstance(value, (np.timedelta64, timedelta)) and (
not isinstance(value, tslibs.Timedelta)) and (
notnull(value)):
value = tslibs.Timedelta(value)

return value
Expand Down Expand Up @@ -119,7 +122,7 @@ def is_bool_indexer(key: Any) -> bool:
"""
na_msg = "cannot index with vector containing NA / NaN values"
if isinstance(key, (ABCSeries, np.ndarray, ABCIndex)) or (
is_array_like(key) and is_extension_array_dtype(key.dtype)
is_array_like(key) and is_extension_array_dtype(key.dtype)
):
if key.dtype == np.object_:
key = np.asarray(values_from_object(key))
Expand Down Expand Up @@ -215,7 +218,6 @@ def try_sort(iterable):


def asarray_tuplesafe(values, dtype=None):

if not (isinstance(values, (list, tuple)) or hasattr(values, "__array__")):
values = list(values)
elif isinstance(values, ABCIndexClass):
Expand Down Expand Up @@ -284,10 +286,10 @@ def is_null_slice(obj):
We have a null slice.
"""
return (
isinstance(obj, slice)
and obj.start is None
and obj.stop is None
and obj.step is None
isinstance(obj, slice)
and obj.start is None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you avoid other whitespace changes

and obj.stop is None
and obj.step is None
)


Expand All @@ -304,7 +306,7 @@ def is_full_slice(obj, l):
We have a full length slice.
"""
return (
isinstance(obj, slice) and obj.start == 0 and obj.stop == l and obj.step is None
isinstance(obj, slice) and obj.start == 0 and obj.stop == l and obj.step is None
)


Expand Down