Skip to content

REGR: to_hdf raising AssertionError with boolean index #48696

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.5.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Fixed regressions

Bug fixes
~~~~~~~~~
-
- Bug in :meth:`DataFrame.to_hdf` raising ``AssertionError`` with boolean index (:issue:`48667`)
-

.. ---------------------------------------------------------------------------
Expand Down
9 changes: 7 additions & 2 deletions pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@

from pandas.core.dtypes.common import (
ensure_object,
is_bool_dtype,
is_categorical_dtype,
is_complex_dtype,
is_datetime64_dtype,
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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(
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/io/pytables/test_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)