-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: converting to datetime with unit=D, unit=M, or unit=Y fails silently (regression) #51027
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
Comments
I suspect this may have been introduced by #47579 based on the code path I see during debugging, but I haven't confirmed this. If there's a new preferred way to reduce the precision of datetime64s (to shave off time, day, and month data) please let me know. The current docs for to_datetime suggest that 'D' should still be a valid unit, at least (see the section about the |
Ideally, all the date units supported by numpy ('Y', 'M', 'W', and 'D') would work in pandas as they used to (see https://numpy.org/doc/stable/reference/arrays.datetime.html#basic-datetimes ) |
thanks @pfiddy for the report!
I think this just needs documenting better |
Hi @MarcoGorelli, thanks for the response! Can you clarify what you mean by "resolution" there? Are you talking about a resolution to this issue, or a resolution in the sense of a numerical precision on the datetime type, or something else? |
Hi @pfiddy Yeah it's the precision of the dtype, which can be:
for your case, if you just want the date part, would this work? In [16]: col
Out[16]:
0 2023-01-31 20:33:12.191560
1 2023-01-31 20:33:12.191563
2 2023-01-31 20:33:12.191563
3 2023-01-31 20:33:12.191563
4 2023-01-31 20:33:12.191563
dtype: datetime64[ns]
In [17]: col.dt.normalize()
Out[17]:
0 2023-01-31
1 2023-01-31
2 2023-01-31
3 2023-01-31
4 2023-01-31
dtype: datetime64[ns] ? |
I'm from the same organization, and I just wanted to report that this will work. At the moment we are using |
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
Issue Description
Converting a datetime to a datetime with unit='D', 'M', or 'Y' has stopped working, both with .astype() and to_datetime().
This issue first appeared in version 1.5.0 (confirmed that it's not present in 1.4.4) and is still present in the latest version (1.5.3).
Expected Behavior
See example code above -- the lines below the comments should evaluate to True instead of False. (In other words: times should disappear from the data when unit='D', 'M', or 'Y'; days should disappear from the data when unit='M' or 'Y'; and only the year should remain in the data when unit='Y'.)
Installed Versions
INSTALLED VERSIONS
commit : 2e218d1
python : 3.10.8.final.0
python-bits : 64
OS : Darwin
OS-release : 21.6.0
Version : Darwin Kernel Version 21.6.0: Sun Nov 6 23:31:13 PST 2022; root:xnu-8020.240.14~1/RELEASE_ARM64_T6000
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : None
LOCALE : en_US.UTF-8
pandas : 1.5.3
numpy : 1.23.4
pytz : 2022.6
dateutil : 2.8.2
setuptools : 65.5.1
pip : 22.0.4
Cython : 0.29.26
pytest : 6.2.5
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.9.1
html5lib : None
pymysql : 1.0.2
psycopg2 : 2.9.5
jinja2 : 3.1.2
IPython : None
pandas_datareader: None
bs4 : 4.11.1
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : 0.17.8
pyarrow : 8.0.0
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.9.3
snappy : None
sqlalchemy : 1.4.41
tables : None
tabulate : 0.9.0
xarray : None
xlrd : None
xlwt : None
zstandard : None
tzdata : 2022.6
The text was updated successfully, but these errors were encountered: