Skip to content

Commit 00a1f2c

Browse files
committed
BUG: Raise error when filtering HDF5 with tz-aware index (GH#61479)
1 parent e2bd8e6 commit 00a1f2c

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

pandas/io/pytables.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,21 @@ def select(
904904
# create the storer and axes
905905
where = _ensure_term(where, scope_level=1)
906906
s = self._create_storer(group)
907+
908+
# Raise an error if trying to query tz-aware index
909+
if where is not None:
910+
try:
911+
index = s.obj.index
912+
if hasattr(index, "tz") and index.tz is not None:
913+
raise ValueError(
914+
"Filtering with `where=` on timezone-aware indexes is not supported "
915+
"in HDFStore."
916+
)
917+
918+
except AttributeError:
919+
# some storer types (e.g. Legacy format) may not have `obj`; skip check
920+
pass
921+
907922
s.infer_axes()
908923

909924
# function to call on iteration
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import pytest
2+
3+
import pandas as pd
4+
5+
6+
@pytest.mark.parametrize("tz", ["US/Eastern", "Europe/Berlin"])
7+
def test_hdf_where_query_on_tzindex_raises(tmp_path, tz):
8+
df = pd.DataFrame({"x": range(5)})
9+
df["dt"] = pd.date_range("2020-01-01", periods=5, tz=tz)
10+
df = df.set_index("dt")
11+
12+
file_path = tmp_path / "test.h5"
13+
df.to_hdf(file_path, key="df", format="table")
14+
15+
with pytest.raises(ValueError, match="invalid variable reference"):
16+
pd.read_hdf(file_path, key="df", where='dt=="2020-01-03 00:00:00-05:00"')

0 commit comments

Comments
 (0)