|
1 | 1 | import numpy as np |
| 2 | +import numpy as np |
2 | 3 | import pytest |
3 | 4 |
|
4 | 5 | from pandas.core.dtypes.common import is_categorical_dtype |
|
13 | 14 | Interval, |
14 | 15 | Series, |
15 | 16 | Timestamp, |
| 17 | + Timedelta, |
16 | 18 | ) |
17 | 19 | from pandas.api.types import CategoricalDtype as CDT |
18 | 20 | import pandas.util.testing as tm |
@@ -763,27 +765,74 @@ def test_map_with_dict_or_series(self): |
763 | 765 | tm.assert_index_equal(expected, output) |
764 | 766 |
|
765 | 767 | @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 | + ], |
767 | 816 | ) |
768 | 817 | def test_loc_with_non_string_categories(self, idx_values, ordered_fixture): |
769 | 818 | # GH-17569 |
770 | 819 | 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]] |
774 | 823 | expected = Series(["foo"], index=["A"], name=idx_values[0]) |
775 | 824 | 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]] |
778 | 827 | expected = DataFrame(["foo", "bar"], index=cat_idx[:2], columns=["A"]) |
779 | 828 | tm.assert_frame_equal(result, expected) |
780 | 829 | # scalar assignment |
781 | | - result = cat.copy() |
| 830 | + result = df.copy() |
782 | 831 | result.loc[idx_values[0]] = "qux" |
783 | 832 | expected = DataFrame({"A": ["qux", "bar", "baz"]}, index=cat_idx) |
784 | 833 | tm.assert_frame_equal(result, expected) |
785 | 834 | # list assignment |
786 | | - result = cat.copy() |
| 835 | + result = df.copy() |
787 | 836 | result.loc[idx_values[:2], "A"] = ["qux", "qux2"] |
788 | 837 | expected = DataFrame({"A": ["qux", "qux2", "baz"]}, index=cat_idx) |
789 | 838 | tm.assert_frame_equal(result, expected) |
0 commit comments