Skip to content

BUG: to_datetime wraps datetime64[ps] as if it were datetime64[ns] #60341

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
2 of 3 tasks
kmuehlbauer opened this issue Nov 16, 2024 · 6 comments · Fixed by #60342
Closed
2 of 3 tasks

BUG: to_datetime wraps datetime64[ps] as if it were datetime64[ns] #60341

kmuehlbauer opened this issue Nov 16, 2024 · 6 comments · Fixed by #60342
Labels
Bug Non-Nano datetime64/timedelta64 with non-nanosecond resolution Timestamp pd.Timestamp and associated methods

Comments

@kmuehlbauer
Copy link
Contributor

kmuehlbauer commented Nov 16, 2024

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import numpy as np
import pandas as pd

print("Good:", pd.to_datetime([np.datetime64(1901901901901, "as")]))
print("Good:", pd.to_datetime([np.datetime64(1901901901901, "fs")]))
print(" Bad:", pd.to_datetime([np.datetime64(1901901901901, "ps")]))
print("Good:", pd.to_datetime([np.datetime64(1901901901901, "ns")]))
print("Good:", pd.to_datetime([np.datetime64(1901901901901, "us")]))
print("Good:", pd.to_datetime([np.datetime64(1901901901901, "ms")]))


# Good: DatetimeIndex(['1970-01-01 00:00:00.000001901'], dtype='datetime64[ns]', freq=None)
# Good: DatetimeIndex(['1970-01-01 00:00:00.001901901'], dtype='datetime64[ns]', freq=None)
#  Bad: DatetimeIndex(['1970-01-01 00:31:41.901901901'], dtype='datetime64[ns]', freq=None)
# Good: DatetimeIndex(['1970-01-01 00:31:41.901901901'], dtype='datetime64[ns]', freq=None)
# Good: DatetimeIndex(['1970-01-23 00:18:21.901901'], dtype='datetime64[ns]', freq=None)
# Good: DatetimeIndex(['2030-04-08 18:05:01.901000'], dtype='datetime64[ns]', freq=None)

Issue Description

Wrapping np.datetime64[ps] data with pd.to_datetime is wrongly converted as if it were np.datetime64[ns].

Expected Behavior

DatetimeIndex(['1970-01-01 00:00:01.901901901'], dtype='datetime64[ns]', freq=None)

Installed Versions

INSTALLED VERSIONS

commit : d9cdd2e
python : 3.12.5.final.0
python-bits : 64
OS : Linux
OS-release : 5.15.0-122-generic
Version : #132-Ubuntu SMP Thu Aug 29 13:45:52 UTC 2024
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : de_DE.UTF-8
LOCALE : de_DE.UTF-8
pandas : 2.2.2
numpy : 2.1.1
pytz : 2024.2
dateutil : 2.9.0
setuptools : 73.0.1
pip : 24.2
Cython : None
pytest : 8.3.3
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.4
IPython : 8.28.0
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.3
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : 2024.9.0
gcsfs : None
matplotlib : 3.9.2
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 17.0.0
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : 2024.9.0
scipy : 1.14.1
sqlalchemy : None
tables : None
tabulate : None
xarray : 2024.10.0
xlrd : None
zstandard : 0.23.0
tzdata : 2024.1
qtpy : None
pyqt5 : None

@kmuehlbauer kmuehlbauer added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Nov 16, 2024
@kmuehlbauer
Copy link
Contributor Author

This was encountered while in an attempt to relax nanosecond resolution over at xarray (pydata/xarray#9618).

@yuanx749
Copy link
Contributor

take

@yuanx749
Copy link
Contributor

Can confirm on main. The bug is from wrong conversion for "ps" case in the C code.

@jorisvandenbossche jorisvandenbossche added Non-Nano datetime64/timedelta64 with non-nanosecond resolution Timestamp pd.Timestamp and associated methods and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Nov 17, 2024
@jorisvandenbossche
Copy link
Member

@kmuehlbauer thanks for the report, and @yuanx749 thanks for looking into it!

@yuanx749
Copy link
Contributor

I've submitted a PR (with a simple test).

@kmuehlbauer
Copy link
Contributor Author

Thanks all!

@yuanx749 yuanx749 removed their assignment Nov 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Non-Nano datetime64/timedelta64 with non-nanosecond resolution Timestamp pd.Timestamp and associated methods
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants