diff --git a/doc/source/whatsnew/v1.5.1.rst b/doc/source/whatsnew/v1.5.1.rst index e5c860a64a255..0c00e26b40a3d 100644 --- a/doc/source/whatsnew/v1.5.1.rst +++ b/doc/source/whatsnew/v1.5.1.rst @@ -25,6 +25,7 @@ Fixed regressions Bug fixes ~~~~~~~~~ - Bug in :meth:`Series.__getitem__` not falling back to positional for integer keys and boolean :class:`Index` (:issue:`48653`) +- Bug in :meth:`DataFrame.to_hdf` raising ``AssertionError`` with boolean index (:issue:`48667`) - .. --------------------------------------------------------------------------- diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index 39273a28b6d0f..b9adb8b852fa4 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -65,6 +65,7 @@ from pandas.core.dtypes.common import ( ensure_object, + is_bool_dtype, is_categorical_dtype, is_complex_dtype, is_datetime64_dtype, @@ -4902,7 +4903,11 @@ def _convert_index(name: str, index: Index, encoding: str, errors: str) -> Index kind = _dtype_to_kind(dtype_name) atom = DataIndexableCol._get_atom(converted) - if isinstance(index, Int64Index) or needs_i8_conversion(index.dtype): + if ( + isinstance(index, Int64Index) + or needs_i8_conversion(index.dtype) + or is_bool_dtype(index.dtype) + ): # Includes Int64Index, RangeIndex, DatetimeIndex, TimedeltaIndex, PeriodIndex, # in which case "kind" is "integer", "integer", "datetime64", # "timedelta64", and "integer", respectively. @@ -4965,7 +4970,7 @@ def _unconvert_index(data, kind: str, encoding: str, errors: str) -> np.ndarray index = np.asarray([date.fromordinal(v) for v in data], dtype=object) except (ValueError): index = np.asarray([date.fromtimestamp(v) for v in data], dtype=object) - elif kind in ("integer", "float"): + elif kind in ("integer", "float", "bool"): index = np.asarray(data) elif kind in ("string"): index = _unconvert_string_array( diff --git a/pandas/tests/io/pytables/test_store.py b/pandas/tests/io/pytables/test_store.py index e8f4e7ee92fc3..db87c8facbfdb 100644 --- a/pandas/tests/io/pytables/test_store.py +++ b/pandas/tests/io/pytables/test_store.py @@ -1026,3 +1026,16 @@ def test_hdfstore_strides(setup_path): with ensure_clean_store(setup_path) as store: store.put("df", df) assert df["a"].values.strides == store["df"]["a"].values.strides + + +def test_store_bool_index(setup_path): + # GH#48667 + df = DataFrame([[1]], columns=[True], index=Index([False], dtype="bool")) + expected = df.copy() + + # # Test to make sure defaults are to not drop. + # # Corresponding to Issue 9382 + with ensure_clean_path(setup_path) as path: + df.to_hdf(path, "a") + result = read_hdf(path, "a") + tm.assert_frame_equal(expected, result)