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

BUG: Confusing error message when clipping datetimes #44785

Open
3 tasks done
deaglancrew opened this issue Dec 6, 2021 · 1 comment
Open
3 tasks done

BUG: Confusing error message when clipping datetimes #44785

deaglancrew opened this issue Dec 6, 2021 · 1 comment
Labels
Bug Datetime Datetime data dtype

Comments

@deaglancrew
Copy link

  • 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 master branch of pandas.

Reproducible Example

import pandas as pd


pd.Series(
    [1.0, 1.0],
    dtype='datetime64[ns]'
).clip(
    pd.Series(
        [None, 1.0],
        dtype='datetime64[ns]')
)

Issue Description

Clipping with columns that contain NaT values raises "TypeError: '>=' not supported between instances of 'Timestamp' and 'float'".

This can be quite confusing, particularly if you're starting with float data and casting to datetime.

Expected Behavior

Ideally the error message would read: "TypeError: '>=' not supported between instances of 'Timestamp' and 'NaT'".

Installed Versions

INSTALLED VERSIONS

commit : 3e1cfc5
python : 3.9.7.final.0
python-bits : 64
OS : Darwin
OS-release : 21.1.0
Version : Darwin Kernel Version 21.1.0: Wed Oct 13 17:33:23 PDT 2021; root:xnu-8019.41.5~1/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : en_GB.UTF-8
LOCALE : en_GB.UTF-8

pandas : 1.4.0.dev0+1294.g3e1cfc528c
numpy : 1.21.2
pytz : 2021.3
dateutil : 2.8.2
pip : 21.2.4
setuptools : 58.0.4
Cython : 0.29.24
pytest : 6.2.5
hypothesis : 6.29.0
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : 2.9.2 (dt dec pq3 ext lo64)
jinja2 : 3.0.2
IPython : 7.28.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : 2021.11.0
fastparquet : None
gcsfs : None
matplotlib : 3.5.0
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 5.0.0
pyxlsb : None
s3fs : 2021.11.0
scipy : 1.7.3
sqlalchemy : None
tables : None
tabulate : None
xarray : 0.19.0
xlrd : None
xlwt : None
numba : None

@deaglancrew deaglancrew added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Dec 6, 2021
@ms7463
Copy link
Contributor

ms7463 commented Dec 15, 2021

Looks like this bug was introduced here: #40927 (comment)

It replaces missing values with np.inf which is not a compatible comparison with pd.Timestamp.

Until that is fixed, I'd suggest doing this:

pd.Series(
    [1.0, 1.0],
    dtype='datetime64[ns]'
).clip(
    pd.Series(
        [None, 1.0],
        dtype='datetime64[ns]').fillna(pd.Timestamp.min)  # use pd.Timestamp.max for upper clip
)

@mroeschke mroeschke added Datetime Datetime data dtype and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Dec 27, 2021
@jreback jreback added this to the 1.5 milestone Jan 10, 2022
@mroeschke mroeschke removed this from the 1.5 milestone Aug 15, 2022
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.

4 participants