-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: Timedelta created by to_timedelta does not add correctly to datetime in Pandas 2 #53643
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 have an explanation but not a solution. delta_2 here has unit="s". Timedeltas with unit="s" can go outside the range supported by datetime.timedelta, so the way they are initialized passes seconds=0 to the parent class (datetime.timedelta) constructor. So the pytimedelta struct (which pandas ignores) looks like timedelta(0). When you do dt + delta_2, that calls the pydatetime's Reversing the operation and doing delta_2 + dt gives the correct result, as does pd.Timestamp(dt) + delta_2. A solution here would need to be in the stdlib |
No way! >>> import datetime
>>> import pandas as pd
>>> issubclass(pd.Timedelta, datetime.timedelta)
True The Liskov substitution principle requires that By the way, why is |
Just caught a bug caused by this while trying to upgrade to pandas 2.0. This is both quite concerning (as mentioned above, I really don't want to sound like I'm demanding a fix for a bug/issue in an open source library/project, but I think solving this should be priority. |
Maybe a silly suggestion, but could we not simply have
Could you reformulate this solution as an issue with the stdlib? As it stands, I don't understand what would be wrong there.
Since |
There is no easy solution, right now: the problem is with See this example:
So python stdlib As a general comment, maybe By the way, there is a generale feature of Python that allows subclasses to override their ancestors’ operations, see the note at the end of https://docs.python.org/3/reference/datamodel.html#emulating-numeric-types. In fact
works as expected, because
So maybe a way forward is to convince the CPython developers that special code should be added to From the side of pandas, the obvious solution would be to ensure that
which works as expected. Bottom line: do not mix python |
Do we understand why this wasn't an issue in |
curious: what is the use case for this? If datetime arithmetic is not guaranteed to work anyway, wouldn't it be reasonable to constraint Code in question: pandas/pandas/_libs/tslibs/timedeltas.pyx Lines 965 to 976 in adec21f
This was introduced in: #47641 |
Pandas datetime arithmetic is guaranteed to work: it is mixed python and pandas arithmetic which is problematic. Simply put, the pytimedelta structure could overflow, so it is simply not initialised for resolutions grater than µs. Note that this structure is not needed for Pandas temporal arithmetic, but only to claim that Here a comparison to numpy behaviour, which was the model for Pandas implementation.
Main takes:
Maybe a mitigation could be to initialise the pytimedelta structure when this is possible without overflow, even if the resolution is greater than µs? Moreover, here an other problem, which shows up on the other end of the spectrum:
but
In fact
is 86399999999999999999 µs, which is 67 bit long. And here we are again at the starting point:
So there is no doubt that it is impossible to have these two types mix and match smoothly... Pandas tried to obtain this compatibility, but there are a lot of rough edges. |
I'm probably missing some context here, but it sounds like making Could any devs chime in on the feasibility of this? Could it be rolled out as part of pandas 2.0, or would it have to wait for pandas 3.0? One alternative might be to make the internal representation not 0 for timedeltas with second/millisecond "frequency", and instead capping them at the largest possible value for |
I agree: silently clipping at 86399999999999999999 µs would be a bad option. I fear that there is no easy way forward here, as also
so that a complete overhaul of the pandas datetime system should be implemented. |
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
It seems that the
pd.Timedelta
object produced by callingpd.to_timedelta
on apd.DateTimeIndex
frequency can no longer be added to adatetime.datetime
object sincepandas>1.5.3
.It's not evident to me why the two
pd.Timedelta
objects (appearing to be equal) are behaving differently.Expected Behavior
Running the above example in
pandas==1.5.3
(and also 1.4.4) works as expected.Installed Versions
INSTALLED VERSIONS
commit : 4149f32
python : 3.9.9.final.0
python-bits : 64
OS : Linux
OS-release : 5.15.11-76051511-generic
Version : #202112220937
164018548121.04~b3a2c21-Ubuntu SMP Mon Jan 3 16:5machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 2.1.0.dev0+956.g4149f32388
numpy : 1.22.4
pytz : 2022.2.1
dateutil : 2.8.2
setuptools : 65.2.0
pip : 23.1.2
Cython : 0.29.30
pytest : 7.0.1
hypothesis : None
sphinx : 4.4.0
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : 1.1
pymysql : None
psycopg2 : 2.9.3
jinja2 : 3.1.2
IPython : None
pandas_datareader: None
bs4 : 4.11.1
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.5.1
numba : 0.56.0
numexpr : None
odfpy : None
openpyxl : 3.0.10
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.8.0
snappy : None
sqlalchemy : 1.4.31
tables : None
tabulate : 0.8.9
xarray : None
xlrd : 2.0.1
zstandard : None
tzdata : 2022.5
qtpy : None
pyqt5 : None
The text was updated successfully, but these errors were encountered: