Skip to content

Commit b1b70c7

Browse files
authored
BUG: HDFStore unable to create colindex w/o error thrown (#34983)
1 parent 723141d commit b1b70c7

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,7 @@ I/O
10421042
- Bug in :meth:`read_excel` for ODS files removes 0.0 values (:issue:`27222`)
10431043
- Bug in :meth:`ujson.encode` was raising an `OverflowError` with numbers larger than sys.maxsize (:issue: `34395`)
10441044
- Bug in :meth:`HDFStore.append_to_multiple` was raising a ``ValueError`` when the min_itemsize parameter is set (:issue:`11238`)
1045+
- Bug in :meth:`~HDFStore.create_table` now raises an error when `column` argument was not specified in `data_columns` on input (:issue:`28156`)
10451046
- :meth:`read_json` now could read line-delimited json file from a file url while `lines` and `chunksize` are set.
10461047

10471048
Plotting

pandas/io/pytables.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -3569,7 +3569,6 @@ def create_index(self, columns=None, optlevel=None, kind: Optional[str] = None):
35693569
for c in columns:
35703570
v = getattr(table.cols, c, None)
35713571
if v is not None:
3572-
35733572
# remove the index if the kind/optlevel have changed
35743573
if v.is_indexed:
35753574
index = v.index
@@ -3597,6 +3596,13 @@ def create_index(self, columns=None, optlevel=None, kind: Optional[str] = None):
35973596
"data_columns when initializing the table."
35983597
)
35993598
v.create_index(**kw)
3599+
elif c in self.non_index_axes[0][1]:
3600+
# GH 28156
3601+
raise AttributeError(
3602+
f"column {c} is not a data_column.\n"
3603+
f"In order to read column {c} you must reload the dataframe \n"
3604+
f"into HDFStore and include {c} with the data_columns argument."
3605+
)
36003606

36013607
def _read_axes(
36023608
self, where, start: Optional[int] = None, stop: Optional[int] = None

pandas/tests/io/pytables/test_store.py

+31
Original file line numberDiff line numberDiff line change
@@ -1727,6 +1727,37 @@ def col(t, column):
17271727
with pytest.raises(TypeError):
17281728
store.create_table_index("f2")
17291729

1730+
def test_create_table_index_data_columns_argument(self, setup_path):
1731+
# GH 28156
1732+
1733+
with ensure_clean_store(setup_path) as store:
1734+
1735+
with catch_warnings(record=True):
1736+
1737+
def col(t, column):
1738+
return getattr(store.get_storer(t).table.cols, column)
1739+
1740+
# data columns
1741+
df = tm.makeTimeDataFrame()
1742+
df["string"] = "foo"
1743+
df["string2"] = "bar"
1744+
store.append("f", df, data_columns=["string"])
1745+
assert col("f", "index").is_indexed is True
1746+
assert col("f", "string").is_indexed is True
1747+
1748+
msg = "'Cols' object has no attribute 'string2'"
1749+
with pytest.raises(AttributeError, match=msg):
1750+
col("f", "string2").is_indexed
1751+
1752+
# try to index a col which isn't a data_column
1753+
msg = (
1754+
f"column string2 is not a data_column.\n"
1755+
f"In order to read column string2 you must reload the dataframe \n"
1756+
f"into HDFStore and include string2 with the data_columns argument."
1757+
)
1758+
with pytest.raises(AttributeError, match=msg):
1759+
store.create_table_index("f", columns=["string2"])
1760+
17301761
def test_append_hierarchical(self, setup_path):
17311762
index = MultiIndex(
17321763
levels=[["foo", "bar", "baz", "qux"], ["one", "two", "three"]],

0 commit comments

Comments
 (0)