Skip to content
Prev Previous commit
Next Next commit
Remove redundant copying
  • Loading branch information
Nick Eubank committed Jan 13, 2016
commit 00869cacadbcd27d3032a5807eba627da3241be8
18 changes: 4 additions & 14 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1219,7 +1219,7 @@ def _slice(self, slobj, axis=0, kind=None):
def _set_item(self, key, value):

# If children are views, reset to copies before setting.
self._convert_views_to_copies()
self._execute_copy_on_write()

self._data.set(key, value)
self._clear_item_cache()
Expand All @@ -1233,23 +1233,13 @@ def _set_is_copy(self, ref=None, copy=True):
else:
self.is_copy = None

def _convert_views_to_copies(self):
def _execute_copy_on_write(self):

# Don't set on views.
if self._is_view and not self._is_column_view:
if (self._is_view and not self._is_column_view) or len(self._children) is not 0:
self._data = self._data.copy()


# Before setting values, make sure children converted to copies.
for child in self._children.valuerefs():

# Make sure children of children converted.
child()._convert_views_to_copies()

if child()._is_view and not child()._is_column_view:
child()._data = child()._data.copy()
self._children = weakref.WeakValueDictionary()

self._children = weakref.WeakValueDictionary()

def _add_to_children(self, view_to_append):
self._children[id(view_to_append)] = view_to_append
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def _get_setitem_indexer(self, key):

def __setitem__(self, key, value):
# Make sure changes don't propagate to children
self.obj._convert_views_to_copies()
self.obj._execute_copy_on_write()

indexer = self._get_setitem_indexer(key)
self._setitem_with_indexer(indexer, value)
Expand Down
2 changes: 0 additions & 2 deletions pandas/tests/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1745,7 +1745,6 @@ def test_copy_on_write(self):

for v in views.keys():
tm.assert_frame_equal(views[v], copies[v])
self.assertFalse(views[v]._is_view)

# Test different forms of value setting
# all trigger conversions
Expand All @@ -1768,7 +1767,6 @@ def test_copy_on_write(self):

for v in views.keys():
tm.assert_frame_equal(views[v], copies[v])
self.assertFalse(views[v]._is_view)

########
# No Backward Propogation
Expand Down