Skip to content

Commit c917512

Browse files
phoflnoatamir
authored andcommitted
REGR: to_hdf raising AssertionError with boolean index (pandas-dev#48696)
* REGR: to_hdf raising AssertionError with boolean index * Add gh ref
1 parent 035ea3b commit c917512

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
@@ -24,7 +24,7 @@ Fixed regressions
2424

2525
Bug fixes
2626
~~~~~~~~~
27-
-
27+
- Bug in :meth:`DataFrame.to_hdf` raising ``AssertionError`` with boolean index (:issue:`48667`)
2828
-
2929

3030
.. ---------------------------------------------------------------------------

pandas/io/pytables.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565

6666
from pandas.core.dtypes.common import (
6767
ensure_object,
68+
is_bool_dtype,
6869
is_categorical_dtype,
6970
is_complex_dtype,
7071
is_datetime64_dtype,
@@ -4902,7 +4903,11 @@ def _convert_index(name: str, index: Index, encoding: str, errors: str) -> Index
49024903
kind = _dtype_to_kind(dtype_name)
49034904
atom = DataIndexableCol._get_atom(converted)
49044905

4905-
if isinstance(index, Int64Index) or needs_i8_conversion(index.dtype):
4906+
if (
4907+
isinstance(index, Int64Index)
4908+
or needs_i8_conversion(index.dtype)
4909+
or is_bool_dtype(index.dtype)
4910+
):
49064911
# Includes Int64Index, RangeIndex, DatetimeIndex, TimedeltaIndex, PeriodIndex,
49074912
# in which case "kind" is "integer", "integer", "datetime64",
49084913
# "timedelta64", and "integer", respectively.
@@ -4965,7 +4970,7 @@ def _unconvert_index(data, kind: str, encoding: str, errors: str) -> np.ndarray
49654970
index = np.asarray([date.fromordinal(v) for v in data], dtype=object)
49664971
except (ValueError):
49674972
index = np.asarray([date.fromtimestamp(v) for v in data], dtype=object)
4968-
elif kind in ("integer", "float"):
4973+
elif kind in ("integer", "float", "bool"):
49694974
index = np.asarray(data)
49704975
elif kind in ("string"):
49714976
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)