File tree 2 files changed +34
-2
lines changed
2 files changed +34
-2
lines changed Original file line number Diff line number Diff line change @@ -2359,14 +2359,18 @@ def ftypes(self):
2359
2359
return Series (self ._data .get_ftypes (), index = self ._info_axis ,
2360
2360
dtype = np .object_ )
2361
2361
2362
- def as_blocks (self ):
2362
+ def as_blocks (self , copy = True ):
2363
2363
"""
2364
2364
Convert the frame to a dict of dtype -> Constructor Types that each has
2365
2365
a homogeneous dtype.
2366
2366
2367
2367
NOTE: the dtypes of the blocks WILL BE PRESERVED HERE (unlike in
2368
2368
as_matrix)
2369
2369
2370
+ Parameters
2371
+ ----------
2372
+ copy : boolean, default True
2373
+
2370
2374
Returns
2371
2375
-------
2372
2376
values : a dict of dtype -> Constructor Types
@@ -2381,7 +2385,7 @@ def as_blocks(self):
2381
2385
for dtype , blocks in bd .items ():
2382
2386
# Must combine even after consolidation, because there may be
2383
2387
# 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 )
2385
2389
result [dtype ] = self ._constructor (combined ).__finalize__ (self )
2386
2390
2387
2391
return result
Original file line number Diff line number Diff line change @@ -6149,6 +6149,34 @@ def test_equals_different_blocks(self):
6149
6149
self.assertTrue(df0.equals(df1))
6150
6150
self.assertTrue(df1.equals(df0))
6151
6151
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
+
6152
6180
def test_to_csv_from_csv(self):
6153
6181
6154
6182
pname = '__tmp_to_csv_from_csv__'
You can’t perform that action at this time.
0 commit comments