Skip to content

Commit

Permalink
add inplace keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
jorisvandenbossche committed May 23, 2022
1 parent d20b0cb commit 453eaba
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
5 changes: 3 additions & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3932,8 +3932,9 @@ def _set_value(
# error: Argument 2 to "column_setitem" of "BlockManager" has
# incompatible type "Union[Hashable, Sequence[Hashable]]";
# expected "Union[int, slice, ndarray[Any, Any]]"
self._mgr.column_setitem(icol, iindex, value) # type: ignore[arg-type]
self._clear_item_cache()
self._mgr.column_setitem( # type: ignore[arg-type]
icol, iindex, value, inplace=True
)

except (KeyError, TypeError, ValueError, LossySetitemError):
# set using a non-recursive method & reset the cache
Expand Down
13 changes: 9 additions & 4 deletions pandas/core/internals/array_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,9 @@ def iset(
self.arrays[mgr_idx] = value_arr
return

def column_setitem(self, loc: int, idx: int | slice | np.ndarray, value) -> None:
def column_setitem(
self, loc: int, idx: int | slice | np.ndarray, value, inplace: bool = False
) -> None:
"""
Set values ("setitem") into a single column (not setting the full column).
Expand All @@ -879,9 +881,12 @@ def column_setitem(self, loc: int, idx: int | slice | np.ndarray, value) -> None
arr = self.arrays[loc]
# create temporary SingleArrayManager without ref to use setitem implementation
mgr = SingleArrayManager([arr], [self._axes[0]])
new_mgr = mgr.setitem((idx,), value)
# update existing ArrayManager in-place
self.arrays[loc] = new_mgr.arrays[0]
if inplace:
mgr.setitem_inplace(idx, value)
else:
new_mgr = mgr.setitem((idx,), value)
# update existing ArrayManager in-place
self.arrays[loc] = new_mgr.arrays[0]

def insert(self, loc: int, item: Hashable, value: ArrayLike) -> None:
"""
Expand Down
11 changes: 8 additions & 3 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1188,16 +1188,21 @@ def _iset_single(
self.blocks = new_blocks
return

def column_setitem(self, loc: int, idx: int | slice | np.ndarray, value) -> None:
def column_setitem(
self, loc: int, idx: int | slice | np.ndarray, value, inplace: bool = False
) -> None:
"""
Set values ("setitem") into a single column (not setting the full column).
This is a method on the BlockManager level, to avoid creating an
intermediate Series at the DataFrame level (`s = df[loc]; s[idx] = value`)
"""
col_mgr = self.iget(loc)
new_mgr = col_mgr.setitem((idx,), value)
self.iset(loc, new_mgr._block.values, inplace=True)
if inplace:
col_mgr.setitem_inplace(idx, value)
else:
new_mgr = col_mgr.setitem((idx,), value)
self.iset(loc, new_mgr._block.values, inplace=True)

def insert(self, loc: int, item: Hashable, value: ArrayLike) -> None:
"""
Expand Down

0 comments on commit 453eaba

Please sign in to comment.