Skip to content
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

Comparing Timedelta and NaT gives inconsistent results depending on order #26039

Closed
frexvahi opened this issue Apr 10, 2019 · 3 comments · Fixed by #26046
Closed

Comparing Timedelta and NaT gives inconsistent results depending on order #26039

frexvahi opened this issue Apr 10, 2019 · 3 comments · Fixed by #26046
Labels
Bug Datetime Datetime data dtype
Milestone

Comments

@frexvahi
Copy link
Contributor

frexvahi commented Apr 10, 2019

Code Sample, a copy-pastable example if possible

In [1]: import pandas as pd                                                                                                                                                 

In [2]: pd.Timedelta(0) > pd.Timedelta(None)                                              
TypeError: Cannot compare type Timedelta with type NaTType

In [3]: pd.Timedelta(None) < pd.Timedelta(0)                                                                                                                                            
Out[3]: False

Problem description

I would expect the result of a < b to be the same as that of b > a but in this case one returns False and the other raises TypeError.

Related issue: #24983 "Separate NaT values for Timedelta and Period" - if pd.Timedelta(None) gave a value of type Timedelta then maybe this error would not have occurred.

Expected Output

Either both False or both TypeError (both False would be more helpful)

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.6.8.final.0
python-bits: 64
OS: Linux
OS-release: 4.15.0-46-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_GB.UTF-8
LOCALE: en_GB.UTF-8

pandas: 0.24.2
pytest: 4.3.1
pip: 19.0.3
setuptools: 40.8.0
Cython: 0.29.5
numpy: 1.16.2
scipy: None
pyarrow: None
xarray: 0.11.3
IPython: 7.3.0
sphinx: None
patsy: None
dateutil: 2.6.1
pytz: 2018.9
blosc: None
bottleneck: None
tables: 3.4.4
numexpr: 2.6.9
feather: None
matplotlib: None
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml.etree: None
bs4: None
html5lib: 1.0.1
sqlalchemy: 1.2.18
pymysql: None
psycopg2: 2.7.6.1 (dt dec pq3 ext lo64)
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
gcsfs: None

@frexvahi
Copy link
Contributor Author

N.B. Timedelta == NaT and NaT == Timedelta both return False

@jreback
Copy link
Contributor

jreback commented Apr 10, 2019

pls check this on master, this has been sustantially reworked

@frexvahi
Copy link
Contributor Author

On master 6d9b702 I get the same problem.

>>> pd.Timedelta(None) > pd.Timedelta(0)
False
>>> pd.Timedelta(0) > pd.Timedelta(None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pandas/_libs/tslibs/timedeltas.pyx", line 786, in pandas._libs.tslibs.timedeltas._Timedelta.__richcmp__
TypeError: Cannot compare type Timedelta with type NaTType

Also pd.show_versions() raises an error:

>>> pd.show_versions()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "~/.conda/envs/pandas-dev/lib/python3.7/site-packages/pandas/util/_print_versions.py", line 109, in show_versions
    ver = ver_f(mod)
  File "~/.conda/envs/pandas-dev/lib/python3.7/site-packages/pandas/util/_print_versions.py", line 72, in <lambda>
    ("xarray", lambda mod: mod.__version__),
AttributeError: module 'xarray' has no attribute '__version__'
>>> pd.__version__
'0.21.0.dev+4045.g6d9b702a6'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Datetime Datetime data dtype
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants