Skip to content

BUG: Raise error when filtering HDF5 with tz-aware index (GH#61479) #61482

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,21 @@ def select(
# create the storer and axes
where = _ensure_term(where, scope_level=1)
s = self._create_storer(group)

# Raise an error if trying to query tz-aware index
if where is not None:
try:
index = s.obj.index
if hasattr(index, "tz") and index.tz is not None:
raise ValueError(
"Filtering with `where=` on timezone-aware indexes is not supported "
"in HDFStore."
)

except AttributeError:
# some storer types (e.g. Legacy format) may not have `obj`; skip check
pass

s.infer_axes()

# function to call on iteration
Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/io/pytables/test_timezone_bug.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import pytest

import pandas as pd


@pytest.mark.parametrize("tz", ["US/Eastern", "Europe/Berlin"])
def test_hdf_where_query_on_tzindex_raises(tmp_path, tz):
df = pd.DataFrame({"x": range(5)})
df["dt"] = pd.date_range("2020-01-01", periods=5, tz=tz)
df = df.set_index("dt")

file_path = tmp_path / "test.h5"
df.to_hdf(file_path, key="df", format="table")

with pytest.raises(ValueError, match="invalid variable reference"):
pd.read_hdf(file_path, key="df", where='dt=="2020-01-03 00:00:00-05:00"')
Loading