Skip to content

Commit

Permalink
REGR: to_hdf raising AssertionError with boolean index (pandas-dev#48696
Browse files Browse the repository at this point in the history
)

* REGR: to_hdf raising AssertionError with boolean index

* Add gh ref
  • Loading branch information
phofl committed Sep 22, 2022
1 parent 34e2b21 commit 0a9f5ac
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.5.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
-

.. ---------------------------------------------------------------------------
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)

0 comments on commit 0a9f5ac

Please sign in to comment.