Skip to content

Commit

Permalink
REF: consolidate NDFrame._iset_item, _set_item with DataFrame methods (
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored Dec 10, 2020
1 parent 2ff1370 commit d932752
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
19 changes: 15 additions & 4 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3211,12 +3211,16 @@ def _setitem_frame(self, key, value):
self._check_setitem_copy()
self._where(-key, value, inplace=True)

def _iset_item(self, loc: int, value):
def _iset_item_mgr(self, loc: int, value) -> None:
self._mgr.iset(loc, value)
self._clear_item_cache()

def _iset_item(self, loc: int, value, broadcast: bool = False):

# technically _sanitize_column expects a label, not a position,
# but the behavior is the same as long as we pass broadcast=False
value = self._sanitize_column(loc, value, broadcast=False)
NDFrame._iset_item(self, loc, value)
value = self._sanitize_column(loc, value, broadcast=broadcast)
self._iset_item_mgr(loc, value)

# check if we are modifying a copy
# try to set first as we want an invalid
Expand All @@ -3235,7 +3239,14 @@ def _set_item(self, key, value):
ensure homogeneity.
"""
value = self._sanitize_column(key, value)
NDFrame._set_item(self, key, value)

try:
loc = self._info_axis.get_loc(key)
except KeyError:
# This item wasn't present, just insert at end
self._mgr.insert(len(self._info_axis), key, value)
else:
self._iset_item_mgr(loc, value)

# check if we are modifying a copy
# try to set first as we want an invalid
Expand Down
14 changes: 0 additions & 14 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3814,20 +3814,6 @@ def _slice(self: FrameOrSeries, slobj: slice, axis=0) -> FrameOrSeries:
result._set_is_copy(self, copy=is_copy)
return result

def _iset_item(self, loc: int, value) -> None:
self._mgr.iset(loc, value)
self._clear_item_cache()

def _set_item(self, key, value) -> None:
try:
loc = self._info_axis.get_loc(key)
except KeyError:
# This item wasn't present, just insert at end
self._mgr.insert(len(self._info_axis), key, value)
return

NDFrame._iset_item(self, loc, value)

@final
def _set_is_copy(self, ref, copy: bool_t = True) -> None:
if not copy:
Expand Down

0 comments on commit d932752

Please sign in to comment.