|  | 
| 10 | 10 |     Categorical, | 
| 11 | 11 |     DatetimeIndex, | 
| 12 | 12 |     Index, | 
|  | 13 | +    IntervalIndex, | 
| 13 | 14 |     MultiIndex, | 
| 14 | 15 |     NaT, | 
| 15 | 16 |     Series, | 
| @@ -906,3 +907,41 @@ def val(self): | 
| 906 | 907 |     def is_inplace(self, obj): | 
| 907 | 908 |         # This is specific to the 4 cases currently implemented for this class. | 
| 908 | 909 |         return obj.dtype.kind != "i" | 
|  | 910 | + | 
|  | 911 | + | 
|  | 912 | +def test_setitem_int_as_positional_fallback_deprecation(): | 
|  | 913 | +    # GH#42215 deprecated falling back to positional on __setitem__ with an | 
|  | 914 | +    #  int not contained in the index | 
|  | 915 | +    ser = Series([1, 2, 3, 4], index=[1.1, 2.1, 3.0, 4.1]) | 
|  | 916 | +    assert not ser.index._should_fallback_to_positional | 
|  | 917 | +    # assert not ser.index.astype(object)._should_fallback_to_positional | 
|  | 918 | + | 
|  | 919 | +    with tm.assert_produces_warning(None): | 
|  | 920 | +        # 3.0 is in our index, so future behavior is unchanged | 
|  | 921 | +        ser[3] = 10 | 
|  | 922 | +    expected = Series([1, 2, 10, 4], index=ser.index) | 
|  | 923 | +    tm.assert_series_equal(ser, expected) | 
|  | 924 | + | 
|  | 925 | +    msg = "Treating integers as positional in Series.__setitem__" | 
|  | 926 | +    with tm.assert_produces_warning(FutureWarning, match=msg): | 
|  | 927 | +        with pytest.raises(IndexError, match="index 5 is out of bounds"): | 
|  | 928 | +            ser[5] = 5 | 
|  | 929 | +    # Once the deprecation is enforced, we will have | 
|  | 930 | +    #  expected = Series([1, 2, 3, 4, 5], index=[1.1, 2.1, 3.0, 4.1, 5.0]) | 
|  | 931 | + | 
|  | 932 | +    ii = IntervalIndex.from_breaks(range(10))[::2] | 
|  | 933 | +    ser2 = Series(range(len(ii)), index=ii) | 
|  | 934 | +    expected2 = ser2.copy() | 
|  | 935 | +    expected2.iloc[-1] = 9 | 
|  | 936 | +    with tm.assert_produces_warning(FutureWarning, match=msg): | 
|  | 937 | +        ser2[4] = 9 | 
|  | 938 | +    tm.assert_series_equal(ser2, expected2) | 
|  | 939 | + | 
|  | 940 | +    mi = MultiIndex.from_product([ser.index, ["A", "B"]]) | 
|  | 941 | +    ser3 = Series(range(len(mi)), index=mi) | 
|  | 942 | +    expected3 = ser3.copy() | 
|  | 943 | +    expected3.iloc[4] = 99 | 
|  | 944 | + | 
|  | 945 | +    with tm.assert_produces_warning(FutureWarning, match=msg): | 
|  | 946 | +        ser3[4] = 99 | 
|  | 947 | +    tm.assert_series_equal(ser3, expected3) | 
0 commit comments