Skip to content

Commit a10837b

Browse files
Backport PR #48696 on branch 1.5.x (REGR: to_hdf raising AssertionError with boolean index) (#48716)
Backport PR #48696: REGR: to_hdf raising AssertionError with boolean index Co-authored-by: Patrick Hoefler <[email protected]>
1 parent 7cbc9f4 commit a10837b

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

doc/source/whatsnew/v1.5.1.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Fixed regressions
2525

2626
Bug fixes
2727
~~~~~~~~~
28-
-
28+
- Bug in :meth:`DataFrame.to_hdf` raising ``AssertionError`` with boolean index (:issue:`48667`)
2929
-
3030

3131
.. ---------------------------------------------------------------------------

pandas/io/pytables.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363

6464
from pandas.core.dtypes.common import (
6565
ensure_object,
66+
is_bool_dtype,
6667
is_categorical_dtype,
6768
is_complex_dtype,
6869
is_datetime64_dtype,
@@ -4893,7 +4894,11 @@ def _convert_index(name: str, index: Index, encoding: str, errors: str) -> Index
48934894
kind = _dtype_to_kind(dtype_name)
48944895
atom = DataIndexableCol._get_atom(converted)
48954896

4896-
if isinstance(index, Int64Index) or needs_i8_conversion(index.dtype):
4897+
if (
4898+
isinstance(index, Int64Index)
4899+
or needs_i8_conversion(index.dtype)
4900+
or is_bool_dtype(index.dtype)
4901+
):
48974902
# Includes Int64Index, RangeIndex, DatetimeIndex, TimedeltaIndex, PeriodIndex,
48984903
# in which case "kind" is "integer", "integer", "datetime64",
48994904
# "timedelta64", and "integer", respectively.
@@ -4956,7 +4961,7 @@ def _unconvert_index(data, kind: str, encoding: str, errors: str) -> np.ndarray
49564961
index = np.asarray([date.fromordinal(v) for v in data], dtype=object)
49574962
except (ValueError):
49584963
index = np.asarray([date.fromtimestamp(v) for v in data], dtype=object)
4959-
elif kind in ("integer", "float"):
4964+
elif kind in ("integer", "float", "bool"):
49604965
index = np.asarray(data)
49614966
elif kind in ("string"):
49624967
index = _unconvert_string_array(

pandas/tests/io/pytables/test_store.py

+13
Original file line numberDiff line numberDiff line change
@@ -1026,3 +1026,16 @@ def test_hdfstore_strides(setup_path):
10261026
with ensure_clean_store(setup_path) as store:
10271027
store.put("df", df)
10281028
assert df["a"].values.strides == store["df"]["a"].values.strides
1029+
1030+
1031+
def test_store_bool_index(setup_path):
1032+
# GH#48667
1033+
df = DataFrame([[1]], columns=[True], index=Index([False], dtype="bool"))
1034+
expected = df.copy()
1035+
1036+
# # Test to make sure defaults are to not drop.
1037+
# # Corresponding to Issue 9382
1038+
with ensure_clean_path(setup_path) as path:
1039+
df.to_hdf(path, "a")
1040+
result = read_hdf(path, "a")
1041+
tm.assert_frame_equal(expected, result)

0 commit comments

Comments
 (0)