Skip to content

Rich comparisons of Timestamps raise TypeError instead of returning NotImplemented #24011

Closed
@AlexandreDecan

Description

@AlexandreDecan

Hello,

Problem description

The problem is that pandas' Timestamp raises TypeError when compared with unsupported types (from pandas' point of view), where I think it should return NotImplemented so that comparisons are delegated to the other object (if supported). See https://github.com/pandas-dev/pandas/blob/master/pandas/_libs/tslibs/timestamps.pyx#L247

The current behaviour prevents other objects to deal with comparisons with Timestamp. For instance, I developed a library that provides arithmetic operations for intervals composed of arbitrary comparable objects. In the context of this library, I defined two specific objects corresponding to negative and positive infinities (that are respectively lower and greater than "everything else"). When these objects are compared with pandas' Timestamp, a TypeError is raised, preventing the comparison to be delegated to my "infinities".

Code Sample

# Let's define infinity, or any object whose magic methods support Timestamp
class Inf:
    def __lt__(self, o): return False
    def __le__(self, o): return isinstance(o, Inf)
    def __gt__(self, o): return not isinstance(o, Inf)
    def __ge__(self, o): return True
    def __eq__(self, o): return isinstance(o, Inf)
    def __ne__(self, o): return not self == o  # Required for Python 2
    def __repr__(self): return '+inf'

# Import pandas and create a timestamp
import pandas as pd
timestamp = pd.Timestamp('2018-11-30')

# Comparison works if compared in *that* order, because magic method is called on Inf
assert Inf() > timestamp
assert not (Inf() < timestamp)

# ... but not when magic method is called on Timestamp
assert timestamp < Inf()

... raises the following:

pandas/_libs/tslib.pyx in pandas._libs.tslib._Timestamp.__richcmp__()
TypeError: Cannot compare type 'Timestamp' with type 'Inf'

Expected Behaviour

TypeError not raised, assertion holds.

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.5.6.final.0
python-bits: 64
OS: Linux
OS-release: 4.18.17-300.fc29.x86_64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: fr_BE.UTF-8
LOCALE: fr_BE.UTF-8

pandas: 0.22.0
pytest: 3.7.4
pip: 9.0.3
setuptools: 40.2.0
Cython: None
numpy: 1.14.0
scipy: 1.0.0
pyarrow: None
xarray: None
IPython: 6.2.1
sphinx: None
patsy: None
dateutil: 2.6.1
pytz: 2017.3
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.1.2
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: 1.0.1
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

Also tested on the latest 0.23.4.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions