|
12 | 12 | Index,
|
13 | 13 | Interval,
|
14 | 14 | Series,
|
| 15 | + Timedelta, |
15 | 16 | Timestamp,
|
| 17 | + conftest, |
16 | 18 | )
|
17 | 19 | from pandas.api.types import CategoricalDtype as CDT
|
18 | 20 | import pandas.util.testing as tm
|
@@ -80,6 +82,13 @@ def test_loc_scalar(self):
|
80 | 82 | with pytest.raises(TypeError, match=msg):
|
81 | 83 | df.loc["d", "C"] = 10
|
82 | 84 |
|
| 85 | + msg = ( |
| 86 | + r"cannot do label indexing on <class 'pandas\.core\.indexes\.category" |
| 87 | + r"\.CategoricalIndex'> with these indexers \[1\] of <class 'int'>" |
| 88 | + ) |
| 89 | + with pytest.raises(TypeError, match=msg): |
| 90 | + df.loc[1] |
| 91 | + |
83 | 92 | def test_getitem_scalar(self):
|
84 | 93 |
|
85 | 94 | cats = Categorical([Timestamp("12-31-1999"), Timestamp("12-31-2000")])
|
@@ -754,3 +763,56 @@ def test_map_with_dict_or_series(self):
|
754 | 763 | output = cur_index.map(mapper)
|
755 | 764 | # Order of categories in output can be different
|
756 | 765 | tm.assert_index_equal(expected, output)
|
| 766 | + |
| 767 | + @pytest.mark.parametrize( |
| 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 | + *[np.array([1, 2, 3], dtype=dtype) for dtype in conftest.ALL_INT_DTYPES], |
| 777 | + # numpy floats |
| 778 | + *[np.array([1.5, 2.5, 3.5], dtype=dtyp) for dtyp in conftest.FLOAT_DTYPES], |
| 779 | + # numpy object |
| 780 | + np.array([1, "b", 3.5], dtype=object), |
| 781 | + # pandas scalars |
| 782 | + [Interval(1, 4), Interval(4, 6), Interval(6, 9)], |
| 783 | + [Timestamp(2019, 1, 1), Timestamp(2019, 2, 1), Timestamp(2019, 3, 1)], |
| 784 | + [Timedelta(1, "d"), Timedelta(2, "d"), Timedelta(3, "D")], |
| 785 | + # pandas Integer arrays |
| 786 | + *[pd.array([1, 2, 3], dtype=dtype) for dtype in conftest.ALL_EA_INT_DTYPES], |
| 787 | + # other pandas arrays |
| 788 | + pd.IntervalIndex.from_breaks([1, 4, 6, 9]).array, |
| 789 | + pd.date_range("2019-01-01", periods=3).array, |
| 790 | + pd.timedelta_range(start="1d", periods=3).array, |
| 791 | + ], |
| 792 | + ) |
| 793 | + def test_loc_with_non_string_categories(self, idx_values, ordered_fixture): |
| 794 | + # GH-17569 |
| 795 | + cat_idx = CategoricalIndex(idx_values, ordered=ordered_fixture) |
| 796 | + df = DataFrame({"A": ["foo", "bar", "baz"]}, index=cat_idx) |
| 797 | + |
| 798 | + # scalar selection |
| 799 | + result = df.loc[idx_values[0]] |
| 800 | + expected = Series(["foo"], index=["A"], name=idx_values[0]) |
| 801 | + tm.assert_series_equal(result, expected) |
| 802 | + |
| 803 | + # list selection |
| 804 | + result = df.loc[idx_values[:2]] |
| 805 | + expected = DataFrame(["foo", "bar"], index=cat_idx[:2], columns=["A"]) |
| 806 | + tm.assert_frame_equal(result, expected) |
| 807 | + |
| 808 | + # scalar assignment |
| 809 | + result = df.copy() |
| 810 | + result.loc[idx_values[0]] = "qux" |
| 811 | + expected = DataFrame({"A": ["qux", "bar", "baz"]}, index=cat_idx) |
| 812 | + tm.assert_frame_equal(result, expected) |
| 813 | + |
| 814 | + # list assignment |
| 815 | + result = df.copy() |
| 816 | + result.loc[idx_values[:2], "A"] = ["qux", "qux2"] |
| 817 | + expected = DataFrame({"A": ["qux", "qux2", "baz"]}, index=cat_idx) |
| 818 | + tm.assert_frame_equal(result, expected) |
0 commit comments