diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index d10eac2722e15..871e47ea3ce8d 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -98,6 +98,8 @@ Deprecations ~~~~~~~~~~~~ - Deprecated :meth:`Index.is_type_compatible` (:issue:`42113`) - Deprecated ``method`` argument in :meth:`Index.get_loc`, use ``index.get_indexer([label], method=...)`` instead (:issue:`42269`) +- Deprecated treating integer keys in :meth:`Series.__setitem__` as positional when the index is a :class:`Float64Index` not containing the key, a :class:`IntervalIndex` with no entries containing the key, or a :class:`MultiIndex` with leading :class:`Float64Index` level not containing the key (:issue:`33469`) +- .. --------------------------------------------------------------------------- diff --git a/pandas/core/series.py b/pandas/core/series.py index c79ecd554bed5..12e7b7cae4983 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -1068,6 +1068,17 @@ def __setitem__(self, key, value) -> None: values = self._values if is_integer(key) and self.index.inferred_type != "integer": # positional setter + if not self.index._should_fallback_to_positional: + # GH#33469 + warnings.warn( + "Treating integers as positional in Series.__setitem__ " + "with a Float64Index is deprecated. In a future version, " + "`series[an_int] = val` will insert a new key into the " + "Series. Use `series.iloc[an_int] = val` to treat the " + "key as positional.", + FutureWarning, + stacklevel=2, + ) values[key] = value else: # GH#12862 adding a new key to the Series diff --git a/pandas/tests/indexing/test_coercion.py b/pandas/tests/indexing/test_coercion.py index 7911cd7f12e0c..761e67bedbf8c 100644 --- a/pandas/tests/indexing/test_coercion.py +++ b/pandas/tests/indexing/test_coercion.py @@ -273,7 +273,12 @@ def _assert_setitem_index_conversion( ): """test index's coercion triggered by assign key""" temp = original_series.copy() - temp[loc_key] = 5 + warn = None + if isinstance(loc_key, int) and temp.index.dtype == np.float64: + # GH#33469 + warn = FutureWarning + with tm.assert_produces_warning(warn): + temp[loc_key] = 5 exp = pd.Series([1, 2, 3, 4, 5], index=expected_index) tm.assert_series_equal(temp, exp) # check dtype explicitly for sure @@ -324,7 +329,10 @@ def test_setitem_index_float64(self, val, exp_dtype, request): temp = obj.copy() msg = "index 5 is out of bounds for axis 0 with size 4" with pytest.raises(exp_dtype, match=msg): - temp[5] = 5 + # GH#33469 + depr_msg = "Treating integers as positional" + with tm.assert_produces_warning(FutureWarning, match=depr_msg): + temp[5] = 5 mark = pytest.mark.xfail(reason="TODO_GH12747 The result must be float") request.node.add_marker(mark) exp_index = pd.Index([1.1, 2.1, 3.1, 4.1, val]) diff --git a/pandas/tests/series/indexing/test_setitem.py b/pandas/tests/series/indexing/test_setitem.py index 13054062defb4..cfb617cd7098b 100644 --- a/pandas/tests/series/indexing/test_setitem.py +++ b/pandas/tests/series/indexing/test_setitem.py @@ -10,6 +10,7 @@ Categorical, DatetimeIndex, Index, + IntervalIndex, MultiIndex, NaT, Series, @@ -906,3 +907,41 @@ def val(self): def is_inplace(self, obj): # This is specific to the 4 cases currently implemented for this class. return obj.dtype.kind != "i" + + +def test_setitem_int_as_positional_fallback_deprecation(): + # GH#42215 deprecated falling back to positional on __setitem__ with an + # int not contained in the index + ser = Series([1, 2, 3, 4], index=[1.1, 2.1, 3.0, 4.1]) + assert not ser.index._should_fallback_to_positional + # assert not ser.index.astype(object)._should_fallback_to_positional + + with tm.assert_produces_warning(None): + # 3.0 is in our index, so future behavior is unchanged + ser[3] = 10 + expected = Series([1, 2, 10, 4], index=ser.index) + tm.assert_series_equal(ser, expected) + + msg = "Treating integers as positional in Series.__setitem__" + with tm.assert_produces_warning(FutureWarning, match=msg): + with pytest.raises(IndexError, match="index 5 is out of bounds"): + ser[5] = 5 + # Once the deprecation is enforced, we will have + # expected = Series([1, 2, 3, 4, 5], index=[1.1, 2.1, 3.0, 4.1, 5.0]) + + ii = IntervalIndex.from_breaks(range(10))[::2] + ser2 = Series(range(len(ii)), index=ii) + expected2 = ser2.copy() + expected2.iloc[-1] = 9 + with tm.assert_produces_warning(FutureWarning, match=msg): + ser2[4] = 9 + tm.assert_series_equal(ser2, expected2) + + mi = MultiIndex.from_product([ser.index, ["A", "B"]]) + ser3 = Series(range(len(mi)), index=mi) + expected3 = ser3.copy() + expected3.iloc[4] = 99 + + with tm.assert_produces_warning(FutureWarning, match=msg): + ser3[4] = 99 + tm.assert_series_equal(ser3, expected3) diff --git a/pandas/tests/series/test_arithmetic.py b/pandas/tests/series/test_arithmetic.py index aac26c13c2a7c..6b7c2c698c211 100644 --- a/pandas/tests/series/test_arithmetic.py +++ b/pandas/tests/series/test_arithmetic.py @@ -874,7 +874,7 @@ def test_none_comparison(series_with_simple_index): # bug brought up by #1079 # changed from TypeError in 0.17.0 - series[0] = np.nan + series.iloc[0] = np.nan # noinspection PyComparisonWithNone result = series == None # noqa