Skip to content

DEPR: enforce indexing deprecations #49511

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

Merged
merged 7 commits into from
Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
DEPR: enforce deprecation of string indexing on DataFrame rows
  • Loading branch information
jbrockmendel committed Nov 3, 2022
commit 5cebd1fc46f93d0cecea81fdab2c7305c8a389fb
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ Removal of prior version deprecations/changes
- Enforced :meth:`Rolling.count` with ``min_periods=None`` to default to the size of the window (:issue:`31302`)
- Renamed ``fname`` to ``path`` in :meth:`DataFrame.to_parquet`, :meth:`DataFrame.to_stata` and :meth:`DataFrame.to_feather` (:issue:`30338`)
- Enforced disallowing indexing a :class:`Series` with a single item list with a slice (e.g. ``ser[[slice(0, 2)]]``). Either convert the list to tuple, or pass the slice directly instead (:issue:`31333`)
- Changed behavior indexing on a :class:`DataFrame` with a :class:`DatetimeIndex` index using a string indexer, previously this operated as a slice on rows, now it operates like any other column key; use ``frame.loc[key]`` for the old behavior (:issue:`36179`)
- Enforced the ``display.max_colwidth`` option to not accept negative integers (:issue:`31569`)
- Removed the ``display.column_space`` option in favor of ``df.to_string(col_space=...)`` (:issue:`47280`)
- Removed the deprecated method ``mad`` from pandas classes (:issue:`11787`)
Expand Down
18 changes: 8 additions & 10 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@
from pandas.core.indexing import (
check_bool_indexer,
check_deprecated_indexers,
convert_to_index_sliceable,
)
from pandas.core.internals import (
ArrayManager,
Expand Down Expand Up @@ -3723,17 +3722,18 @@ def __getitem__(self, key):
elif is_mi and self.columns.is_unique and key in self.columns:
return self._getitem_multilevel(key)
# Do we have a slicer (on rows)?
indexer = convert_to_index_sliceable(self, key)
if indexer is not None:
if isinstance(key, slice):
indexer = self.index._convert_slice_indexer(
key, kind="getitem", is_frame=True
)
if isinstance(indexer, np.ndarray):
# reachable with DatetimeIndex
indexer = lib.maybe_indices_to_slice(
indexer.astype(np.intp, copy=False), len(self)
)
if isinstance(indexer, np.ndarray):
# GH#43223 If we can not convert, use take
return self.take(indexer, axis=0)
# either we have a slice or we have a string that can be converted
# to a slice for partial-string date indexing
return self._slice(indexer, axis=0)

# Do we have a (boolean) DataFrame?
Expand Down Expand Up @@ -3903,11 +3903,9 @@ def __setitem__(self, key, value):
key = com.apply_if_callable(key, self)

# see if we can slice the rows
indexer = convert_to_index_sliceable(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
return self._setitem_slice(indexer, value)
if isinstance(key, slice):
slc = self.index._convert_slice_indexer(key, kind="getitem", is_frame=True)
return self._setitem_slice(slc, value)

if isinstance(key, DataFrame) or getattr(key, "ndim", None) == 2:
self._setitem_frame(key, value)
Expand Down
34 changes: 0 additions & 34 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2493,40 +2493,6 @@ def _tupleize_axis_indexer(ndim: int, axis: AxisInt, key) -> tuple:
return tuple(new_key)


def convert_to_index_sliceable(obj: DataFrame, key):
"""
If we are index sliceable, then return my slicer, otherwise return None.
"""
idx = obj.index
if isinstance(key, slice):
return idx._convert_slice_indexer(key, kind="getitem", is_frame=True)

elif isinstance(key, str):

# we are an actual column
if key in obj.columns:
return None

# We might have a datetimelike string that we can translate to a
# slice here via partial string indexing
if idx._supports_partial_string_indexing:
try:
res = idx._get_string_slice(str(key))
warnings.warn(
"Indexing a DataFrame with a datetimelike index using a single "
"string to slice the rows, like `frame[string]`, is deprecated "
"and will be removed in a future version. Use `frame.loc[string]` "
"instead.",
FutureWarning,
stacklevel=find_stack_level(),
)
return res
except (KeyError, ValueError, NotImplementedError):
return None

return None


def check_bool_indexer(index: Index, key) -> np.ndarray:
"""
Check if key is a valid boolean indexer for an object with such index and
Expand Down
10 changes: 4 additions & 6 deletions pandas/tests/indexes/datetimes/test_partial_slicing.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,12 +295,10 @@ def test_partial_slicing_dataframe(self):
expected = df["a"][theslice]
tm.assert_series_equal(result, expected)

# Frame should return slice as well
with tm.assert_produces_warning(FutureWarning):
# GH#36179 deprecated this indexing
result = df[ts_string]
expected = df[theslice]
tm.assert_frame_equal(result, expected)
# pre-2.0 df[ts_string] was overloaded to interpret this
# as slicing along index
with pytest.raises(KeyError, match=ts_string):
df[ts_string]

# Timestamp with resolution more precise than index
# Compatible with existing key
Expand Down
15 changes: 7 additions & 8 deletions pandas/tests/series/indexing/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,10 +426,11 @@ def test_indexing():
expected.name = "A"

df = DataFrame({"A": ts})
with tm.assert_produces_warning(FutureWarning):
# GH#36179 string indexing on rows for DataFrame deprecated
result = df["2001"]["A"]
tm.assert_series_equal(expected, result)

# GH#36179 pre-2.0 df["2001"] operated as slicing on rows. in 2.0 it behaves
# like any other key, so raises
with pytest.raises(KeyError, match="2001"):
df["2001"]

# setting
ts["2001"] = 1
Expand All @@ -438,10 +439,8 @@ def test_indexing():

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

with tm.assert_produces_warning(FutureWarning):
# GH#36179 string indexing on rows for DataFrame deprecated
result = df["2001"]["A"]
tm.assert_series_equal(expected, result)
with pytest.raises(KeyError, match="2001"):
df["2001"]


def test_getitem_str_month_with_datetimeindex():
Expand Down