Skip to content

Commit 5ade9ab

Browse files
committed
REGR: Bug in indexing with a CategoricalIndex
closes pandas-dev#16115
1 parent 8d122e6 commit 5ade9ab

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

pandas/core/indexes/category.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ def _convert_list_indexer(self, keyarr, kind=None):
504504
indexer = self.categories._convert_list_indexer(keyarr, kind=kind)
505505
return Index(self.codes).get_indexer_for(indexer)
506506

507-
indexer = self.categories.get_indexer(keyarr)
507+
indexer = self.categories.get_indexer(np.asarray(keyarr))
508508
if (indexer == -1).any():
509509
raise KeyError(
510510
"a list-indexer must only "

pandas/tests/indexing/test_categorical.py

+24-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import pandas as pd
66
import numpy as np
7-
from pandas import Series, DataFrame
7+
from pandas import Series, DataFrame, Timestamp
88
from pandas.util.testing import assert_series_equal, assert_frame_equal
99
from pandas.util import testing as tm
1010

@@ -407,3 +407,26 @@ def test_indexing_with_category(self):
407407

408408
res = (cat[['A']] == 'foo')
409409
tm.assert_frame_equal(res, exp)
410+
411+
def test_get_indexer_array(self):
412+
arr = np.array([Timestamp('1999-12-31 00:00:00'),
413+
Timestamp('2000-12-31 00:00:00')], dtype=object)
414+
cats = [Timestamp('1999-12-31 00:00:00'),
415+
Timestamp('2000-12-31 00:00:00')]
416+
ci = pd.CategoricalIndex(cats,
417+
categories=cats,
418+
ordered=False, dtype='category')
419+
result = ci.get_indexer(arr)
420+
expected = np.array([0, 1], dtype='intp')
421+
tm.assert_numpy_array_equal(result, expected)
422+
423+
def test_with_categorical_index(self):
424+
# GH 16115
425+
cats = pd.Categorical([Timestamp('12-31-1999'),
426+
Timestamp('12-31-2000')])
427+
428+
expected = DataFrame([[1, 0], [0, 1]], dtype='uint8',
429+
index=[0, 1], columns=cats)
430+
dummies = pd.get_dummies(cats)
431+
result = dummies[[c for c in dummies.columns]]
432+
assert_frame_equal(result, expected)

pandas/tests/reshape/test_reshape.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -490,8 +490,8 @@ def test_dataframe_dummies_with_categorical(self):
490490
'cat_x', 'cat_y']]
491491
assert_frame_equal(result, expected)
492492

493-
# GH12402 Add a new parameter `drop_first` to avoid collinearity
494493
def test_basic_drop_first(self):
494+
# GH12402 Add a new parameter `drop_first` to avoid collinearity
495495
# Basic case
496496
s_list = list('abc')
497497
s_series = Series(s_list)

0 commit comments

Comments
 (0)