Skip to content

PERF: to_datetime with uint #43268

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

Merged
merged 2 commits into from
Aug 30, 2021
Merged
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
10 changes: 9 additions & 1 deletion asv_bench/benchmarks/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,19 +115,27 @@ def time_maybe_convert_objects(self):
class ToDatetimeFromIntsFloats:
def setup(self):
self.ts_sec = Series(range(1521080307, 1521685107), dtype="int64")
self.ts_sec_uint = Series(range(1521080307, 1521685107), dtype="uint64")
self.ts_sec_float = self.ts_sec.astype("float64")

self.ts_nanosec = 1_000_000 * self.ts_sec
self.ts_nanosec_uint = 1_000_000 * self.ts_sec_uint
self.ts_nanosec_float = self.ts_nanosec.astype("float64")

# speed of int64 and float64 paths should be comparable
# speed of int64, uint64 and float64 paths should be comparable

def time_nanosec_int64(self):
to_datetime(self.ts_nanosec, unit="ns")

def time_nanosec_uint64(self):
to_datetime(self.ts_nanosec_uint, unit="ns")

def time_nanosec_float64(self):
to_datetime(self.ts_nanosec_float, unit="ns")

def time_sec_uint64(self):
to_datetime(self.ts_sec_uint, unit="s")

def time_sec_int64(self):
to_datetime(self.ts_sec, unit="s")

Expand Down
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ Performance improvements
- Performance improvement in :meth:`DataFrame.corr` for ``method=pearson`` on data without missing values (:issue:`40956`)
- Performance improvement in some :meth:`GroupBy.apply` operations (:issue:`42992`)
- Performance improvement in :func:`read_stata` (:issue:`43059`)
-
- Performance improvement in :meth:`to_datetime` with ``uint`` dtypes (:issue:`42606`)

.. ---------------------------------------------------------------------------

Expand Down
4 changes: 2 additions & 2 deletions pandas/_libs/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ def array_with_unit_to_datetime(
# if we have nulls that are not type-compat
# then need to iterate

if values.dtype.kind == "i" or values.dtype.kind == "f":
if values.dtype.kind in ["i", "f", "u"]:
iresult = values.astype("i8", copy=False)
# fill missing values by comparing to NPY_NAT
mask = iresult == NPY_NAT
Expand All @@ -263,7 +263,7 @@ def array_with_unit_to_datetime(
):
raise OutOfBoundsDatetime(f"cannot convert input with unit '{unit}'")

if values.dtype.kind == "i":
if values.dtype.kind in ["i", "u"]:
result = (iresult * m).astype("M8[ns]")

elif values.dtype.kind == "f":
Expand Down