Skip to content

Commit be101ae

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

File tree

3 files changed

+6
-4
lines changed

3 files changed

+6
-4
lines changed

pandas/core/arrays/categorical.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2439,9 +2439,13 @@ 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
2444-
if not is_dtype_equal(values.dtype, categories.dtype):
2443+
if is_dtype_equal(values.dtype, categories.dtype):
2444+
# To prevent erroneous dtype coercion in _get_data_algo, retrieve
2445+
# the underlying numpy array. gh-22702
2446+
values = getattr(values, 'values', values)
2447+
categories = getattr(categories, 'values', categories)
2448+
else:
24452449
values = ensure_object(values)
24462450
categories = ensure_object(categories)
24472451

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)