Skip to content

Commit a885d67

Browse files
PDEP-8 inplace: return self for inplace=True in methods that will keep the keyword (#63195)
1 parent 53e778b commit a885d67

File tree

15 files changed

+252
-531
lines changed

15 files changed

+252
-531
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,10 @@ Other API changes
753753
- Arithmetic operations between a :class:`Series`, :class:`Index`, or :class:`ExtensionArray` with a ``list`` now consistently wrap that list with an array equivalent to ``Series(my_list).array``. To do any other kind of type inference or casting, do so explicitly before operating (:issue:`62552`)
754754
- Comparison operations between :class:`Index` and :class:`Series` now consistently return :class:`Series` regardless of which object is on the left or right (:issue:`36759`)
755755
- Numpy functions like ``np.isinf`` that return a bool dtype when called on a :class:`Index` object now return a bool-dtype :class:`Index` instead of ``np.ndarray`` (:issue:`52676`)
756+
- Methods that can operate in-place (:meth:`~DataFrame.replace`, :meth:`~DataFrame.fillna`,
757+
:meth:`~DataFrame.ffill`, :meth:`~DataFrame.bfill`, :meth:`~DataFrame.interpolate`,
758+
:meth:`~DataFrame.where`, :meth:`~DataFrame.mask`, :meth:`~DataFrame.clip`) now return
759+
the modified DataFrame or Series (``self``) instead of ``None`` when ``inplace=True`` (:issue:`63207`)
756760

757761
.. ---------------------------------------------------------------------------
758762
.. _whatsnew_300.deprecations:

pandas/core/frame.py

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6109,19 +6109,9 @@ def pop(self, item: Hashable) -> Series:
61096109
"""
61106110
return super().pop(item=item)
61116111

6112-
@overload
6113-
def _replace_columnwise(
6114-
self, mapping: dict[Hashable, tuple[Any, Any]], inplace: Literal[True], regex
6115-
) -> None: ...
6116-
6117-
@overload
6118-
def _replace_columnwise(
6119-
self, mapping: dict[Hashable, tuple[Any, Any]], inplace: Literal[False], regex
6120-
) -> Self: ...
6121-
61226112
def _replace_columnwise(
61236113
self, mapping: dict[Hashable, tuple[Any, Any]], inplace: bool, regex
6124-
) -> Self | None:
6114+
) -> Self:
61256115
"""
61266116
Dispatch to Series.replace column-wise.
61276117
@@ -6134,7 +6124,7 @@ def _replace_columnwise(
61346124
61356125
Returns
61366126
-------
6137-
DataFrame or None
6127+
DataFrame
61386128
"""
61396129
# Operate column-wise
61406130
res = self if inplace else self.copy(deep=False)
@@ -6149,9 +6139,7 @@ def _replace_columnwise(
61496139

61506140
res._iset_item(i, newobj, inplace=inplace)
61516141

6152-
if inplace:
6153-
return None
6154-
return res.__finalize__(self)
6142+
return res if inplace else res.__finalize__(self)
61556143

61566144
@doc(NDFrame.shift, klass=_shared_doc_kwargs["klass"])
61576145
def shift(

0 commit comments

Comments
 (0)