Skip to content

"Cannot compare tz-naive and tz-aware datetime-like objects" when querying DatetimeIndex #29463

Closed
@hjfreyer

Description

@hjfreyer

Code Sample, a copy-pastable example if possible

import pandas as pd

df = pd.DataFrame({
    'val': range(10),
    'time': pd.date_range(start='2019-01-01', freq='1d', periods=10, tz='UTC')
})

# This works.
df.query('"2019-01-03 00:00:00+00" < time')

# This raises an exception.
df.set_index('time').query('"2019-01-03 00:00:00+00" < time')

# Perhaps interestingly, this works.
df.set_index('time').reset_index().query('"2019-01-03 00:00:00+00" < time')

Problem description

query shouldn't behave differently when the thing being queried is an index vs a column. Also, the error just seems objectively incorrect, as both values are tz-aware to start with (perhaps it's getting lost along the way).

May be related to #26236 , but the repro is different.

Expected Output

Not an exception.

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.6.7.final.0
python-bits: 64
OS: Linux
OS-release: 4.3.5-smp-821.23.0.0
machine: x86_64
processor:
byteorder: little
LC_ALL: en_US.UTF-8
LANG: None
LOCALE: en_US.UTF-8

pandas: 0.24.1
pytest: None
pip: None
setuptools: unknown
Cython: None
numpy: 1.16.4
scipy: 1.2.1
pyarrow: None
xarray: None
IPython: 2.0.0
sphinx: None
patsy: 0.4.1
dateutil: 2.8.0
pytz: 2019.3
blosc: None
bottleneck: None
tables: 3.5.2
numexpr: 2.6.10dev0
feather: None
matplotlib: 3.0.3
openpyxl: None
xlrd: 1.2.0
xlwt: None
xlsxwriter: None
lxml.etree: None
bs4: None
html5lib: 1.0.1
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10.3
s3fs: None
fastparquet: None
pandas_gbq: 0+unknown
pandas_datareader: None
gcsfs: None

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions