-
-
Notifications
You must be signed in to change notification settings - Fork 18.2k
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
Timedelta always returning False for equality tests for incompatible types #20829
Comments
Can you try on master? custom = CustomClass()
var1 = datetime.timedelta(seconds=1)
var2 = pandas.Timedelta("1s")
# Following code raises CustomClass exception
var1 == custom
# Following code returns False, does not call CustomClass.__eq__
var2 == custom
## -- End pasted text --
---------------------------------------------------------------------------
Exception Traceback (most recent call last)
<ipython-input-1-82804cdda1a5> in <module>()
16
17 # Following code raises CustomClass exception
---> 18 var1 == custom
19
20 # Following code returns False, does not call CustomClass.__eq__
<ipython-input-1-82804cdda1a5> in __eq__(self, other)
8
9 def __eq__(self, other):
---> 10 raise Exception("Custom Class eq")
11
12
Exception: Custom Class eq |
Same thing on master. Note that in the example we want exceptions both in |
Ah sorry I didn't get to that because of the first exception. cc @jbrockmendel whose been active here recently. I think this is may be non-trivial to support. |
Tom is right, this would be non-trivial to support. There is no branch of That said, if you were careful to always write |
@jbrockmendel I was taking a look at the code, shouldn't it be just a matter of changing this branch: pandas/pandas/_libs/tslibs/timedeltas.pyx Lines 695 to 702 in c85ab08
To return
|
It might be reasonable to return Also if Have you considered using |
Could you elaborate a bit more on than? What would be an example?
Yep that's a good idea for time being! Another question related to it: If I also happen to have a custom Timedelta subclass (for instance allowing to sum a timedelta with "1s" directly). Is there an easy way to make pandas use this custom when working with TimedeltaIndexes? |
Timedelta(whatever) == 6 --> False
Try |
I thought python would fallback automatically to identity comparison if both classes returned NotImplemented for a comparison. Is it a different situation for pandas Timedelta? |
@Sup3rGeo Off the top of my head I can't think of any reason why Timedelta would behave differently, but the check-for-eq-then-ne-then-raise pattern is present in a handful of places in pandas. Maybe try making the substitution you have in mind and see if it causes any test failures? |
Will do it! Just as quick snippet:
|
Code Sample, a copy-pastable example if possible
Problem description
I am trying to implement a version of
pytest.approx
for Timedeltas, which basically returns a class with a custom__eq__
implementation.`Timedelta("5s") == AproxTimedelta(...)
However it does not work with pandas Timedelta because it always return False, so the
__eq__
of AproxTimedelta object is never called.This is not the behavior for python datetime timedelta, as can be seen in the example script.
Expected Output
Exception("Custom Class eq")
raised for both casesOutput of
pd.show_versions()
INSTALLED VERSIONS
commit: None
python: 3.6.3.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 158 Stepping 9, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None
pandas: 0.22.0
pytest: 3.5.0
pip: 10.0.0
setuptools: 38.5.1
Cython: 0.28.2
numpy: 1.14.1
scipy: 1.0.0
pyarrow: None
xarray: None
IPython: 6.3.1
sphinx: 1.7.1
patsy: None
dateutil: 2.6.1
pytz: 2017.2
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.1.1
openpyxl: 1.7.0
xlrd: 1.1.0
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
The text was updated successfully, but these errors were encountered: