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 all 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
19 changes: 16 additions & 3 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,16 @@ def time_convert(self, data):
lib.maybe_convert_numeric(data, set(), coerce_numeric=False)


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)


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

7 changes: 5 additions & 2 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,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 @@ -85,6 +84,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 @@ -216,7 +220,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