Pandas version checks
-
[X] 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
It seems like timedelta rounding broke with the 2.2.0 release or the naming convention for rounding frequency is not properly documented.
Issue Description
Under pandas>=2.2.0, I cannot get any rounding to occur whether I use 'D', 'd', 'day', or 'days':
>>> import pandas as pd
>>> print(pd.__version__)
2.2.1
>>> pd.Series([
... pd.Timedelta('1 days 23:18:00'),
... pd.Timedelta('2 days 07:00:00')]).round('d')
0 1 days 23:18:00
1 2 days 07:00:00
dtype: timedelta64[ns]
Expected Behavior
Before 2.2.0 timedeltas rounded to the nearest day appropriately:
>>> import pandas as pd
>>> print(pd.__version__)
2.1.4
dtype: timedelta64[ns]
>>> pd.Series([
... pd.Timedelta('1 days 23:18:00'),
... pd.Timedelta('2 days 07:00:00')]).round('d')
0 2 days
1 2 days
dtype: timedelta64[ns]
Installed Versions
INSTALLED VERSIONS
commit : bdc79c146c2e32f2cab629be240f01658cfb6cc2 python : 3.11.5.final.0 python-bits : 64 OS : Windows OS-release : 10 Version : 10.0.19045 machine : AMD64 processor : Intel64 Family 6 Model 126 Stepping 5, GenuineIntel byteorder : little LC_ALL : None LANG : en_US.UTF-8 LOCALE : English_United States.1252
pandas : 2.2.1 numpy : 1.24.3 pytz : 2023.3.post1 dateutil : 2.8.2 setuptools : 68.0.0 pip : 23.3 Cython : None pytest : 7.4.0 hypothesis : None sphinx : None blosc : None feather : None xlsxwriter : None lxml.etree : 4.9.3 html5lib : None pymysql : None psycopg2 : None jinja2 : 3.1.2 IPython : 8.15.0 pandas_datareader : None adbc-driver-postgresql: None adbc-driver-sqlite : None bs4 : None bottleneck : None dataframe-api-compat : None fastparquet : None fsspec : None gcsfs : None matplotlib : 3.8.0 numba : 0.58.0 numexpr : 2.8.7 odfpy : None openpyxl : None pandas_gbq : None pyarrow : 14.0.1 pyreadstat : None python-calamine : None pyxlsb : None s3fs : None scipy : 1.11.3 sqlalchemy : None tables : None tabulate : None xarray : None xlrd : None zstandard : None tzdata : 2023.3 qtpy : None pyqt5 : None
Comment From: bergnerjonas
Tried to reproduce. Your example for .round on a Series fails for me as well, however rounding single timedeltas (e.g. pd.Timedelta('1 days 23:18:00').round('d')
) works fine.
Comment From: rhshadrach
Thanks for the report! Result of a git bisect:
commit 6dbeeb4009bbfac5ea1ae2111346f5e9f05b81f4
Author: Lumberbot (aka Jack)
Date: Mon Jan 8 23:24:22 2024 +0100
Backport PR #56767 on branch 2.2.x (BUG: Series.round raising for nullable bool dtype) (#56782)
Backport PR #56767: BUG: Series.round raising for nullable bool dtype
Co-authored-by: Patrick Hoefler
cc @phofl
Note that ser.dt.round('d')
still works. I don't believe that Series.round
having special logic for different dtypes is documented anywhere.
Comment From: phofl
Yeah this is a bit weird, DataFrame.round never worked for those, Series.round now follows this behavior. We can either make both work or keep as is
Comment From: rhshadrach
Making this work for DataFrame.round seems undesirable - numeric columns would take the number of digits as an argument whereas timedelta/datetime would take a frequency. I'd lean toward keeping this as-is.
Comment From: veenstrajelmer
I have encountered the same issue, but then with datetimes. And as @rhshadrach already mentioned, it matters if you use .round()
or .dt.round()
:
import pandas as pd
dtindex = pd.date_range("2010-01-01", "2010-01-01 12:00", periods=3)
df = pd.DataFrame()
df["datetimes"] = dtindex
df["round_1"] = df["datetimes"].round('d') # this does not round in pandas 2.2.0, it did in pandas<=2.1.4
df["round_2"] = df["datetimes"].dt.round('d')
print(df)
These two rounding methods produce different results with pandas>=2.2.0
:
datetimes round_1 round_2
0 2010-01-01 00:00:00 2010-01-01 00:00:00 2010-01-01
1 2010-01-01 06:00:00 2010-01-01 06:00:00 2010-01-01
2 2010-01-01 12:00:00 2010-01-01 12:00:00 2010-01-01
In pandas<=2.1.4
the two rounding methods produce the same results:
datetimes round_1 round_2
0 2010-01-01 00:00:00 2010-01-01 2010-01-01
1 2010-01-01 06:00:00 2010-01-01 2010-01-01
2 2010-01-01 12:00:00 2010-01-01 2010-01-01