Skip to content

Commit 4539f4f

Browse files
Deprecate positional access for label based indexes in Series.__getitem__ (#14654)
This PR deprecates positional access in `Series.__getitem__` when a label-based index is present. xref: pandas-dev/pandas#53201 On `pandas_2.0_feature_branch`: ``` = 260 failed, 101179 passed, 2091 skipped, 954 xfailed, 312 xpassed in 1104.58s (0:18:24) = ``` This PR: ``` = 248 failed, 101190 passed, 2091 skipped, 954 xfailed, 312 xpassed in 1105.78s (0:18:25) = ```
1 parent f736d72 commit 4539f4f

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

python/cudf/cudf/core/series.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,12 @@ def _loc_to_iloc(self, arg):
371371
arg = arg[0]
372372
if _is_scalar_or_zero_d_array(arg):
373373
index_dtype = self._frame.index.dtype
374+
warn_msg = (
375+
"Series.__getitem__ treating keys as positions is deprecated. "
376+
"In a future version, integer keys will always be treated "
377+
"as labels (consistent with DataFrame behavior). To access "
378+
"a value by position, use `ser.iloc[pos]`"
379+
)
374380
if not _is_non_decimal_numeric_dtype(index_dtype) and not (
375381
isinstance(index_dtype, cudf.CategoricalDtype)
376382
and is_integer_dtype(index_dtype.categories.dtype)
@@ -379,11 +385,13 @@ def _loc_to_iloc(self, arg):
379385
if isinstance(arg, cudf.Scalar) and is_integer_dtype(
380386
arg.dtype
381387
):
382-
found_index = arg.value
383-
return found_index
388+
# Do not remove until pandas 3.0 support is added.
389+
warnings.warn(warn_msg, FutureWarning)
390+
return arg.value
384391
elif is_integer(arg):
385-
found_index = arg
386-
return found_index
392+
# Do not remove until pandas 3.0 support is added.
393+
warnings.warn(warn_msg, FutureWarning)
394+
return arg
387395
try:
388396
indices = self._frame.index._indices_of(arg)
389397
if (n := len(indices)) == 0:

python/cudf/cudf/tests/test_csv.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -595,12 +595,12 @@ def test_csv_reader_NaN_values():
595595
header=None,
596596
na_values=custom_na_values,
597597
)
598-
assert gdf.dtypes[0] == "int8"
598+
assert gdf.dtypes.iloc[0] == "int8"
599599
assert all(gdf["0"][idx] is cudf.NA for idx in range(len(gdf["0"])))
600600

601601
# data type detection should evaluate the column to object if some nulls
602602
gdf = read_csv(StringIO(all_cells), header=None)
603-
assert gdf.dtypes[0] == np.dtype("object")
603+
assert gdf.dtypes.iloc[0] == np.dtype("object")
604604

605605

606606
def test_csv_reader_thousands(tmpdir):

python/cudf/cudf/tests/test_indexing.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
import pytest
1010

1111
import cudf
12+
from cudf.core._compat import PANDAS_GE_210
1213
from cudf.testing import _utils as utils
1314
from cudf.testing._utils import (
1415
INTEGER_TYPES,
1516
assert_eq,
1617
assert_exceptions_equal,
18+
expect_warning_if,
1719
)
1820

1921
index_dtypes = INTEGER_TYPES
@@ -151,8 +153,10 @@ def test_series_get_item_iloc_defer(arg):
151153
ps = pd.Series([1, 2, 3], index=pd.Index(["a", "b", "c"]))
152154
gs = cudf.from_pandas(ps)
153155

154-
expect = ps[arg]
155-
got = gs[arg]
156+
with expect_warning_if(PANDAS_GE_210 and not isinstance(arg, str)):
157+
expect = ps[arg]
158+
with expect_warning_if(not isinstance(arg, str)):
159+
got = gs[arg]
156160

157161
assert_eq(expect, got)
158162

@@ -163,7 +167,7 @@ def test_series_iloc_defer_cudf_scalar():
163167

164168
for t in index_dtypes:
165169
arg = cudf.Scalar(1, dtype=t)
166-
got = gs[arg]
170+
got = gs.iloc[arg]
167171
expect = 2
168172
assert_eq(expect, got)
169173

0 commit comments

Comments
 (0)