Skip to content

Commit 8eabc4e

Browse files
tzinckgrafjreback
authored andcommitted
ENH: allow as_blocks to take a copy argument (pandas-dev#9607)
1 parent 25fc49d commit 8eabc4e

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

pandas/core/generic.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2359,14 +2359,18 @@ def ftypes(self):
23592359
return Series(self._data.get_ftypes(), index=self._info_axis,
23602360
dtype=np.object_)
23612361

2362-
def as_blocks(self):
2362+
def as_blocks(self, copy=True):
23632363
"""
23642364
Convert the frame to a dict of dtype -> Constructor Types that each has
23652365
a homogeneous dtype.
23662366
23672367
NOTE: the dtypes of the blocks WILL BE PRESERVED HERE (unlike in
23682368
as_matrix)
23692369
2370+
Parameters
2371+
----------
2372+
copy : boolean, default True
2373+
23702374
Returns
23712375
-------
23722376
values : a dict of dtype -> Constructor Types
@@ -2381,7 +2385,7 @@ def as_blocks(self):
23812385
for dtype, blocks in bd.items():
23822386
# Must combine even after consolidation, because there may be
23832387
# sparse items which are never consolidated into one block.
2384-
combined = self._data.combine(blocks, copy=True)
2388+
combined = self._data.combine(blocks, copy=copy)
23852389
result[dtype] = self._constructor(combined).__finalize__(self)
23862390

23872391
return result

pandas/tests/test_frame.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6149,6 +6149,34 @@ def test_equals_different_blocks(self):
61496149
self.assertTrue(df0.equals(df1))
61506150
self.assertTrue(df1.equals(df0))
61516151

6152+
def test_copy_blocks(self):
6153+
# API/ENH 9607
6154+
df = DataFrame(self.frame, copy=True)
6155+
column = df.columns[0]
6156+
6157+
# use the default copy=True, change a column
6158+
blocks = df.as_blocks()
6159+
for dtype, _df in blocks.items():
6160+
if column in _df:
6161+
_df.ix[:, column] = _df[column] + 1
6162+
6163+
# make sure we did not change the original DataFrame
6164+
self.assertFalse(_df[column].equals(df[column]))
6165+
6166+
def test_no_copy_blocks(self):
6167+
# API/ENH 9607
6168+
df = DataFrame(self.frame, copy=True)
6169+
column = df.columns[0]
6170+
6171+
# use the copy=False, change a column
6172+
blocks = df.as_blocks(copy=False)
6173+
for dtype, _df in blocks.items():
6174+
if column in _df:
6175+
_df.ix[:, column] = _df[column] + 1
6176+
6177+
# make sure we did change the original DataFrame
6178+
self.assertTrue(_df[column].equals(df[column]))
6179+
61526180
def test_to_csv_from_csv(self):
61536181

61546182
pname = '__tmp_to_csv_from_csv__'

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy