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
Show file tree
Hide file tree
Changes from 13 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
18 changes: 14 additions & 4 deletions asv_bench/benchmarks/inference.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import datetime

import numpy as np

from pandas import DataFrame, Series, to_numeric
from pandas import DataFrame, Series, Timestamp, to_numeric
from pandas.core.common import maybe_box_datetimelike
import pandas.util.testing as tm

from .pandas_vb_common import lib, numeric_dtypes
Expand Down Expand Up @@ -52,7 +55,6 @@ def time_add_timedeltas(self, df):


class ToNumeric:

params = ["ignore", "coerce"]
param_names = ["errors"]

Expand All @@ -73,7 +75,6 @@ def time_from_str(self, errors):


class ToNumericDowncast:

param_names = ["dtype", "downcast"]
params = [
[
Expand Down Expand Up @@ -121,4 +122,13 @@ def time_convert(self, data):
lib.maybe_convert_numeric(data, set(), coerce_numeric=False)


from .pandas_vb_common import setup # noqa: F401 isort:skip
Copy link
Contributor

Choose a reason for hiding this comment

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

you need to leave this

class MaybeBoxDatetimelike:
def setup(self):
self.pd_timestamp = Timestamp.now()
self.py_timestamp = datetime.datetime.now()

def pd_datetime_box(self):
maybe_box_datetimelike(self.pd_timestamp)

def py_datetime_box(self):
maybe_box_datetimelike(self.py_timestamp)
19 changes: 11 additions & 8 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,6 +83,11 @@ 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, (tslibs.Timedelta, tslibs.Timestamp)) or (
value is tslibs.NaT
):
return value

if isinstance(value, (np.datetime64, datetime)):
value = tslibs.Timestamp(value)
elif isinstance(value, (np.timedelta64, timedelta)):
Expand Down Expand Up @@ -119,7 +123,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 +219,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 +287,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 +307,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