Skip to content

DEPR: row-based indexing in DataFrame.__getitem__ #31334

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2776,6 +2776,13 @@ def __getitem__(self, key):
if indexer is not None:
# either we have a slice or we have a string that can be converted
# to a slice for partial-string date indexing
if not isinstance(key, slice):
warnings.warn(
"row-based slicing on a DataFrame is deprecated and will "
"raise in a future version. Use `df.loc[key]` instead.",
FutureWarning,
stacklevel=2,
)
return self._slice(indexer, axis=0)

# Do we have a (boolean) DataFrame?
Expand Down Expand Up @@ -2926,6 +2933,13 @@ def __setitem__(self, key, value):
if indexer is not None:
# either we have a slice or we have a string that can be converted
# to a slice for partial-string date indexing
if not isinstance(key, slice):
warnings.warn(
"row-based slicing on a DataFrame is deprecated and will "
"raise in a future version. Use `df.loc[key]` instead.",
FutureWarning,
stacklevel=2,
)
return self._setitem_slice(indexer, value)

if isinstance(key, DataFrame) or getattr(key, "ndim", None) == 2:
Expand Down
8 changes: 6 additions & 2 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Hashable, List, Tuple, Union
from typing import TYPE_CHECKING, Hashable, List, Tuple, Union

import numpy as np

Expand Down Expand Up @@ -29,6 +29,10 @@
)
from pandas.core.indexes.api import Index

if TYPE_CHECKING:
from pandas import DataFrame


# "null slice"
_NS = slice(None, None)

Expand Down Expand Up @@ -2167,7 +2171,7 @@ def _tuplify(ndim: int, loc: Hashable) -> Tuple[Union[Hashable, slice], ...]:
return tuple(_tup)


def convert_to_index_sliceable(obj, key):
def convert_to_index_sliceable(obj: "DataFrame", key):
"""
If we are index sliceable, then return my slicer, otherwise return None.
"""
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/indexes/datetimes/test_partial_slicing.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,8 @@ def test_partial_slicing_dataframe(self):
tm.assert_series_equal(result, expected)

# Frame should return slice as well
result = df[ts_string]
with tm.assert_produces_warning(FutureWarning):
result = df[ts_string]
expected = df[theslice]
tm.assert_frame_equal(result, expected)

Expand Down
8 changes: 6 additions & 2 deletions pandas/tests/series/indexing/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,9 @@ def test_indexing():
expected.name = "A"

df = DataFrame(dict(A=ts))
result = df["2001"]["A"]
with tm.assert_produces_warning(FutureWarning):
ser = df["2001"]
result = ser["A"]
tm.assert_series_equal(expected, result)

# setting
Expand All @@ -651,7 +653,9 @@ def test_indexing():

df.loc["2001", "A"] = 1

result = df["2001"]["A"]
with tm.assert_produces_warning(FutureWarning):
ser = df["2001"]
result = ser["A"]
tm.assert_series_equal(expected, result)

# GH3546 (not including times on the last day)
Expand Down