Skip to content

Commit 4119bdd

Browse files
committed
BUG: Failure in empty boolean CategoricalIndex
Fixes GH #22702.
1 parent d04e6ae commit 4119bdd

File tree

3 files changed

+5
-3
lines changed

3 files changed

+5
-3
lines changed

pandas/core/arrays/categorical.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2439,11 +2439,15 @@ 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+
# To prevent erroneous dtype coercion in _get_data_algo, retrieve
2448+
# the underlying numpy array. gh-22702
2449+
values = getattr(values, 'values', values)
2450+
categories = getattr(categories, 'values', categories)
24472451

24482452
(hash_klass, vec_klass), vals = _get_data_algo(values, _hashtables)
24492453
(_, _), 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.Categorical([], categories=[True, False])

pandas/tests/indexes/test_category.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ def test_construction_with_dtype(self):
136136
result = CategoricalIndex(idx, categories=idx, ordered=True)
137137
tm.assert_index_equal(result, expected, exact=True)
138138

139-
@pytest.mark.xfail
140139
def test_construction_empty_with_bool_categories(self):
141140
# see gh-22702
142141
cat = pd.CategoricalIndex([], categories=[True, False])

0 commit comments

Comments
 (0)