Skip to content

Commit e9f5832

Browse files
committed
remove defer_to_indexing + add tests
1 parent 618f479 commit e9f5832

File tree

2 files changed

+58
-9
lines changed

2 files changed

+58
-9
lines changed

pandas/core/indexes/category.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@ def get_indexer_non_unique(self, target):
696696

697697
@Appender(_index_shared_docs["_convert_scalar_indexer"])
698698
def _convert_scalar_indexer(self, key, kind=None):
699-
if kind == "loc" or self.categories._defer_to_indexing:
699+
if kind == "loc":
700700
try:
701701
return self.categories._convert_scalar_indexer(key, kind=kind)
702702
except TypeError:

pandas/tests/indexing/test_categorical.py

+57-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import numpy as np
2+
import numpy as np
23
import pytest
34

45
from pandas.core.dtypes.common import is_categorical_dtype
@@ -13,6 +14,7 @@
1314
Interval,
1415
Series,
1516
Timestamp,
17+
Timedelta,
1618
)
1719
from pandas.api.types import CategoricalDtype as CDT
1820
import pandas.util.testing as tm
@@ -763,27 +765,74 @@ def test_map_with_dict_or_series(self):
763765
tm.assert_index_equal(expected, output)
764766

765767
@pytest.mark.parametrize(
766-
"idx_values", [[1, 2, 3], [-1, -2, -3], [1.5, 2.5, 3.5], [-1.5, -2.5, -3.5]]
768+
"idx_values",
769+
[
770+
# python types
771+
[1, 2, 3],
772+
[-1, -2, -3],
773+
[1.5, 2.5, 3.5],
774+
[-1.5, -2.5, -3.5],
775+
# numpy int/uint
776+
*[
777+
np.array([1, 2, 3], dtype=dtype)
778+
for dtype in [
779+
np.int8,
780+
np.int16,
781+
np.int32,
782+
np.int64,
783+
np.uint8,
784+
np.uint16,
785+
np.uint32,
786+
np.uint64,
787+
]
788+
],
789+
# numpy floats
790+
*[
791+
np.array([1.5, 2.5, 3.5], dtype=dtype)
792+
for dtype in (np.float16, np.float32, np.float64)
793+
],
794+
# pandas scalars
795+
[Interval(1, 4), Interval(4, 6), Interval(6, 9)],
796+
[Timestamp(2019, 1, 1), Timestamp(2019, 2, 1), Timestamp(2019, 3, 1)],
797+
[Timedelta(1, "d"), Timedelta(2, "d"), Timedelta(3, "D")],
798+
# pandas Integer arrays
799+
*[
800+
pd.array([1, 2, 3], dtype=dtype)
801+
for dtype in [
802+
"Int8",
803+
"Int16",
804+
"Int32",
805+
"Int64",
806+
"UInt8",
807+
"UInt32",
808+
"UInt64",
809+
]
810+
],
811+
# other pandas arrays
812+
pd.IntervalIndex.from_breaks([1, 4, 6, 9]).array,
813+
pd.date_range("2019-01-01", periods=3).array,
814+
pd.timedelta_range(start="1d", periods=3).array,
815+
],
767816
)
768817
def test_loc_with_non_string_categories(self, idx_values, ordered_fixture):
769818
# GH-17569
770819
cat_idx = CategoricalIndex(idx_values, ordered=ordered_fixture)
771-
cat = DataFrame({"A": ["foo", "bar", "baz"]}, index=cat_idx)
772-
# scalar
773-
result = cat.loc[idx_values[0]]
820+
df = DataFrame({"A": ["foo", "bar", "baz"]}, index=cat_idx)
821+
# scalar selection
822+
result = df.loc[idx_values[0]]
774823
expected = Series(["foo"], index=["A"], name=idx_values[0])
775824
tm.assert_series_equal(result, expected)
776-
# list
777-
result = cat.loc[idx_values[:2]]
825+
# list selection
826+
result = df.loc[idx_values[:2]]
778827
expected = DataFrame(["foo", "bar"], index=cat_idx[:2], columns=["A"])
779828
tm.assert_frame_equal(result, expected)
780829
# scalar assignment
781-
result = cat.copy()
830+
result = df.copy()
782831
result.loc[idx_values[0]] = "qux"
783832
expected = DataFrame({"A": ["qux", "bar", "baz"]}, index=cat_idx)
784833
tm.assert_frame_equal(result, expected)
785834
# list assignment
786-
result = cat.copy()
835+
result = df.copy()
787836
result.loc[idx_values[:2], "A"] = ["qux", "qux2"]
788837
expected = DataFrame({"A": ["qux", "qux2", "baz"]}, index=cat_idx)
789838
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)