Skip to content

Commit fc87f6d

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

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

pandas/io/pytables.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,8 +904,22 @@ 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 in HDFStore."
915+
)
916+
except AttributeError:
917+
# some storer types (e.g. Legacy format) may not have `obj`; skip check
918+
pass
919+
907920
s.infer_axes()
908921

922+
909923
# function to call on iteration
910924
def func(_start, _stop, _where):
911925
return s.read(start=_start, stop=_stop, where=_where, columns=columns)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import pytest
2+
import pandas as pd
3+
import numpy as np
4+
from datetime import datetime
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)