Skip to content

Commit ad4155d

Browse files
committed
BUG: Failure in empty boolean CategoricalIndex
Fixes GH #22702.
1 parent d8a6238 commit ad4155d

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

pandas/core/arrays/categorical.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2439,11 +2439,16 @@ def _get_codes_for_values(values, categories):
24392439
"""
24402440
utility routine to turn values into codes given the specified categories
24412441
"""
2442-
24432442
from pandas.core.algorithms import _get_data_algo, _hashtables
24442443
if not is_dtype_equal(values.dtype, categories.dtype):
24452444
values = ensure_object(values)
24462445
categories = ensure_object(categories)
2446+
else:
2447+
# These may be Index, in which case their dtype would be coerced
2448+
# as part of _get_data_algo; in that case, we should retrieve the
2449+
# underlying numpy array instead.
2450+
values = getattr(values, 'values', values)
2451+
categories = getattr(categories, 'values', categories)
24472452

24482453
(hash_klass, vec_klass), vals = _get_data_algo(values, _hashtables)
24492454
(_, _), cats = _get_data_algo(categories, _hashtables)

pandas/tests/arrays/categorical/test_constructors.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ def test_constructor_empty(self):
4242
expected = pd.Int64Index([1, 2, 3])
4343
tm.assert_index_equal(c.categories, expected)
4444

45-
@pytest.mark.xfail
4645
def test_constructor_empty_boolean(self):
4746
# see gh-22702
4847
cat = pd.CategoricalIndex([], categories=[True, False])

0 commit comments

Comments
 (0)