From 1692ed4d1eb0031a2b0e232eb6c69502b2bf457c Mon Sep 17 00:00:00 2001 From: Brock Date: Fri, 12 May 2023 09:51:30 -0700 Subject: [PATCH 1/8] DEPR: positional indexing on Series __getitem__/__setitem__ --- doc/source/whatsnew/v2.1.0.rst | 2 +- pandas/core/apply.py | 2 +- pandas/core/indexing.py | 2 +- pandas/core/series.py | 36 +++++++++++++++ pandas/io/stata.py | 2 +- pandas/tests/apply/test_frame_apply.py | 2 +- pandas/tests/copy_view/test_indexing.py | 12 ++++- pandas/tests/copy_view/test_interp_fillna.py | 4 +- pandas/tests/extension/base/getitem.py | 8 ++-- pandas/tests/frame/indexing/test_indexing.py | 2 +- pandas/tests/frame/methods/test_map.py | 2 +- pandas/tests/frame/methods/test_replace.py | 6 +-- pandas/tests/frame/methods/test_values.py | 4 +- pandas/tests/frame/test_reductions.py | 8 ++-- pandas/tests/frame/test_stack_unstack.py | 2 +- pandas/tests/groupby/test_groupby.py | 8 ++-- pandas/tests/indexes/datetimes/test_misc.py | 4 +- .../indexes/datetimes/test_partial_slicing.py | 2 +- .../indexes/period/test_partial_slicing.py | 8 ++-- pandas/tests/indexing/test_coercion.py | 4 +- pandas/tests/indexing/test_floats.py | 9 +++- pandas/tests/indexing/test_iloc.py | 3 +- pandas/tests/indexing/test_indexing.py | 8 ++++ pandas/tests/indexing/test_loc.py | 4 +- pandas/tests/io/pytables/test_round_trip.py | 2 +- pandas/tests/io/test_clipboard.py | 2 +- pandas/tests/io/test_sql.py | 2 +- pandas/tests/io/test_stata.py | 2 +- pandas/tests/plotting/test_datetimelike.py | 16 +++---- pandas/tests/resample/test_datetime_index.py | 30 ++++++------ pandas/tests/resample/test_period_index.py | 4 +- pandas/tests/resample/test_timedelta.py | 2 +- .../series/accessors/test_dt_accessor.py | 2 +- pandas/tests/series/indexing/test_datetime.py | 37 ++++++++------- pandas/tests/series/indexing/test_get.py | 34 ++++++++++---- pandas/tests/series/indexing/test_getitem.py | 46 +++++++++++++------ pandas/tests/series/indexing/test_indexing.py | 33 +++++++------ pandas/tests/series/indexing/test_setitem.py | 13 ++++-- pandas/tests/series/methods/test_asof.py | 16 +++---- pandas/tests/series/methods/test_fillna.py | 8 ++-- pandas/tests/series/methods/test_map.py | 2 +- .../tests/series/methods/test_reset_index.py | 4 +- pandas/tests/series/methods/test_values.py | 2 +- pandas/tests/series/test_arithmetic.py | 4 +- pandas/tests/series/test_iteration.py | 4 +- pandas/tests/strings/test_find_replace.py | 10 ++-- pandas/tests/tools/test_to_datetime.py | 6 +-- pandas/tests/window/test_apply.py | 2 +- pandas/tests/window/test_pairwise.py | 6 +-- pandas/tests/window/test_rolling.py | 2 +- pandas/tests/window/test_rolling_functions.py | 2 +- pandas/tests/window/test_rolling_quantile.py | 2 +- pandas/tests/window/test_rolling_skew_kurt.py | 2 +- 53 files changed, 279 insertions(+), 162 deletions(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 63e212e40e9a3..58d7192fc941a 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -263,7 +263,7 @@ Deprecations - Deprecated allowing ``downcast`` keyword other than ``None``, ``False``, "infer", or a dict with these as values in :meth:`Series.fillna`, :meth:`DataFrame.fillna` (:issue:`40988`) - Deprecated allowing arbitrary ``fill_value`` in :class:`SparseDtype`, in a future version the ``fill_value`` will need to be compatible with the ``dtype.subtype``, either a scalar that can be held by that subtype or ``NaN`` for integer or bool subtypes (:issue:`23124`) - Deprecated constructing :class:`SparseArray` from scalar data, pass a sequence instead (:issue:`53039`) -- +- Deprecated positional indexing on :class:`Series` with :meth:`Series.__getitem__` and :meth:`Series.__setitem__`, in a future version ``ser[item]`` will *always* interpret ``item`` as a label, not a position (:issue:`50617`) .. --------------------------------------------------------------------------- .. _whatsnew_210.performance: diff --git a/pandas/core/apply.py b/pandas/core/apply.py index 0c2adb89a2422..503640c23c4ae 100644 --- a/pandas/core/apply.py +++ b/pandas/core/apply.py @@ -1437,7 +1437,7 @@ def relabel_result( com.get_callable_name(f) if not isinstance(f, str) else f for f in fun ] col_idx_order = Index(s.index).get_indexer(fun) - s = s[col_idx_order] + s = s.iloc[col_idx_order] # assign the new user-provided "named aggregation" as index names, and reindex # it based on the whole user-provided names. diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index 6080c55de8a86..0a1a9deb1c04f 100644 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -1885,7 +1885,7 @@ def _setitem_with_indexer_split_path(self, indexer, value, name: str): pass elif self._is_scalar_access(indexer) and is_object_dtype( - self.obj.dtypes[ilocs[0]] + self.obj.dtypes._values[ilocs[0]] ): # We are setting nested data, only possible for object dtype data self._setitem_single_column(indexer[1], value, pi) diff --git a/pandas/core/series.py b/pandas/core/series.py index 84fa874831d85..829cc86952e27 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -967,6 +967,15 @@ def __getitem__(self, key): key = unpack_1tuple(key) if is_integer(key) and self.index._should_fallback_to_positional: + warnings.warn( + # GH#50617 + "Series.__getitem__ treating keys as positions is deprecated. " + "In a future version, integer keys will always be treated " + "as labels (consistent with DataFrame behavior). To access " + "a value by position, use `ser.iloc[pos]`", + FutureWarning, + stacklevel=find_stack_level(), + ) return self._values[key] elif key_is_scalar: @@ -1029,6 +1038,15 @@ def _get_with(self, key): if not self.index._should_fallback_to_positional: return self.loc[key] else: + warnings.warn( + # GH#50617 + "Series.__getitem__ treating keys as positions is deprecated. " + "In a future version, integer keys will always be treated " + "as labels (consistent with DataFrame behavior). To access " + "a value by position, use `ser.iloc[pos]`", + FutureWarning, + stacklevel=find_stack_level(), + ) return self.iloc[key] # handle the dup indexing case GH#4246 @@ -1130,6 +1148,15 @@ def __setitem__(self, key, value) -> None: # positional setter # can't use _mgr.setitem_inplace yet bc could have *both* # KeyError and then ValueError, xref GH#45070 + warnings.warn( + # GH#50617 + "Series.__setitem__ treating keys as positions is deprecated. " + "In a future version, integer keys will always be treated " + "as labels (consistent with DataFrame behavior). To set " + "a value by position, use `ser.iloc[pos] = value`", + FutureWarning, + stacklevel=find_stack_level(), + ) self._set_values(key, value) else: # GH#12862 adding a new key to the Series @@ -1205,6 +1232,15 @@ def _set_with(self, key, value) -> None: key_type = lib.infer_dtype(key, skipna=False) if key_type == "integer": + warnings.warn( + # GH#50617 + "Series.__setitem__ treating keys as positions is deprecated. " + "In a future version, integer keys will always be treated " + "as labels (consistent with DataFrame behavior). To set " + "a value by position, use `ser.iloc[pos] = value`", + FutureWarning, + stacklevel=find_stack_level(), + ) self._set_values(key, value) else: self._set_labels(key, value) diff --git a/pandas/io/stata.py b/pandas/io/stata.py index aed28efecb696..d8dcbfd8375f1 100644 --- a/pandas/io/stata.py +++ b/pandas/io/stata.py @@ -2624,7 +2624,7 @@ def _prepare_pandas(self, data: DataFrame) -> None: ) for key in self._convert_dates: new_type = _convert_datetime_to_stata_type(self._convert_dates[key]) - dtypes[key] = np.dtype(new_type) + dtypes.iloc[key] = np.dtype(new_type) # Verify object arrays are strings and encode to bytes self._encode_strings() diff --git a/pandas/tests/apply/test_frame_apply.py b/pandas/tests/apply/test_frame_apply.py index 5a2574e62b41e..80292ac6641ac 100644 --- a/pandas/tests/apply/test_frame_apply.py +++ b/pandas/tests/apply/test_frame_apply.py @@ -1385,7 +1385,7 @@ def sum_div2(s): def test_apply_getitem_axis_1(): # GH 13427 df = DataFrame({"a": [0, 1, 2], "b": [1, 2, 3]}) - result = df[["a", "a"]].apply(lambda x: x[0] + x[1], axis=1) + result = df[["a", "a"]].apply(lambda x: x.iloc[0] + x.iloc[1], axis=1) expected = Series([0, 2, 4]) tm.assert_series_equal(result, expected) diff --git a/pandas/tests/copy_view/test_indexing.py b/pandas/tests/copy_view/test_indexing.py index 8e211c60ee654..f9b4ba295f3a0 100644 --- a/pandas/tests/copy_view/test_indexing.py +++ b/pandas/tests/copy_view/test_indexing.py @@ -802,7 +802,17 @@ def test_series_subset_set_with_indexer( s_orig = s.copy() subset = s[:] - indexer_si(subset)[indexer] = 0 + warn = None + msg = "Series.__setitem__ treating keys as positions is deprecated" + if ( + indexer_si is tm.setitem + and isinstance(indexer, np.ndarray) + and indexer.dtype.kind == "i" + ): + warn = FutureWarning + + with tm.assert_produces_warning(warn, match=msg): + indexer_si(subset)[indexer] = 0 expected = Series([0, 0, 3], index=["a", "b", "c"]) tm.assert_series_equal(subset, expected) diff --git a/pandas/tests/copy_view/test_interp_fillna.py b/pandas/tests/copy_view/test_interp_fillna.py index 91c6b96767142..576d3a9cdedde 100644 --- a/pandas/tests/copy_view/test_interp_fillna.py +++ b/pandas/tests/copy_view/test_interp_fillna.py @@ -287,7 +287,7 @@ def test_fillna_ea_noop_shares_memory( if using_copy_on_write: assert np.shares_memory(get_array(df, "b"), get_array(df2, "b")) assert not df2._mgr._has_no_reference(1) - elif isinstance(df.dtypes[0], ArrowDtype): + elif isinstance(df.dtypes.iloc[0], ArrowDtype): # arrow is immutable, so no-ops do not need to copy underlying array assert np.shares_memory(get_array(df, "b"), get_array(df2, "b")) else: @@ -317,7 +317,7 @@ def test_fillna_inplace_ea_noop_shares_memory( assert np.shares_memory(get_array(df, "b"), get_array(view, "b")) assert not df._mgr._has_no_reference(1) assert not view._mgr._has_no_reference(1) - elif isinstance(df.dtypes[0], ArrowDtype): + elif isinstance(df.dtypes.iloc[0], ArrowDtype): # arrow is immutable, so no-ops do not need to copy underlying array assert np.shares_memory(get_array(df, "b"), get_array(view, "b")) else: diff --git a/pandas/tests/extension/base/getitem.py b/pandas/tests/extension/base/getitem.py index cf51d9d693155..26fd5a0aa0fa4 100644 --- a/pandas/tests/extension/base/getitem.py +++ b/pandas/tests/extension/base/getitem.py @@ -330,9 +330,11 @@ def test_get(self, data): result = s.get("Z") assert result is None - assert s.get(4) == s.iloc[4] - assert s.get(-1) == s.iloc[-1] - assert s.get(len(s)) is None + msg = "Series.__getitem__ treating keys as positions is deprecated" + with tm.assert_produces_warning(FutureWarning, match=msg): + assert s.get(4) == s.iloc[4] + assert s.get(-1) == s.iloc[-1] + assert s.get(len(s)) is None # GH 21257 s = pd.Series(data) diff --git a/pandas/tests/frame/indexing/test_indexing.py b/pandas/tests/frame/indexing/test_indexing.py index 224abbcef27df..6ac1faad060ef 100644 --- a/pandas/tests/frame/indexing/test_indexing.py +++ b/pandas/tests/frame/indexing/test_indexing.py @@ -454,7 +454,7 @@ def test_setitem_corner(self, float_frame): # set existing column dm["A"] = "bar" - assert "bar" == dm["A"][0] + assert "bar" == dm["A"].iloc[0] dm = DataFrame(index=np.arange(3)) dm["A"] = 1 diff --git a/pandas/tests/frame/methods/test_map.py b/pandas/tests/frame/methods/test_map.py index 53cae2d079217..7ef51ebc4b6a3 100644 --- a/pandas/tests/frame/methods/test_map.py +++ b/pandas/tests/frame/methods/test_map.py @@ -19,7 +19,7 @@ def test_map(float_frame): float_frame.map(type) # GH 465: function returning tuples - result = float_frame.map(lambda x: (x, x))["A"][0] + result = float_frame.map(lambda x: (x, x))["A"].iloc[0] assert isinstance(result, tuple) diff --git a/pandas/tests/frame/methods/test_replace.py b/pandas/tests/frame/methods/test_replace.py index ad89ccd6e3c60..1619b79f64514 100644 --- a/pandas/tests/frame/methods/test_replace.py +++ b/pandas/tests/frame/methods/test_replace.py @@ -614,8 +614,8 @@ def test_replace_mixed3(self): result = df.replace(3, df.mean().to_dict()) expected = df.copy().astype("float64") m = df.mean() - expected.iloc[0, 0] = m[0] - expected.iloc[1, 1] = m[1] + expected.iloc[0, 0] = m.iloc[0] + expected.iloc[1, 1] = m.iloc[1] tm.assert_frame_equal(result, expected) def test_replace_nullable_int_with_string_doesnt_cast(self): @@ -1072,7 +1072,7 @@ def test_replace_period(self): assert set(df.fname.values) == set(d["fname"].keys()) expected = DataFrame({"fname": [d["fname"][k] for k in df.fname.values]}) - assert expected.dtypes[0] == "Period[M]" + assert expected.dtypes.iloc[0] == "Period[M]" result = df.replace(d) tm.assert_frame_equal(result, expected) diff --git a/pandas/tests/frame/methods/test_values.py b/pandas/tests/frame/methods/test_values.py index 5728a849262ee..8c2253820db31 100644 --- a/pandas/tests/frame/methods/test_values.py +++ b/pandas/tests/frame/methods/test_values.py @@ -38,9 +38,9 @@ def test_values_mixed_dtypes(self, float_frame, float_string_frame): for j, value in enumerate(row): col = frame_cols[j] if np.isnan(value): - assert np.isnan(frame[col][i]) + assert np.isnan(frame[col].iloc[i]) else: - assert value == frame[col][i] + assert value == frame[col].iloc[i] # mixed type arr = float_string_frame[["foo", "A"]].values diff --git a/pandas/tests/frame/test_reductions.py b/pandas/tests/frame/test_reductions.py index 096f6fe83ea88..dd8a71f6c10b1 100644 --- a/pandas/tests/frame/test_reductions.py +++ b/pandas/tests/frame/test_reductions.py @@ -611,16 +611,16 @@ def test_operators_timedelta64(self): # min result = diffs.min() - assert result[0] == diffs.loc[0, "A"] - assert result[1] == diffs.loc[0, "B"] + assert result.iloc[0] == diffs.loc[0, "A"] + assert result.iloc[1] == diffs.loc[0, "B"] result = diffs.min(axis=1) assert (result == diffs.loc[0, "B"]).all() # max result = diffs.max() - assert result[0] == diffs.loc[2, "A"] - assert result[1] == diffs.loc[2, "B"] + assert result.iloc[0] == diffs.loc[2, "A"] + assert result.iloc[1] == diffs.loc[2, "B"] result = diffs.max(axis=1) assert (result == diffs["A"]).all() diff --git a/pandas/tests/frame/test_stack_unstack.py b/pandas/tests/frame/test_stack_unstack.py index 889c44522f7bb..bca4675e6b3e0 100644 --- a/pandas/tests/frame/test_stack_unstack.py +++ b/pandas/tests/frame/test_stack_unstack.py @@ -1075,7 +1075,7 @@ def test_stack_full_multiIndex(self): ), columns=Index(["B", "C"], name="Upper"), ) - expected["B"] = expected["B"].astype(df.dtypes[0]) + expected["B"] = expected["B"].astype(df.dtypes.iloc[0]) tm.assert_frame_equal(result, expected) @pytest.mark.parametrize("ordered", [False, True]) diff --git a/pandas/tests/groupby/test_groupby.py b/pandas/tests/groupby/test_groupby.py index 7bda7c575d994..3236b165e9444 100644 --- a/pandas/tests/groupby/test_groupby.py +++ b/pandas/tests/groupby/test_groupby.py @@ -1344,11 +1344,11 @@ def convert_force_pure(x): result = grouped.agg(convert_fast) assert result.dtype == np.object_ - assert isinstance(result[0], Decimal) + assert isinstance(result.iloc[0], Decimal) result = grouped.agg(convert_force_pure) assert result.dtype == np.object_ - assert isinstance(result[0], Decimal) + assert isinstance(result.iloc[0], Decimal) def test_groupby_dtype_inference_empty(): @@ -1967,8 +1967,8 @@ def get_categorical_invalid_expected(): expected = DataFrame([], columns=[], index=idx) return expected - is_per = isinstance(df.dtypes[0], pd.PeriodDtype) - is_dt64 = df.dtypes[0].kind == "M" + is_per = isinstance(df.dtypes.iloc[0], pd.PeriodDtype) + is_dt64 = df.dtypes.iloc[0].kind == "M" is_cat = isinstance(values, Categorical) if isinstance(values, Categorical) and not values.ordered and op in ["min", "max"]: diff --git a/pandas/tests/indexes/datetimes/test_misc.py b/pandas/tests/indexes/datetimes/test_misc.py index f13dfcd5c20bd..139190962895d 100644 --- a/pandas/tests/indexes/datetimes/test_misc.py +++ b/pandas/tests/indexes/datetimes/test_misc.py @@ -50,8 +50,8 @@ def test_datetimeindex_accessors(self): assert dti.dayofyear[0] == 1 assert dti.dayofyear[120] == 121 - assert dti.isocalendar().week[0] == 1 - assert dti.isocalendar().week[120] == 18 + assert dti.isocalendar().week.iloc[0] == 1 + assert dti.isocalendar().week.iloc[120] == 18 assert dti.quarter[0] == 1 assert dti.quarter[120] == 2 diff --git a/pandas/tests/indexes/datetimes/test_partial_slicing.py b/pandas/tests/indexes/datetimes/test_partial_slicing.py index f030f4101512e..28c9c07a9c9ef 100644 --- a/pandas/tests/indexes/datetimes/test_partial_slicing.py +++ b/pandas/tests/indexes/datetimes/test_partial_slicing.py @@ -379,7 +379,7 @@ def test_partial_slice_requires_monotonicity(self): # Disallowed since 2.0 (GH 37819) ser = Series(np.arange(10), date_range("2014-01-01", periods=10)) - nonmonotonic = ser[[3, 5, 4]] + nonmonotonic = ser.iloc[[3, 5, 4]] timestamp = Timestamp("2014-01-10") with pytest.raises( KeyError, match="Value based partial slicing on non-monotonic" diff --git a/pandas/tests/indexes/period/test_partial_slicing.py b/pandas/tests/indexes/period/test_partial_slicing.py index 1f704cfa064d8..bf81d2cde98e7 100644 --- a/pandas/tests/indexes/period/test_partial_slicing.py +++ b/pandas/tests/indexes/period/test_partial_slicing.py @@ -39,7 +39,7 @@ def test_getitem_periodindex_quarter_string(self): pi = PeriodIndex(["2Q05", "3Q05", "4Q05", "1Q06", "2Q06"], freq="Q") ser = Series(np.random.rand(len(pi)), index=pi).cumsum() # Todo: fix these accessors! - assert ser["05Q4"] == ser[2] + assert ser["05Q4"] == ser.iloc[2] def test_pindex_slice_index(self): pi = period_range(start="1/1/10", end="12/31/12", freq="M") @@ -160,7 +160,7 @@ def test_partial_slice_doesnt_require_monotonicity(self): ser_montonic = Series(np.arange(30), index=pi) shuffler = list(range(0, 30, 2)) + list(range(1, 31, 2)) - ser = ser_montonic[shuffler] + ser = ser_montonic.iloc[shuffler] nidx = ser.index # Manually identified locations of year==2014 @@ -173,7 +173,7 @@ def test_partial_slice_doesnt_require_monotonicity(self): result = nidx.get_loc("2014") tm.assert_numpy_array_equal(result, indexer_2014) - expected = ser[indexer_2014] + expected = ser.iloc[indexer_2014] result = ser.loc["2014"] tm.assert_series_equal(result, expected) @@ -187,7 +187,7 @@ def test_partial_slice_doesnt_require_monotonicity(self): result = nidx.get_loc("May 2015") tm.assert_numpy_array_equal(result, indexer_may2015) - expected = ser[indexer_may2015] + expected = ser.iloc[indexer_may2015] result = ser.loc["May 2015"] tm.assert_series_equal(result, expected) diff --git a/pandas/tests/indexing/test_coercion.py b/pandas/tests/indexing/test_coercion.py index 86d6246437ea1..2c39729097487 100644 --- a/pandas/tests/indexing/test_coercion.py +++ b/pandas/tests/indexing/test_coercion.py @@ -116,9 +116,11 @@ def test_setitem_index_object(self, val, exp_dtype): if exp_dtype is IndexError: temp = obj.copy() + warn_msg = "Series.__setitem__ treating keys as positions is deprecated" msg = "index 5 is out of bounds for axis 0 with size 4" with pytest.raises(exp_dtype, match=msg): - temp[5] = 5 + with tm.assert_produces_warning(FutureWarning, match=warn_msg): + temp[5] = 5 else: exp_index = pd.Index(list("abcd") + [val]) self._assert_setitem_index_conversion(obj, val, exp_index, exp_dtype) diff --git a/pandas/tests/indexing/test_floats.py b/pandas/tests/indexing/test_floats.py index 3b622d7b4703a..0bcc2aa75d78a 100644 --- a/pandas/tests/indexing/test_floats.py +++ b/pandas/tests/indexing/test_floats.py @@ -86,7 +86,10 @@ def test_scalar_non_numeric_series_fallback(self, index_func): # fallsback to position selection, series only i = index_func(5) s = Series(np.arange(len(i)), index=i) - s[3] + + msg = "Series.__getitem__ treating keys as positions is deprecated" + with tm.assert_produces_warning(FutureWarning, match=msg): + s[3] with pytest.raises(KeyError, match="^3.0$"): s[3.0] @@ -113,7 +116,9 @@ def test_scalar_with_mixed(self, indexer_sl): if indexer_sl is not tm.loc: # __getitem__ falls back to positional - result = s3[1] + msg = "Series.__getitem__ treating keys as positions is deprecated" + with tm.assert_produces_warning(FutureWarning, match=msg): + result = s3[1] expected = 2 assert result == expected diff --git a/pandas/tests/indexing/test_iloc.py b/pandas/tests/indexing/test_iloc.py index bfa9a6497b3d5..3dec187a54439 100644 --- a/pandas/tests/indexing/test_iloc.py +++ b/pandas/tests/indexing/test_iloc.py @@ -775,7 +775,8 @@ def test_iloc_mask(self): for idx in [None, "index", "locs"]: mask = (df.nums > 2).values if idx: - mask = Series(mask, list(reversed(getattr(df, idx)))) + mask_index = getattr(df, idx)[::-1] + mask = Series(mask, list(mask_index)) for method in ["", ".loc", ".iloc"]: try: if method: diff --git a/pandas/tests/indexing/test_indexing.py b/pandas/tests/indexing/test_indexing.py index b4cb7abbd418f..21036598f46df 100644 --- a/pandas/tests/indexing/test_indexing.py +++ b/pandas/tests/indexing/test_indexing.py @@ -71,6 +71,10 @@ def test_setitem_ndarray_1d_2(self): with pytest.raises(ValueError, match=msg): df[2:5] = np.arange(1, 4) * 1j + @pytest.mark.filterwarnings( + "ignore:Series.__getitem__ treating keys as positions is deprecated:" + "FutureWarning" + ) def test_getitem_ndarray_3d( self, index, frame_or_series, indexer_sli, using_array_manager ): @@ -113,6 +117,10 @@ def test_getitem_ndarray_3d( with pytest.raises(potential_errors, match=msg): idxr[nd3] + @pytest.mark.filterwarnings( + "ignore:Series.__setitem__ treating keys as positions is deprecated:" + "FutureWarning" + ) def test_setitem_ndarray_3d(self, index, frame_or_series, indexer_sli): # GH 25567 obj = gen_obj(frame_or_series, index) diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index 3aab884f06131..19421345087fc 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -3026,7 +3026,7 @@ def test_loc_getitem(self, string_series, datetime_series): def test_loc_getitem_not_monotonic(self, datetime_series): d1, d2 = datetime_series.index[[5, 15]] - ts2 = datetime_series[::2][[1, 2, 0]] + ts2 = datetime_series[::2].iloc[[1, 2, 0]] msg = r"Timestamp\('2000-01-10 00:00:00'\)" with pytest.raises(KeyError, match=msg): @@ -3184,7 +3184,7 @@ def test_loc_setitem(self, string_series): result.loc[inds] = 5 expected = string_series.copy() - expected[[3, 4, 7]] = 5 + expected.iloc[[3, 4, 7]] = 5 tm.assert_series_equal(result, expected) result.iloc[5:10] = 10 diff --git a/pandas/tests/io/pytables/test_round_trip.py b/pandas/tests/io/pytables/test_round_trip.py index 163951a6ebc45..2461f937c9eff 100644 --- a/pandas/tests/io/pytables/test_round_trip.py +++ b/pandas/tests/io/pytables/test_round_trip.py @@ -217,7 +217,7 @@ def test_table_values_dtypes_roundtrip(setup_path): df1 = DataFrame(np.array([[1], [2], [3]], dtype="f4"), columns=["A"]) store.append("df_f4", df1) tm.assert_series_equal(df1.dtypes, store["df_f4"].dtypes) - assert df1.dtypes[0] == "float32" + assert df1.dtypes.iloc[0] == "float32" # check with mixed dtypes df1 = DataFrame( diff --git a/pandas/tests/io/test_clipboard.py b/pandas/tests/io/test_clipboard.py index b5e040e05e8f7..ff81d0125144e 100644 --- a/pandas/tests/io/test_clipboard.py +++ b/pandas/tests/io/test_clipboard.py @@ -316,7 +316,7 @@ def test_read_clipboard_infer_excel(self, request, mock_clipboard): df = read_clipboard(**clip_kwargs) # excel data is parsed correctly - assert df.iloc[1][1] == "Harry Carney" + assert df.iloc[1, 1] == "Harry Carney" # having diff tab counts doesn't trigger it text = dedent( diff --git a/pandas/tests/io/test_sql.py b/pandas/tests/io/test_sql.py index 77e7e6f8d6c41..a864903d9c6d1 100644 --- a/pandas/tests/io/test_sql.py +++ b/pandas/tests/io/test_sql.py @@ -262,7 +262,7 @@ def create_and_load_types(conn, types_data: list[dict], dialect: str): def check_iris_frame(frame: DataFrame): - pytype = frame.dtypes[0].type + pytype = frame.dtypes.iloc[0].type row = frame.iloc[0] assert issubclass(pytype, np.floating) tm.equalContents(row.values, [5.1, 3.5, 1.4, 0.2, "Iris-setosa"]) diff --git a/pandas/tests/io/test_stata.py b/pandas/tests/io/test_stata.py index 05c397c4ea4f1..bb4a9f68f348f 100644 --- a/pandas/tests/io/test_stata.py +++ b/pandas/tests/io/test_stata.py @@ -2056,7 +2056,7 @@ def test_iterator_value_labels(): with read_stata(path, chunksize=100) as reader: for j, chunk in enumerate(reader): for i in range(2): - tm.assert_index_equal(chunk.dtypes[i].categories, expected) + tm.assert_index_equal(chunk.dtypes.iloc[i].categories, expected) tm.assert_frame_equal(chunk, df.iloc[j * 100 : (j + 1) * 100]) diff --git a/pandas/tests/plotting/test_datetimelike.py b/pandas/tests/plotting/test_datetimelike.py index 8fc4170a8562c..dacc0cbea7c9e 100644 --- a/pandas/tests/plotting/test_datetimelike.py +++ b/pandas/tests/plotting/test_datetimelike.py @@ -235,7 +235,7 @@ def test_line_plot_inferred_freq(self, freq): ser = Series(ser.values, Index(np.asarray(ser.index))) _check_plot_works(ser.plot, ser.index.inferred_freq) - ser = ser[[0, 3, 5, 6]] + ser = ser.iloc[[0, 3, 5, 6]] _check_plot_works(ser.plot) def test_fake_inferred_business(self): @@ -299,7 +299,7 @@ def test_irreg_hf(self): def test_irregular_datetime64_repr_bug(self): ser = tm.makeTimeSeries() - ser = ser[[0, 1, 2, 7]] + ser = ser.iloc[[0, 1, 2, 7]] _, ax = self.plt.subplots() @@ -539,7 +539,7 @@ def test_gaps(self): # irregular ts = tm.makeTimeSeries() - ts = ts[[0, 1, 2, 5, 7, 9, 12, 15, 20]] + ts = ts.iloc[[0, 1, 2, 5, 7, 9, 12, 15, 20]] ts.iloc[2:5] = np.nan _, ax = self.plt.subplots() ax = ts.plot(ax=ax) @@ -679,7 +679,7 @@ def test_secondary_bar_frame(self): def test_mixed_freq_regular_first(self): # TODO s1 = tm.makeTimeSeries() - s2 = s1[[0, 5, 10, 11, 12, 13, 14, 15]] + s2 = s1.iloc[[0, 5, 10, 11, 12, 13, 14, 15]] # it works! _, ax = self.plt.subplots() @@ -700,7 +700,7 @@ def test_mixed_freq_regular_first(self): def test_mixed_freq_irregular_first(self): s1 = tm.makeTimeSeries() - s2 = s1[[0, 5, 10, 11, 12, 13, 14, 15]] + s2 = s1.iloc[[0, 5, 10, 11, 12, 13, 14, 15]] _, ax = self.plt.subplots() s2.plot(style="g", ax=ax) s1.plot(ax=ax) @@ -792,7 +792,7 @@ def test_mixed_freq_lf_first(self): def test_mixed_freq_irreg_period(self): ts = tm.makeTimeSeries() - irreg = ts[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 16, 17, 18, 29]] + irreg = ts.iloc[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 16, 17, 18, 29]] rng = period_range("1/3/2000", periods=30, freq="B") ps = Series(np.random.randn(len(rng)), rng) _, ax = self.plt.subplots() @@ -1246,7 +1246,7 @@ def test_irregular_ts_shared_ax_xlim(self): from pandas.plotting._matplotlib.converter import DatetimeConverter ts = tm.makeTimeSeries()[:20] - ts_irregular = ts[[1, 4, 5, 6, 8, 9, 10, 12, 13, 14, 15, 17, 18]] + ts_irregular = ts.iloc[[1, 4, 5, 6, 8, 9, 10, 12, 13, 14, 15, 17, 18]] # plot the left section of the irregular series, then the right section _, ax = self.plt.subplots() @@ -1310,7 +1310,7 @@ def test_secondary_y_irregular_ts_xlim(self): from pandas.plotting._matplotlib.converter import DatetimeConverter ts = tm.makeTimeSeries()[:20] - ts_irregular = ts[[1, 4, 5, 6, 8, 9, 10, 12, 13, 14, 15, 17, 18]] + ts_irregular = ts.iloc[[1, 4, 5, 6, 8, 9, 10, 12, 13, 14, 15, 17, 18]] _, ax = self.plt.subplots() ts_irregular[:5].plot(ax=ax) diff --git a/pandas/tests/resample/test_datetime_index.py b/pandas/tests/resample/test_datetime_index.py index cbf530e995c43..389db2d36474d 100644 --- a/pandas/tests/resample/test_datetime_index.py +++ b/pandas/tests/resample/test_datetime_index.py @@ -111,7 +111,7 @@ def test_custom_grouper_df(index, unit): ( "right", lambda s: Series( - [s[0], s[1:6].mean(), s[6:11].mean(), s[11:].mean()], + [s.iloc[0], s[1:6].mean(), s[6:11].mean(), s[11:].mean()], index=date_range("1/1/2000", periods=4, freq="5min", name="index"), ), ), @@ -164,7 +164,7 @@ def test_resample_basic_grouper(series, unit): s.index = s.index.as_unit(unit) result = s.resample("5Min").last() grouper = Grouper(freq=Minute(5), closed="left", label="left") - expected = s.groupby(grouper).agg(lambda x: x[-1]) + expected = s.groupby(grouper).agg(lambda x: x.iloc[-1]) tm.assert_series_equal(result, expected) @@ -227,7 +227,7 @@ def test_resample_how_ohlc(series, unit): def _ohlc(group): if isna(group).all(): return np.repeat(np.nan, 4) - return [group[0], group.max(), group.min(), group[-1]] + return [group.iloc[0], group.max(), group.min(), group.iloc[-1]] expected = DataFrame( s.groupby(grouplist).agg(_ohlc).values.tolist(), @@ -460,8 +460,8 @@ def test_resample_upsample(unit): # to minutely, by padding result = s.resample("Min").ffill() assert len(result) == 12961 - assert result[0] == s[0] - assert result[-1] == s[-1] + assert result.iloc[0] == s.iloc[0] + assert result.iloc[-1] == s.iloc[-1] assert result.index.name == "index" @@ -534,23 +534,23 @@ def test_resample_ohlc(series, unit): s.index = s.index.as_unit(unit) grouper = Grouper(freq=Minute(5)) - expect = s.groupby(grouper).agg(lambda x: x[-1]) + expect = s.groupby(grouper).agg(lambda x: x.iloc[-1]) result = s.resample("5Min").ohlc() assert len(result) == len(expect) assert len(result.columns) == 4 xs = result.iloc[-2] - assert xs["open"] == s[-6] + assert xs["open"] == s.iloc[-6] assert xs["high"] == s[-6:-1].max() assert xs["low"] == s[-6:-1].min() - assert xs["close"] == s[-2] + assert xs["close"] == s.iloc[-2] xs = result.iloc[0] - assert xs["open"] == s[0] + assert xs["open"] == s.iloc[0] assert xs["high"] == s[:5].max() assert xs["low"] == s[:5].min() - assert xs["close"] == s[4] + assert xs["close"] == s.iloc[4] def test_resample_ohlc_result(unit): @@ -688,14 +688,14 @@ def test_ohlc_5min(unit): def _ohlc(group): if isna(group).all(): return np.repeat(np.nan, 4) - return [group[0], group.max(), group.min(), group[-1]] + return [group.iloc[0], group.max(), group.min(), group.iloc[-1]] rng = date_range("1/1/2000 00:00:00", "1/1/2000 5:59:50", freq="10s").as_unit(unit) ts = Series(np.random.randn(len(rng)), index=rng) resampled = ts.resample("5min", closed="right", label="right").ohlc() - assert (resampled.loc["1/1/2000 00:00"] == ts[0]).all() + assert (resampled.loc["1/1/2000 00:00"] == ts.iloc[0]).all() exp = _ohlc(ts[1:31]) assert (resampled.loc["1/1/2000 00:05"] == exp).all() @@ -713,8 +713,8 @@ def test_downsample_non_unique(unit): expected = ts.groupby(lambda x: x.month).mean() assert len(result) == 2 - tm.assert_almost_equal(result[0], expected[1]) - tm.assert_almost_equal(result[1], expected[2]) + tm.assert_almost_equal(result.iloc[0], expected[1]) + tm.assert_almost_equal(result.iloc[1], expected[2]) def test_asfreq_non_unique(unit): @@ -1318,7 +1318,7 @@ def test_resample_consistency(unit): i30 = date_range("2002-02-02", periods=4, freq="30T").as_unit(unit) s = Series(np.arange(4.0), index=i30) - s[2] = np.NaN + s.iloc[2] = np.NaN # Upsample by factor 3 with reindex() and resample() methods: i10 = date_range(i30[0], i30[-1], freq="10T").as_unit(unit) diff --git a/pandas/tests/resample/test_period_index.py b/pandas/tests/resample/test_period_index.py index 17277b0c74568..9c3ccd96a8d59 100644 --- a/pandas/tests/resample/test_period_index.py +++ b/pandas/tests/resample/test_period_index.py @@ -535,7 +535,7 @@ def test_closed_left_corner(self): np.random.randn(21), index=date_range(start="1/1/2012 9:30", freq="1min", periods=21), ) - s[0] = np.nan + s.iloc[0] = np.nan result = s.resample("10min", closed="left", label="right").mean() exp = s[1:].resample("10min", closed="left", label="right").mean() @@ -657,7 +657,7 @@ def test_all_values_single_bin(self): s = Series(np.random.randn(len(index)), index=index) result = s.resample("A").mean() - tm.assert_almost_equal(result[0], s.mean()) + tm.assert_almost_equal(result.iloc[0], s.mean()) def test_evenly_divisible_with_no_extra_bins(self): # 4076 diff --git a/pandas/tests/resample/test_timedelta.py b/pandas/tests/resample/test_timedelta.py index 8b6e757c0a46a..79e024ef84c90 100644 --- a/pandas/tests/resample/test_timedelta.py +++ b/pandas/tests/resample/test_timedelta.py @@ -147,7 +147,7 @@ def test_resample_timedelta_edge_case(start, end, freq, resample_freq): expected_index = timedelta_range(freq=resample_freq, start=start, end=end) tm.assert_index_equal(result.index, expected_index) assert result.index.freq == expected_index.freq - assert not np.isnan(result[-1]) + assert not np.isnan(result.iloc[-1]) @pytest.mark.parametrize("duplicates", [True, False]) diff --git a/pandas/tests/series/accessors/test_dt_accessor.py b/pandas/tests/series/accessors/test_dt_accessor.py index 330d0f6a89c71..5a715fcda97f8 100644 --- a/pandas/tests/series/accessors/test_dt_accessor.py +++ b/pandas/tests/series/accessors/test_dt_accessor.py @@ -243,7 +243,7 @@ def test_dt_namespace_accessor_index_and_values(self): exp = Series(np.array([0, 1, 2], dtype="int32"), index=index, name="xxx") tm.assert_series_equal(ser.dt.second, exp) - exp = Series([ser[0]] * 3, index=index, name="xxx") + exp = Series([ser.iloc[0]] * 3, index=index, name="xxx") tm.assert_series_equal(ser.dt.normalize(), exp) def test_dt_accessor_limited_display_api(self): diff --git a/pandas/tests/series/indexing/test_datetime.py b/pandas/tests/series/indexing/test_datetime.py index 64da669343147..f47e344336a8b 100644 --- a/pandas/tests/series/indexing/test_datetime.py +++ b/pandas/tests/series/indexing/test_datetime.py @@ -35,7 +35,9 @@ def test_fancy_getitem(): s = Series(np.arange(len(dti)), index=dti) - assert s[48] == 48 + msg = "Series.__getitem__ treating keys as positions is deprecated" + with tm.assert_produces_warning(FutureWarning, match=msg): + assert s[48] == 48 assert s["1/2/2009"] == 48 assert s["2009-1-2"] == 48 assert s[datetime(2009, 1, 2)] == 48 @@ -53,10 +55,13 @@ def test_fancy_setitem(): ) s = Series(np.arange(len(dti)), index=dti) - s[48] = -1 - assert s[48] == -1 + + msg = "Series.__setitem__ treating keys as positions is deprecated" + with tm.assert_produces_warning(FutureWarning, match=msg): + s[48] = -1 + assert s.iloc[48] == -1 s["1/2/2009"] = -2 - assert s[48] == -2 + assert s.iloc[48] == -2 s["1/2/2009":"2009-06-05"] = -3 assert (s[48:54] == -3).all() @@ -77,25 +82,25 @@ def test_getitem_setitem_datetime_tz(tz_source): # also test Timestamp tz handling, GH #2789 result = ts.copy() result["1990-01-01 09:00:00+00:00"] = 0 - result["1990-01-01 09:00:00+00:00"] = ts[4] + result["1990-01-01 09:00:00+00:00"] = ts.iloc[4] tm.assert_series_equal(result, ts) result = ts.copy() result["1990-01-01 03:00:00-06:00"] = 0 - result["1990-01-01 03:00:00-06:00"] = ts[4] + result["1990-01-01 03:00:00-06:00"] = ts.iloc[4] tm.assert_series_equal(result, ts) # repeat with datetimes result = ts.copy() result[datetime(1990, 1, 1, 9, tzinfo=tzget("UTC"))] = 0 - result[datetime(1990, 1, 1, 9, tzinfo=tzget("UTC"))] = ts[4] + result[datetime(1990, 1, 1, 9, tzinfo=tzget("UTC"))] = ts.iloc[4] tm.assert_series_equal(result, ts) result = ts.copy() dt = Timestamp(1990, 1, 1, 3).tz_localize(tzget("US/Central")) dt = dt.to_pydatetime() result[dt] = 0 - result[dt] = ts[4] + result[dt] = ts.iloc[4] tm.assert_series_equal(result, ts) @@ -106,12 +111,12 @@ def test_getitem_setitem_datetimeindex(): ts = Series(np.random.randn(N), index=rng) result = ts["1990-01-01 04:00:00"] - expected = ts[4] + expected = ts.iloc[4] assert result == expected result = ts.copy() result["1990-01-01 04:00:00"] = 0 - result["1990-01-01 04:00:00"] = ts[4] + result["1990-01-01 04:00:00"] = ts.iloc[4] tm.assert_series_equal(result, ts) result = ts["1990-01-01 04:00:00":"1990-01-01 07:00:00"] @@ -148,7 +153,7 @@ def test_getitem_setitem_datetimeindex(): # GH#36148 as of 2.0 we do not ignore tzawareness mismatch in indexing, # so setting it as a new key casts to object rather than matching # rng[4] - result[naive] = ts[4] + result[naive] = ts.iloc[4] assert result.index.dtype == object tm.assert_index_equal(result.index[:-1], rng.astype(object)) assert result.index[-1] == naive @@ -183,7 +188,7 @@ def test_getitem_setitem_datetimeindex(): tm.assert_series_equal(result, expected) result = ts[ts.index[4]] - expected = ts[4] + expected = ts.iloc[4] assert result == expected result = ts[ts.index[4:8]] @@ -212,12 +217,12 @@ def test_getitem_setitem_periodindex(): ts = Series(np.random.randn(N), index=rng) result = ts["1990-01-01 04"] - expected = ts[4] + expected = ts.iloc[4] assert result == expected result = ts.copy() result["1990-01-01 04"] = 0 - result["1990-01-01 04"] = ts[4] + result["1990-01-01 04"] = ts.iloc[4] tm.assert_series_equal(result, ts) result = ts["1990-01-01 04":"1990-01-01 07"] @@ -237,7 +242,7 @@ def test_getitem_setitem_periodindex(): # GH 2782 result = ts[ts.index[4]] - expected = ts[4] + expected = ts.iloc[4] assert result == expected result = ts[ts.index[4:8]] @@ -290,7 +295,7 @@ def test_indexing_with_duplicate_datetimeindex( if total > 1: tm.assert_series_equal(result, expected) else: - tm.assert_almost_equal(result, expected[0]) + tm.assert_almost_equal(result, expected.iloc[0]) cp = ts.copy() cp[date] = 0 diff --git a/pandas/tests/series/indexing/test_get.py b/pandas/tests/series/indexing/test_get.py index b78f545b2a010..e64a91d9ca581 100644 --- a/pandas/tests/series/indexing/test_get.py +++ b/pandas/tests/series/indexing/test_get.py @@ -144,7 +144,6 @@ def test_get_with_default(): # GH#7725 d0 = ["a", "b", "c", "d"] d1 = np.arange(4, dtype="int64") - others = ["e", 10] for data, index in ((d0, d1), (d1, d0)): s = Series(data, index=index) @@ -152,9 +151,17 @@ def test_get_with_default(): assert s.get(i) == d assert s.get(i, d) == d assert s.get(i, "z") == d - for other in others: - assert s.get(other, "z") == "z" - assert s.get(other, other) == other + + assert s.get("e", "z") == "z" + assert s.get("e", "e") == "e" + + msg = "Series.__getitem__ treating keys as positions is deprecated" + warn = None + if index is d0: + warn = FutureWarning + with tm.assert_produces_warning(warn, match=msg): + assert s.get(10, "z") == "z" + assert s.get(10, 10) == 10 @pytest.mark.parametrize( @@ -187,9 +194,13 @@ def test_get_with_ea(arr): result = ser.get("Z") assert result is None - assert ser.get(4) == ser.iloc[4] - assert ser.get(-1) == ser.iloc[-1] - assert ser.get(len(ser)) is None + msg = "Series.__getitem__ treating keys as positions is deprecated" + with tm.assert_produces_warning(FutureWarning, match=msg): + assert ser.get(4) == ser.iloc[4] + with tm.assert_produces_warning(FutureWarning, match=msg): + assert ser.get(-1) == ser.iloc[-1] + with tm.assert_produces_warning(FutureWarning, match=msg): + assert ser.get(len(ser)) is None # GH#21257 ser = Series(arr) @@ -198,14 +209,17 @@ def test_get_with_ea(arr): def test_getitem_get(string_series, object_series): + msg = "Series.__getitem__ treating keys as positions is deprecated" + for obj in [string_series, object_series]: idx = obj.index[5] assert obj[idx] == obj.get(idx) - assert obj[idx] == obj[5] + assert obj[idx] == obj.iloc[5] - assert string_series.get(-1) == string_series.get(string_series.index[-1]) - assert string_series[5] == string_series.get(string_series.index[5]) + with tm.assert_produces_warning(FutureWarning, match=msg): + assert string_series.get(-1) == string_series.get(string_series.index[-1]) + assert string_series.iloc[5] == string_series.get(string_series.index[5]) def test_get_none(): diff --git a/pandas/tests/series/indexing/test_getitem.py b/pandas/tests/series/indexing/test_getitem.py index 0f044ae576af8..8bfa59c5d9f08 100644 --- a/pandas/tests/series/indexing/test_getitem.py +++ b/pandas/tests/series/indexing/test_getitem.py @@ -72,14 +72,18 @@ def test_getitem_negative_out_of_bounds(self): ser = Series(tm.rands_array(5, 10), index=tm.rands_array(10, 10)) msg = "index -11 is out of bounds for axis 0 with size 10" + warn_msg = "Series.__getitem__ treating keys as positions is deprecated" with pytest.raises(IndexError, match=msg): - ser[-11] + with tm.assert_produces_warning(FutureWarning, match=warn_msg): + ser[-11] def test_getitem_out_of_bounds_indexerror(self, datetime_series): # don't segfault, GH#495 msg = r"index \d+ is out of bounds for axis 0 with size \d+" + warn_msg = "Series.__getitem__ treating keys as positions is deprecated" with pytest.raises(IndexError, match=msg): - datetime_series[len(datetime_series)] + with tm.assert_produces_warning(FutureWarning, match=warn_msg): + datetime_series[len(datetime_series)] def test_getitem_out_of_bounds_empty_rangeindex_keyerror(self): # GH#917 @@ -109,7 +113,10 @@ def test_getitem_keyerror_with_integer_index(self, any_int_numpy_dtype): def test_getitem_int64(self, datetime_series): idx = np.int64(5) - assert datetime_series[idx] == datetime_series[5] + msg = "Series.__getitem__ treating keys as positions is deprecated" + with tm.assert_produces_warning(FutureWarning, match=msg): + res = datetime_series[idx] + assert res == datetime_series.iloc[5] def test_getitem_full_range(self): # github.com/pandas-dev/pandas/commit/4f433773141d2eb384325714a2776bcc5b2e20f7 @@ -140,7 +147,7 @@ def test_string_index_alias_tz_aware(self, tz): ser = Series(np.random.randn(len(rng)), index=rng) result = ser["1/3/2000"] - tm.assert_almost_equal(result, ser[2]) + tm.assert_almost_equal(result, ser.iloc[2]) def test_getitem_time_object(self): rng = date_range("1/1/2000", "1/5/2000", freq="5min") @@ -205,7 +212,9 @@ def test_getitem_str_with_timedeltaindex(self): def test_getitem_bool_index_positional(self): # GH#48653 ser = Series({True: 1, False: 0}) - result = ser[0] + msg = "Series.__getitem__ treating keys as positions is deprecated" + with tm.assert_produces_warning(FutureWarning, match=msg): + result = ser[0] assert result == 1 @@ -371,7 +380,9 @@ def test_getitem_intlist_intervalindex_non_int(self, box): expected = ser.iloc[:1] key = box([0]) - result = ser[key] + msg = "Series.__getitem__ treating keys as positions is deprecated" + with tm.assert_produces_warning(FutureWarning, match=msg): + result = ser[key] tm.assert_series_equal(result, expected) @pytest.mark.parametrize("box", [list, np.array, Index]) @@ -612,7 +623,9 @@ def test_getitem_preserve_name(datetime_series): result = datetime_series[datetime_series > 0] assert result.name == datetime_series.name - result = datetime_series[[0, 2, 4]] + msg = "Series.__getitem__ treating keys as positions is deprecated" + with tm.assert_produces_warning(FutureWarning, match=msg): + result = datetime_series[[0, 2, 4]] assert result.name == datetime_series.name result = datetime_series[5:10] @@ -640,16 +653,20 @@ def test_getitem_missing(datetime_series): def test_getitem_fancy(string_series, object_series): - slice1 = string_series[[1, 2, 3]] - slice2 = object_series[[1, 2, 3]] + msg = "Series.__getitem__ treating keys as positions is deprecated" + with tm.assert_produces_warning(FutureWarning, match=msg): + slice1 = string_series[[1, 2, 3]] + slice2 = object_series[[1, 2, 3]] assert string_series.index[2] == slice1.index[1] assert object_series.index[2] == slice2.index[1] - assert string_series[2] == slice1[1] - assert object_series[2] == slice2[1] + assert string_series.iloc[2] == slice1.iloc[1] + assert object_series.iloc[2] == slice2.iloc[1] def test_getitem_box_float64(datetime_series): - value = datetime_series[5] + msg = "Series.__getitem__ treating keys as positions is deprecated" + with tm.assert_produces_warning(FutureWarning, match=msg): + value = datetime_series[5] assert isinstance(value, np.float64) @@ -683,7 +700,10 @@ def test_slice_can_reorder_not_uniquely_indexed(): def test_duplicated_index_getitem_positional_indexer(index_vals): # GH 11747 s = Series(range(5), index=list(index_vals)) - result = s[3] + + msg = "Series.__getitem__ treating keys as positions is deprecated" + with tm.assert_produces_warning(FutureWarning, match=msg): + result = s[3] assert result == 3 diff --git a/pandas/tests/series/indexing/test_indexing.py b/pandas/tests/series/indexing/test_indexing.py index b347691c3c101..83cae8d148feb 100644 --- a/pandas/tests/series/indexing/test_indexing.py +++ b/pandas/tests/series/indexing/test_indexing.py @@ -27,11 +27,14 @@ def test_basic_indexing(): s = Series(np.random.randn(5), index=["a", "b", "a", "a", "b"]) + warn_msg = "Series.__[sg]etitem__ treating keys as positions is deprecated" msg = "index 5 is out of bounds for axis 0 with size 5" with pytest.raises(IndexError, match=msg): - s[5] + with tm.assert_produces_warning(FutureWarning, match=warn_msg): + s[5] with pytest.raises(IndexError, match=msg): - s[5] = 0 + with tm.assert_produces_warning(FutureWarning, match=warn_msg): + s[5] = 0 with pytest.raises(KeyError, match=r"^'c'$"): s["c"] @@ -39,10 +42,12 @@ def test_basic_indexing(): s = s.sort_index() with pytest.raises(IndexError, match=msg): - s[5] + with tm.assert_produces_warning(FutureWarning, match=warn_msg): + s[5] msg = r"index 5 is out of bounds for axis (0|1) with size 5|^5$" with pytest.raises(IndexError, match=msg): - s[5] = 0 + with tm.assert_produces_warning(FutureWarning, match=warn_msg): + s[5] = 0 def test_getitem_numeric_should_not_fallback_to_positional(any_numeric_dtype): @@ -144,7 +149,9 @@ def test_series_box_timestamp(): assert isinstance(ser.iloc[4], Timestamp) ser = Series(rng, index=rng) - assert isinstance(ser[0], Timestamp) + msg = "Series.__getitem__ treating keys as positions is deprecated" + with tm.assert_produces_warning(FutureWarning, match=msg): + assert isinstance(ser[0], Timestamp) assert isinstance(ser.at[rng[1]], Timestamp) assert isinstance(ser.iat[2], Timestamp) assert isinstance(ser.loc[rng[3]], Timestamp) @@ -187,12 +194,12 @@ def test_setitem_ambiguous_keyerror(indexer_sl): def test_setitem(datetime_series): datetime_series[datetime_series.index[5]] = np.NaN - datetime_series[[1, 2, 17]] = np.NaN - datetime_series[6] = np.NaN - assert np.isnan(datetime_series[6]) - assert np.isnan(datetime_series[2]) + datetime_series.iloc[[1, 2, 17]] = np.NaN + datetime_series.iloc[6] = np.NaN + assert np.isnan(datetime_series.iloc[6]) + assert np.isnan(datetime_series.iloc[2]) datetime_series[np.isnan(datetime_series)] = 5 - assert not np.isnan(datetime_series[2]) + assert not np.isnan(datetime_series.iloc[2]) def test_setslice(datetime_series): @@ -291,9 +298,9 @@ def test_underlying_data_conversion(using_copy_on_write): def test_preserve_refs(datetime_series): - seq = datetime_series[[5, 10, 15]] - seq[1] = np.NaN - assert not np.isnan(datetime_series[10]) + seq = datetime_series.iloc[[5, 10, 15]] + seq.iloc[1] = np.NaN + assert not np.isnan(datetime_series.iloc[10]) def test_multilevel_preserve_name(lexsorted_two_level_string_multiindex, indexer_sl): diff --git a/pandas/tests/series/indexing/test_setitem.py b/pandas/tests/series/indexing/test_setitem.py index 9df607cb6c64c..ede59b3ac9cb9 100644 --- a/pandas/tests/series/indexing/test_setitem.py +++ b/pandas/tests/series/indexing/test_setitem.py @@ -176,8 +176,10 @@ def test_setitem_negative_out_of_bounds(self): ser = Series(tm.rands_array(5, 10), index=tm.rands_array(10, 10)) msg = "index -11 is out of bounds for axis 0 with size 10" + warn_msg = "Series.__setitem__ treating keys as positions is deprecated" with pytest.raises(IndexError, match=msg): - ser[-11] = "foo" + with tm.assert_produces_warning(FutureWarning, match=warn_msg): + ser[-11] = "foo" @pytest.mark.parametrize("indexer", [tm.loc, tm.at]) @pytest.mark.parametrize("ser_index", [0, 1]) @@ -1527,7 +1529,9 @@ def test_setitem_positional_with_casting(): # we fallback we *also* get a ValueError if we try to set inplace. ser = Series([1, 2, 3], index=["a", "b", "c"]) - ser[0] = "X" + warn_msg = "Series.__setitem__ treating keys as positions is deprecated" + with tm.assert_produces_warning(FutureWarning, match=warn_msg): + ser[0] = "X" expected = Series(["X", 2, 3], index=["a", "b", "c"], dtype=object) tm.assert_series_equal(ser, expected) @@ -1536,7 +1540,10 @@ def test_setitem_positional_float_into_int_coerces(): # Case where we hit a KeyError and then trying to set in-place incorrectly # casts a float to an int ser = Series([1, 2, 3], index=["a", "b", "c"]) - ser[0] = 1.5 + + warn_msg = "Series.__setitem__ treating keys as positions is deprecated" + with tm.assert_produces_warning(FutureWarning, match=warn_msg): + ser[0] = 1.5 expected = Series([1.5, 2, 3], index=["a", "b", "c"]) tm.assert_series_equal(ser, expected) diff --git a/pandas/tests/series/methods/test_asof.py b/pandas/tests/series/methods/test_asof.py index 638216302b92f..5ee5671d1dea3 100644 --- a/pandas/tests/series/methods/test_asof.py +++ b/pandas/tests/series/methods/test_asof.py @@ -71,16 +71,16 @@ def test_scalar(self): val1 = ts.asof(ts.index[7]) val2 = ts.asof(ts.index[19]) - assert val1 == ts[4] - assert val2 == ts[14] + assert val1 == ts.iloc[4] + assert val2 == ts.iloc[14] # accepts strings val1 = ts.asof(str(ts.index[7])) - assert val1 == ts[4] + assert val1 == ts.iloc[4] # in there result = ts.asof(ts.index[3]) - assert result == ts[3] + assert result == ts.iloc[3] # no as of value d = ts.index[0] - offsets.BDay() @@ -144,15 +144,15 @@ def test_periodindex(self): val1 = ts.asof(ts.index[7]) val2 = ts.asof(ts.index[19]) - assert val1 == ts[4] - assert val2 == ts[14] + assert val1 == ts.iloc[4] + assert val2 == ts.iloc[14] # accepts strings val1 = ts.asof(str(ts.index[7])) - assert val1 == ts[4] + assert val1 == ts.iloc[4] # in there - assert ts.asof(ts.index[3]) == ts[3] + assert ts.asof(ts.index[3]) == ts.iloc[3] # no as of value d = ts.index[0].to_timestamp() - offsets.BDay() diff --git a/pandas/tests/series/methods/test_fillna.py b/pandas/tests/series/methods/test_fillna.py index 36967c5cad925..df9e218e9fae4 100644 --- a/pandas/tests/series/methods/test_fillna.py +++ b/pandas/tests/series/methods/test_fillna.py @@ -72,7 +72,7 @@ def test_fillna(self): tm.assert_series_equal(ts, ts.fillna(method="ffill")) - ts[2] = np.NaN + ts.iloc[2] = np.NaN exp = Series([0.0, 1.0, 1.0, 3.0, 4.0], index=ts.index) tm.assert_series_equal(ts.fillna(method="ffill"), exp) @@ -859,7 +859,7 @@ def test_fillna_bug(self): def test_ffill(self): ts = Series([0.0, 1.0, 2.0, 3.0, 4.0], index=tm.makeDateIndex(5)) - ts[2] = np.NaN + ts.iloc[2] = np.NaN tm.assert_series_equal(ts.ffill(), ts.fillna(method="ffill")) def test_ffill_mixed_dtypes_without_missing_data(self): @@ -870,7 +870,7 @@ def test_ffill_mixed_dtypes_without_missing_data(self): def test_bfill(self): ts = Series([0.0, 1.0, 2.0, 3.0, 4.0], index=tm.makeDateIndex(5)) - ts[2] = np.NaN + ts.iloc[2] = np.NaN tm.assert_series_equal(ts.bfill(), ts.fillna(method="bfill")) def test_pad_nan(self): @@ -885,7 +885,7 @@ def test_pad_nan(self): [np.nan, 1.0, 1.0, 3.0, 3.0], ["z", "a", "b", "c", "d"], dtype=float ) tm.assert_series_equal(x[1:], expected[1:]) - assert np.isnan(x[0]), np.isnan(expected[0]) + assert np.isnan(x.iloc[0]), np.isnan(expected.iloc[0]) def test_series_fillna_limit(self): index = np.arange(10) diff --git a/pandas/tests/series/methods/test_map.py b/pandas/tests/series/methods/test_map.py index c4570fdb499a3..925384cac605e 100644 --- a/pandas/tests/series/methods/test_map.py +++ b/pandas/tests/series/methods/test_map.py @@ -242,7 +242,7 @@ def test_map_type_inference(): def test_map_decimal(string_series): result = string_series.map(lambda x: Decimal(str(x))) assert result.dtype == np.object_ - assert isinstance(result[0], Decimal) + assert isinstance(result.iloc[0], Decimal) def test_map_na_exclusion(): diff --git a/pandas/tests/series/methods/test_reset_index.py b/pandas/tests/series/methods/test_reset_index.py index 9362b0b52a698..ec38c5b8b744b 100644 --- a/pandas/tests/series/methods/test_reset_index.py +++ b/pandas/tests/series/methods/test_reset_index.py @@ -20,7 +20,7 @@ def test_reset_index_dti_round_trip(self): dti = date_range(start="1/1/2001", end="6/1/2001", freq="D")._with_freq(None) d1 = DataFrame({"v": np.random.rand(len(dti))}, index=dti) d2 = d1.reset_index() - assert d2.dtypes[0] == np.dtype("M8[ns]") + assert d2.dtypes.iloc[0] == np.dtype("M8[ns]") d3 = d2.set_index("index") tm.assert_frame_equal(d1, d3, check_names=False) @@ -30,7 +30,7 @@ def test_reset_index_dti_round_trip(self): df = df.set_index("Date") assert df.index[0] == stamp - assert df.reset_index()["Date"][0] == stamp + assert df.reset_index()["Date"].iloc[0] == stamp def test_reset_index(self): df = tm.makeDataFrame()[:5] diff --git a/pandas/tests/series/methods/test_values.py b/pandas/tests/series/methods/test_values.py index 479c7033a3fb5..cb1595e68264f 100644 --- a/pandas/tests/series/methods/test_values.py +++ b/pandas/tests/series/methods/test_values.py @@ -25,5 +25,5 @@ def test_values_object_extension_dtypes(self, data): def test_values(self, datetime_series): tm.assert_almost_equal( - datetime_series.values, datetime_series, check_dtype=False + datetime_series.values, list(datetime_series), check_dtype=False ) diff --git a/pandas/tests/series/test_arithmetic.py b/pandas/tests/series/test_arithmetic.py index 3aa81c5e99ffe..a0edfae606e3f 100644 --- a/pandas/tests/series/test_arithmetic.py +++ b/pandas/tests/series/test_arithmetic.py @@ -225,8 +225,8 @@ def test_add_na_handling(self): result = ser + ser.shift(1) result2 = ser.shift(1) + ser - assert isna(result[0]) - assert isna(result2[0]) + assert isna(result.iloc[0]) + assert isna(result2.iloc[0]) def test_add_corner_cases(self, datetime_series): empty = Series([], index=Index([]), dtype=np.float64) diff --git a/pandas/tests/series/test_iteration.py b/pandas/tests/series/test_iteration.py index 21ad1747e1086..edc82455234bb 100644 --- a/pandas/tests/series/test_iteration.py +++ b/pandas/tests/series/test_iteration.py @@ -5,12 +5,12 @@ def test_keys(self, datetime_series): def test_iter_datetimes(self, datetime_series): for i, val in enumerate(datetime_series): # pylint: disable-next=unnecessary-list-index-lookup - assert val == datetime_series[i] + assert val == datetime_series.iloc[i] def test_iter_strings(self, string_series): for i, val in enumerate(string_series): # pylint: disable-next=unnecessary-list-index-lookup - assert val == string_series[i] + assert val == string_series.iloc[i] def test_iteritems_datetimes(self, datetime_series): for idx, val in datetime_series.items(): diff --git a/pandas/tests/strings/test_find_replace.py b/pandas/tests/strings/test_find_replace.py index 6f6acb7a996b2..61f9289dd63f2 100644 --- a/pandas/tests/strings/test_find_replace.py +++ b/pandas/tests/strings/test_find_replace.py @@ -954,21 +954,21 @@ def test_flags_kwarg(any_string_dtype): with tm.maybe_produces_warning(PerformanceWarning, using_pyarrow): result = data.str.match(pat, flags=re.IGNORECASE) - assert result[0] + assert result.iloc[0] with tm.maybe_produces_warning(PerformanceWarning, using_pyarrow): result = data.str.fullmatch(pat, flags=re.IGNORECASE) - assert result[0] + assert result.iloc[0] result = data.str.findall(pat, flags=re.IGNORECASE) - assert result[0][0] == ("dave", "google", "com") + assert result.iloc[0][0] == ("dave", "google", "com") result = data.str.count(pat, flags=re.IGNORECASE) - assert result[0] == 1 + assert result.iloc[0] == 1 msg = "has match groups" with tm.assert_produces_warning( UserWarning, match=msg, raise_on_extra_warnings=not using_pyarrow ): result = data.str.contains(pat, flags=re.IGNORECASE) - assert result[0] + assert result.iloc[0] diff --git a/pandas/tests/tools/test_to_datetime.py b/pandas/tests/tools/test_to_datetime.py index 0b5696116e610..e51bd0252054f 100644 --- a/pandas/tests/tools/test_to_datetime.py +++ b/pandas/tests/tools/test_to_datetime.py @@ -2575,11 +2575,11 @@ def test_string_na_nat_conversion_with_name(self, cache): expected = Series(np.empty(5, dtype="M8[ns]"), index=idx) for i in range(5): - x = series[i] + x = series.iloc[i] if isna(x): - expected[i] = NaT + expected.iloc[i] = NaT else: - expected[i] = to_datetime(x, cache=cache) + expected.iloc[i] = to_datetime(x, cache=cache) tm.assert_series_equal(result, expected, check_names=False) assert result.name == "foo" diff --git a/pandas/tests/window/test_apply.py b/pandas/tests/window/test_apply.py index ee7f57ddab0ad..c37fd5258874f 100644 --- a/pandas/tests/window/test_apply.py +++ b/pandas/tests/window/test_apply.py @@ -250,7 +250,7 @@ def test_time_rule_series(raw, series): prev_date = last_date - 24 * offsets.BDay() trunc_series = series[::2].truncate(prev_date, last_date) - tm.assert_almost_equal(series_result[-1], np.mean(trunc_series)) + tm.assert_almost_equal(series_result.iloc[-1], np.mean(trunc_series)) def test_time_rule_frame(raw, frame): diff --git a/pandas/tests/window/test_pairwise.py b/pandas/tests/window/test_pairwise.py index 77a64999053e2..f9bc572b41312 100644 --- a/pandas/tests/window/test_pairwise.py +++ b/pandas/tests/window/test_pairwise.py @@ -54,7 +54,7 @@ def test_rolling_cov(series): B = A + np.random.randn(len(A)) result = A.rolling(window=50, min_periods=25).cov(B) - tm.assert_almost_equal(result[-1], np.cov(A[-50:], B[-50:])[0, 1]) + tm.assert_almost_equal(result.iloc[-1], np.cov(A[-50:], B[-50:])[0, 1]) def test_rolling_corr(series): @@ -62,7 +62,7 @@ def test_rolling_corr(series): B = A + np.random.randn(len(A)) result = A.rolling(window=50, min_periods=25).corr(B) - tm.assert_almost_equal(result[-1], np.corrcoef(A[-50:], B[-50:])[0, 1]) + tm.assert_almost_equal(result.iloc[-1], np.corrcoef(A[-50:], B[-50:])[0, 1]) # test for correct bias correction a = tm.makeTimeSeries() @@ -71,7 +71,7 @@ def test_rolling_corr(series): b[:10] = np.nan result = a.rolling(window=len(a), min_periods=1).corr(b) - tm.assert_almost_equal(result[-1], a.corr(b)) + tm.assert_almost_equal(result.iloc[-1], a.corr(b)) @pytest.mark.parametrize("func", ["cov", "corr"]) diff --git a/pandas/tests/window/test_rolling.py b/pandas/tests/window/test_rolling.py index 3a58a6860a8b5..084b4b606ad5e 100644 --- a/pandas/tests/window/test_rolling.py +++ b/pandas/tests/window/test_rolling.py @@ -1060,7 +1060,7 @@ def test_rolling_numerical_too_large_numbers(): # GH: 11645 dates = date_range("2015-01-01", periods=10, freq="D") ds = Series(data=range(10), index=dates, dtype=np.float64) - ds[2] = -9e33 + ds.iloc[2] = -9e33 result = ds.rolling(5).mean() expected = Series( [ diff --git a/pandas/tests/window/test_rolling_functions.py b/pandas/tests/window/test_rolling_functions.py index c4519701da7ab..8839aee04595d 100644 --- a/pandas/tests/window/test_rolling_functions.py +++ b/pandas/tests/window/test_rolling_functions.py @@ -99,7 +99,7 @@ def test_time_rule_series(series, compare_func, roll_func, kwargs, minp): prev_date = last_date - 24 * offsets.BDay() trunc_series = series[::2].truncate(prev_date, last_date) - tm.assert_almost_equal(series_result[-1], compare_func(trunc_series)) + tm.assert_almost_equal(series_result.iloc[-1], compare_func(trunc_series)) @pytest.mark.parametrize( diff --git a/pandas/tests/window/test_rolling_quantile.py b/pandas/tests/window/test_rolling_quantile.py index e78e997f220b5..40c9a74f2cf3a 100644 --- a/pandas/tests/window/test_rolling_quantile.py +++ b/pandas/tests/window/test_rolling_quantile.py @@ -65,7 +65,7 @@ def test_time_rule_series(series, q): prev_date = last_date - 24 * offsets.BDay() trunc_series = series[::2].truncate(prev_date, last_date) - tm.assert_almost_equal(series_result[-1], compare_func(trunc_series)) + tm.assert_almost_equal(series_result.iloc[-1], compare_func(trunc_series)) @pytest.mark.parametrize("q", [0.0, 0.1, 0.5, 0.9, 1.0]) diff --git a/pandas/tests/window/test_rolling_skew_kurt.py b/pandas/tests/window/test_rolling_skew_kurt.py index 43db772251219..56acee542aea4 100644 --- a/pandas/tests/window/test_rolling_skew_kurt.py +++ b/pandas/tests/window/test_rolling_skew_kurt.py @@ -56,7 +56,7 @@ def test_time_rule_series(series, sp_func, roll_func): prev_date = last_date - 24 * offsets.BDay() trunc_series = series[::2].truncate(prev_date, last_date) - tm.assert_almost_equal(series_result[-1], compare_func(trunc_series)) + tm.assert_almost_equal(series_result.iloc[-1], compare_func(trunc_series)) @td.skip_if_no_scipy From e282934744f0d1282d01128e8c7c7ca93b5d4311 Mon Sep 17 00:00:00 2001 From: Brock Date: Fri, 12 May 2023 12:27:26 -0700 Subject: [PATCH 2/8] troubleshoot docs --- doc/source/user_guide/basics.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/user_guide/basics.rst b/doc/source/user_guide/basics.rst index 34965a2a6d711..7335f1cb066ce 100644 --- a/doc/source/user_guide/basics.rst +++ b/doc/source/user_guide/basics.rst @@ -1364,7 +1364,7 @@ We illustrate these fill methods on a simple Series: rng = pd.date_range("1/3/2000", periods=8) ts = pd.Series(np.random.randn(8), index=rng) - ts2 = ts[[0, 3, 6]] + ts2 = ts.iloc[[0, 3, 6]] ts ts2 From d2d3849d9a8afa425c40e41d8138ab1848ba11f0 Mon Sep 17 00:00:00 2001 From: Brock Date: Mon, 15 May 2023 09:26:44 -0700 Subject: [PATCH 3/8] troubleshoot docs --- doc/source/user_guide/cookbook.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/user_guide/cookbook.rst b/doc/source/user_guide/cookbook.rst index 36c4604f772a3..c7278c604ca02 100644 --- a/doc/source/user_guide/cookbook.rst +++ b/doc/source/user_guide/cookbook.rst @@ -546,7 +546,7 @@ Unlike agg, apply's callable is passed a sub-DataFrame which gives you access to def MyCust(x): if len(x) > 2: - return x[1] * 1.234 + return x.iloc[1] * 1.234 return pd.NaT mhc = {"Mean": np.mean, "Max": np.max, "Custom": MyCust} From 255cf7db3fb00aca6b5ebe2b8de5df742d2f122c Mon Sep 17 00:00:00 2001 From: Brock Date: Wed, 17 May 2023 08:36:22 -0700 Subject: [PATCH 4/8] update doc --- doc/source/user_guide/dsintro.rst | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/doc/source/user_guide/dsintro.rst b/doc/source/user_guide/dsintro.rst index 01ec336f7aae5..0ad52c3cd80a7 100644 --- a/doc/source/user_guide/dsintro.rst +++ b/doc/source/user_guide/dsintro.rst @@ -102,15 +102,15 @@ However, operations such as slicing will also slice the index. .. ipython:: python - s[0] - s[:3] - s[s > s.median()] - s[[4, 3, 1]] + s.iloc[0] + s.iloc[:3] + s.iloc[s > s.median()] + s.iloc[[4, 3, 1]] np.exp(s) .. note:: - We will address array-based indexing like ``s[[4, 3, 1]]`` + We will address array-based indexing like ``s.iloc[[4, 3, 1]]`` in :ref:`section on indexing `. Like a NumPy array, a pandas :class:`Series` has a single :attr:`~Series.dtype`. @@ -201,7 +201,7 @@ labels. .. ipython:: python - s[1:] + s[:-1] + s.iloc[1:] + s.iloc[:-1] The result of an operation between unaligned :class:`Series` will have the **union** of the indexes involved. If a label is not found in one :class:`Series` or the other, the From d4d6a08ecde7376e1afbf2f72636970f8c83c2c9 Mon Sep 17 00:00:00 2001 From: Brock Date: Wed, 17 May 2023 09:37:38 -0700 Subject: [PATCH 5/8] update docs --- doc/source/user_guide/dsintro.rst | 2 +- doc/source/user_guide/missing_data.rst | 4 ++-- doc/source/user_guide/timeseries.rst | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/doc/source/user_guide/dsintro.rst b/doc/source/user_guide/dsintro.rst index 0ad52c3cd80a7..4b0829e4a23b9 100644 --- a/doc/source/user_guide/dsintro.rst +++ b/doc/source/user_guide/dsintro.rst @@ -104,7 +104,7 @@ However, operations such as slicing will also slice the index. s.iloc[0] s.iloc[:3] - s.iloc[s > s.median()] + s[s > s.median()] s.iloc[[4, 3, 1]] np.exp(s) diff --git a/doc/source/user_guide/missing_data.rst b/doc/source/user_guide/missing_data.rst index a17d0eba294b2..6ea4c213e85c8 100644 --- a/doc/source/user_guide/missing_data.rst +++ b/doc/source/user_guide/missing_data.rst @@ -368,7 +368,7 @@ Index aware interpolation is available via the ``method`` keyword: .. ipython:: python :suppress: - ts2 = ts[[0, 1, 30, 60, 99]] + ts2 = ts.iloc[[0, 1, 30, 60, 99]] .. ipython:: python @@ -443,7 +443,7 @@ Compare several methods: ser = pd.Series(np.arange(1, 10.1, 0.25) ** 2 + np.random.randn(37)) missing = np.array([4, 13, 14, 15, 16, 17, 18, 20, 29]) - ser[missing] = np.nan + ser.iloc[missing] = np.nan methods = ["linear", "quadratic", "cubic"] df = pd.DataFrame({m: ser.interpolate(method=m) for m in methods}) diff --git a/doc/source/user_guide/timeseries.rst b/doc/source/user_guide/timeseries.rst index 3519ac2d64f71..97be46e338c09 100644 --- a/doc/source/user_guide/timeseries.rst +++ b/doc/source/user_guide/timeseries.rst @@ -777,7 +777,7 @@ regularity will result in a ``DatetimeIndex``, although frequency is lost: .. ipython:: python - ts2[[0, 2, 6]].index + ts2.iloc[[0, 2, 6]].index .. _timeseries.components: From 872ea35bb9332709dd29774993ee284d764a155a Mon Sep 17 00:00:00 2001 From: Brock Date: Wed, 17 May 2023 13:13:40 -0700 Subject: [PATCH 6/8] docs --- doc/source/whatsnew/v1.1.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index ab14891b40151..f31ab02725394 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -55,7 +55,7 @@ For example: pi = dti.to_period("D") ser_monotonic = pd.Series(np.arange(30), index=pi) shuffler = list(range(0, 30, 2)) + list(range(1, 31, 2)) - ser = ser_monotonic[shuffler] + ser = ser_monotonic.iloc[shuffler] ser .. ipython:: python From ada8ca2c2c10cbedfca16882fa91ea731d7ec3c9 Mon Sep 17 00:00:00 2001 From: Brock Date: Wed, 17 May 2023 15:39:12 -0700 Subject: [PATCH 7/8] update fixture --- pandas/conftest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/conftest.py b/pandas/conftest.py index 86f0121dd00a9..90377c53b447b 100644 --- a/pandas/conftest.py +++ b/pandas/conftest.py @@ -746,7 +746,7 @@ def series_with_multilevel_index() -> Series: index = MultiIndex.from_tuples(tuples) data = np.random.randn(8) ser = Series(data, index=index) - ser[3] = np.NaN + ser.iloc[3] = np.NaN return ser From 0825bd58ab6af2caefe2dc1baebfe0f963199af5 Mon Sep 17 00:00:00 2001 From: Brock Date: Wed, 17 May 2023 16:52:17 -0700 Subject: [PATCH 8/8] update doctest --- pandas/core/generic.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 52bbafffe5340..79e385024aee1 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -6528,8 +6528,8 @@ def copy(self, deep: bool_t | None = True) -> Self: Updates to the data shared by shallow copy and original is reflected in both; deep copy remains unchanged. - >>> s[0] = 3 - >>> shallow[1] = 4 + >>> s.iloc[0] = 3 + >>> shallow.iloc[1] = 4 >>> s a 3 b 4