From 84237c80056545a65fdacc0fb0bedd9af383af53 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Wed, 21 Sep 2022 14:51:16 +0200 Subject: [PATCH 1/2] REGR: to_hdf raising AssertionError with boolean index --- doc/source/whatsnew/v1.5.1.rst | 2 +- pandas/io/pytables.py | 9 +++++++-- pandas/tests/io/pytables/test_store.py | 13 +++++++++++++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v1.5.1.rst b/doc/source/whatsnew/v1.5.1.rst index 9d40d9118db32..8a9912e6f4ad5 100644 --- a/doc/source/whatsnew/v1.5.1.rst +++ b/doc/source/whatsnew/v1.5.1.rst @@ -23,7 +23,7 @@ Fixed regressions Bug fixes ~~~~~~~~~ -- +- Bug in :meth:`DataFrame.to_hdf` raising ``AssertionError`` with boolean index (:issue:`50000`) - .. --------------------------------------------------------------------------- diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index 75cbf57f82644..84b16402894c1 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..a0e63950775d3 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# + 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) From 6b788e9254b95cc63995fc1afc7541d9574dfb30 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Wed, 21 Sep 2022 14:53:22 -0700 Subject: [PATCH 2/2] Add gh ref --- doc/source/whatsnew/v1.5.1.rst | 2 +- pandas/tests/io/pytables/test_store.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v1.5.1.rst b/doc/source/whatsnew/v1.5.1.rst index 8a9912e6f4ad5..0d5f7cf81de69 100644 --- a/doc/source/whatsnew/v1.5.1.rst +++ b/doc/source/whatsnew/v1.5.1.rst @@ -23,7 +23,7 @@ Fixed regressions Bug fixes ~~~~~~~~~ -- Bug in :meth:`DataFrame.to_hdf` raising ``AssertionError`` with boolean index (:issue:`50000`) +- Bug in :meth:`DataFrame.to_hdf` raising ``AssertionError`` with boolean index (:issue:`48667`) - .. --------------------------------------------------------------------------- diff --git a/pandas/tests/io/pytables/test_store.py b/pandas/tests/io/pytables/test_store.py index a0e63950775d3..db87c8facbfdb 100644 --- a/pandas/tests/io/pytables/test_store.py +++ b/pandas/tests/io/pytables/test_store.py @@ -1029,7 +1029,7 @@ def test_hdfstore_strides(setup_path): def test_store_bool_index(setup_path): - # GH# + # GH#48667 df = DataFrame([[1]], columns=[True], index=Index([False], dtype="bool")) expected = df.copy()