Skip to content

BUG: Regression from v2.1.4 to v2.2.0 when querying pd.NaT/timestamp comparison  #57068

Closed
@TheDurableDane

Description

@TheDurableDane

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

import pandas as pd

now = pd.Timestamp.utcnow()
df = pd.DataFrame({'a': [None, None]})
df.loc[:, 'a'] = pd.to_datetime(df.loc[:, 'a'], utc=True)
df2 = df.query('a > @now')

Issue Description

The final line of the reproducible example throws an error:

TypeError: Invalid comparison between dtype=datetime64[ns] and Timestamp

in v2.2.0 but not in v2.1.4. I suspect this is not intended behavior. If not, I would like to know why the behavior has changed and how to fix my code.

Expected Behavior

The expected behavior is as in v2.1.4 which results in df2 being an empty dataframe.

Installed Versions

INSTALLED VERSIONS

commit : fd3f571
python : 3.10.12.final.0
python-bits : 64
OS : Linux
OS-release : 6.5.0-14-generic
Version : #14~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Mon Nov 20 18:15:30 UTC 2
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.2.0
numpy : 1.26.3
pytz : 2023.3.post1
dateutil : 2.8.2
setuptools : 69.0.3
pip : 22.0.4
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.3
IPython : None
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.8.2
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.4
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugRegressionFunctionality that used to work in a prior pandas versionTimestamppd.Timestamp and associated methodsexpressionspd.eval, query

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions