Skip to content

Commit 7b22448

Browse files
authored
DEPR: positional indexing on Series __getitem__/__setitem__ (#53201)
* DEPR: positional indexing on Series __getitem__/__setitem__ * troubleshoot docs * troubleshoot docs * update doc * update docs * docs * update fixture * update doctest
1 parent b968ce5 commit 7b22448

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+293
-176
lines changed

doc/source/user_guide/basics.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1364,7 +1364,7 @@ We illustrate these fill methods on a simple Series:
13641364
13651365
rng = pd.date_range("1/3/2000", periods=8)
13661366
ts = pd.Series(np.random.randn(8), index=rng)
1367-
ts2 = ts[[0, 3, 6]]
1367+
ts2 = ts.iloc[[0, 3, 6]]
13681368
ts
13691369
ts2
13701370

doc/source/user_guide/cookbook.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ Unlike agg, apply's callable is passed a sub-DataFrame which gives you access to
546546
547547
def MyCust(x):
548548
if len(x) > 2:
549-
return x[1] * 1.234
549+
return x.iloc[1] * 1.234
550550
return pd.NaT
551551
552552
mhc = {"Mean": np.mean, "Max": np.max, "Custom": MyCust}

doc/source/user_guide/dsintro.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,15 @@ However, operations such as slicing will also slice the index.
102102

103103
.. ipython:: python
104104
105-
s[0]
106-
s[:3]
105+
s.iloc[0]
106+
s.iloc[:3]
107107
s[s > s.median()]
108-
s[[4, 3, 1]]
108+
s.iloc[[4, 3, 1]]
109109
np.exp(s)
110110
111111
.. note::
112112

113-
We will address array-based indexing like ``s[[4, 3, 1]]``
113+
We will address array-based indexing like ``s.iloc[[4, 3, 1]]``
114114
in :ref:`section on indexing <indexing>`.
115115

116116
Like a NumPy array, a pandas :class:`Series` has a single :attr:`~Series.dtype`.
@@ -201,7 +201,7 @@ labels.
201201

202202
.. ipython:: python
203203
204-
s[1:] + s[:-1]
204+
s.iloc[1:] + s.iloc[:-1]
205205
206206
The result of an operation between unaligned :class:`Series` will have the **union** of
207207
the indexes involved. If a label is not found in one :class:`Series` or the other, the

doc/source/user_guide/missing_data.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ Index aware interpolation is available via the ``method`` keyword:
368368
.. ipython:: python
369369
:suppress:
370370
371-
ts2 = ts[[0, 1, 30, 60, 99]]
371+
ts2 = ts.iloc[[0, 1, 30, 60, 99]]
372372
373373
.. ipython:: python
374374
@@ -443,7 +443,7 @@ Compare several methods:
443443
444444
ser = pd.Series(np.arange(1, 10.1, 0.25) ** 2 + np.random.randn(37))
445445
missing = np.array([4, 13, 14, 15, 16, 17, 18, 20, 29])
446-
ser[missing] = np.nan
446+
ser.iloc[missing] = np.nan
447447
methods = ["linear", "quadratic", "cubic"]
448448
449449
df = pd.DataFrame({m: ser.interpolate(method=m) for m in methods})

doc/source/user_guide/timeseries.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -777,7 +777,7 @@ regularity will result in a ``DatetimeIndex``, although frequency is lost:
777777

778778
.. ipython:: python
779779
780-
ts2[[0, 2, 6]].index
780+
ts2.iloc[[0, 2, 6]].index
781781
782782
.. _timeseries.components:
783783

doc/source/whatsnew/v1.1.0.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ For example:
5555
pi = dti.to_period("D")
5656
ser_monotonic = pd.Series(np.arange(30), index=pi)
5757
shuffler = list(range(0, 30, 2)) + list(range(1, 31, 2))
58-
ser = ser_monotonic[shuffler]
58+
ser = ser_monotonic.iloc[shuffler]
5959
ser
6060
6161
.. ipython:: python

doc/source/whatsnew/v2.1.0.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ Deprecations
264264
- 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`)
265265
- 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`)
266266
- Deprecated constructing :class:`SparseArray` from scalar data, pass a sequence instead (:issue:`53039`)
267-
-
267+
- 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`)
268268

269269
.. ---------------------------------------------------------------------------
270270
.. _whatsnew_210.performance:

pandas/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ def series_with_multilevel_index() -> Series:
746746
index = MultiIndex.from_tuples(tuples)
747747
data = np.random.randn(8)
748748
ser = Series(data, index=index)
749-
ser[3] = np.NaN
749+
ser.iloc[3] = np.NaN
750750
return ser
751751

752752

pandas/core/apply.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1435,7 +1435,7 @@ def relabel_result(
14351435
com.get_callable_name(f) if not isinstance(f, str) else f for f in fun
14361436
]
14371437
col_idx_order = Index(s.index).get_indexer(fun)
1438-
s = s[col_idx_order]
1438+
s = s.iloc[col_idx_order]
14391439

14401440
# assign the new user-provided "named aggregation" as index names, and reindex
14411441
# it based on the whole user-provided names.

pandas/core/generic.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6512,8 +6512,8 @@ def copy(self, deep: bool_t | None = True) -> Self:
65126512
Updates to the data shared by shallow copy and original is reflected
65136513
in both; deep copy remains unchanged.
65146514
6515-
>>> s[0] = 3
6516-
>>> shallow[1] = 4
6515+
>>> s.iloc[0] = 3
6516+
>>> shallow.iloc[1] = 4
65176517
>>> s
65186518
a 3
65196519
b 4

0 commit comments

Comments
 (0)