Skip to content

Commit

Permalink
BUG: fix to_datetime with np.datetime64[ps] giving wrong conversion (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
yuanx749 authored Nov 20, 2024
1 parent 6a7685f commit 0e20990
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,7 @@ Datetimelike
- Bug in :meth:`Series.dt.microsecond` producing incorrect results for pyarrow backed :class:`Series`. (:issue:`59154`)
- Bug in :meth:`to_datetime` not respecting dayfirst if an uncommon date string was passed. (:issue:`58859`)
- Bug in :meth:`to_datetime` reports incorrect index in case of any failure scenario. (:issue:`58298`)
- Bug in :meth:`to_datetime` wrongly converts when ``arg`` is a ``np.datetime64`` object with unit of ``ps``. (:issue:`60341`)
- Bug in setting scalar values with mismatched resolution into arrays with non-nanosecond ``datetime64``, ``timedelta64`` or :class:`DatetimeTZDtype` incorrectly truncating those scalars (:issue:`56410`)

Timedelta
Expand Down
11 changes: 6 additions & 5 deletions pandas/_libs/src/vendored/numpy/datetime/np_datetime.c
Original file line number Diff line number Diff line change
Expand Up @@ -660,11 +660,12 @@ void pandas_datetime_to_datetimestruct(npy_datetime dt, NPY_DATETIMEUNIT base,
perday = 24LL * 60 * 60 * 1000 * 1000 * 1000 * 1000;

set_datetimestruct_days(extract_unit(&dt, perday), out);
out->hour = (npy_int32)extract_unit(&dt, 1000LL * 1000 * 1000 * 60 * 60);
out->min = (npy_int32)extract_unit(&dt, 1000LL * 1000 * 1000 * 60);
out->sec = (npy_int32)extract_unit(&dt, 1000LL * 1000 * 1000);
out->us = (npy_int32)extract_unit(&dt, 1000LL);
out->ps = (npy_int32)(dt * 1000);
out->hour =
(npy_int32)extract_unit(&dt, 1000LL * 1000 * 1000 * 1000 * 60 * 60);
out->min = (npy_int32)extract_unit(&dt, 1000LL * 1000 * 1000 * 1000 * 60);
out->sec = (npy_int32)extract_unit(&dt, 1000LL * 1000 * 1000 * 1000);
out->us = (npy_int32)extract_unit(&dt, 1000LL * 1000);
out->ps = (npy_int32)(dt);
break;

case NPY_FR_fs:
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/tools/test_to_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -3668,3 +3668,12 @@ def test_to_datetime_mixed_awareness_mixed_types(aware_val, naive_val, naive_fir
to_datetime(vec, format="mixed")
with pytest.raises(ValueError, match=msg):
DatetimeIndex(vec)


def test_to_datetime_wrapped_datetime64_ps():
# GH#60341
result = to_datetime([np.datetime64(1901901901901, "ps")])
expected = DatetimeIndex(
["1970-01-01 00:00:01.901901901"], dtype="datetime64[ns]", freq=None
)
tm.assert_index_equal(result, expected)

0 comments on commit 0e20990

Please sign in to comment.