Skip to content

Commit

Permalink
REGR: Bug in indexing with a CategoricalIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
jreback committed Apr 25, 2017
1 parent 8d122e6 commit 5ade9ab
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ def _convert_list_indexer(self, keyarr, kind=None):
indexer = self.categories._convert_list_indexer(keyarr, kind=kind)
return Index(self.codes).get_indexer_for(indexer)

indexer = self.categories.get_indexer(keyarr)
indexer = self.categories.get_indexer(np.asarray(keyarr))
if (indexer == -1).any():
raise KeyError(
"a list-indexer must only "
Expand Down
25 changes: 24 additions & 1 deletion pandas/tests/indexing/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pandas as pd
import numpy as np
from pandas import Series, DataFrame
from pandas import Series, DataFrame, Timestamp
from pandas.util.testing import assert_series_equal, assert_frame_equal
from pandas.util import testing as tm

Expand Down Expand Up @@ -407,3 +407,26 @@ def test_indexing_with_category(self):

res = (cat[['A']] == 'foo')
tm.assert_frame_equal(res, exp)

def test_get_indexer_array(self):
arr = np.array([Timestamp('1999-12-31 00:00:00'),
Timestamp('2000-12-31 00:00:00')], dtype=object)
cats = [Timestamp('1999-12-31 00:00:00'),
Timestamp('2000-12-31 00:00:00')]
ci = pd.CategoricalIndex(cats,
categories=cats,
ordered=False, dtype='category')
result = ci.get_indexer(arr)
expected = np.array([0, 1], dtype='intp')
tm.assert_numpy_array_equal(result, expected)

def test_with_categorical_index(self):
# GH 16115
cats = pd.Categorical([Timestamp('12-31-1999'),
Timestamp('12-31-2000')])

expected = DataFrame([[1, 0], [0, 1]], dtype='uint8',
index=[0, 1], columns=cats)
dummies = pd.get_dummies(cats)
result = dummies[[c for c in dummies.columns]]
assert_frame_equal(result, expected)
2 changes: 1 addition & 1 deletion pandas/tests/reshape/test_reshape.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,8 +490,8 @@ def test_dataframe_dummies_with_categorical(self):
'cat_x', 'cat_y']]
assert_frame_equal(result, expected)

# GH12402 Add a new parameter `drop_first` to avoid collinearity
def test_basic_drop_first(self):
# GH12402 Add a new parameter `drop_first` to avoid collinearity
# Basic case
s_list = list('abc')
s_series = Series(s_list)
Expand Down

0 comments on commit 5ade9ab

Please sign in to comment.