Skip to content

Commit 1c438e8

Browse files
committed
Merge pull request #6748 from immerrr/fix-as-blocks-for-sparse
BUG: fix NDFrame.as_blocks() for sparse containers
2 parents a44c304 + 1a62444 commit 1c438e8

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

doc/source/release.rst

+1
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ Bug Fixes
296296
- Bug in consistency of groupby aggregation when passing a custom function (:issue:`6715`)
297297
- Bug in resample when ``how=None`` resample freq is the same as the axis frequency (:issue:`5955`)
298298
- Bug in downcasting inference with empty arrays (:issue:`6733`)
299+
- Bug in ``obj.blocks`` on sparse containers dropping all but the last items of same for dtype (:issue:`6748`)
299300

300301
pandas 0.13.1
301302
-------------

pandas/core/generic.py

+12-6
Original file line numberDiff line numberDiff line change
@@ -2003,7 +2003,7 @@ def ftypes(self):
20032003
return Series(self._data.get_ftypes(), index=self._info_axis,
20042004
dtype=np.object_)
20052005

2006-
def as_blocks(self, columns=None):
2006+
def as_blocks(self):
20072007
"""
20082008
Convert the frame to a dict of dtype -> Constructor Types that each has
20092009
a homogeneous dtype.
@@ -2025,12 +2025,18 @@ def as_blocks(self, columns=None):
20252025
"""
20262026
self._consolidate_inplace()
20272027

2028-
bd = dict()
2028+
bd = {}
20292029
for b in self._data.blocks:
2030-
b = b.reindex_items_from(columns or b.items)
2031-
bd[str(b.dtype)] = self._constructor(
2032-
BlockManager([b], [b.items, self.index])).__finalize__(self)
2033-
return bd
2030+
bd.setdefault(str(b.dtype), []).append(b)
2031+
2032+
result = {}
2033+
for dtype, blocks in bd.items():
2034+
# Must combine even after consolidation, because there may be
2035+
# sparse items which are never consolidated into one block.
2036+
combined = self._data.combine(blocks, copy=True)
2037+
result[dtype] = self._constructor(combined).__finalize__(self)
2038+
2039+
return result
20342040

20352041
@property
20362042
def blocks(self):

pandas/sparse/tests/test_sparse.py

+8
Original file line numberDiff line numberDiff line change
@@ -1515,6 +1515,14 @@ def test_sparse_pow_issue(self):
15151515

15161516
self.assertEqual(len(r2.sp_values), len(r1.sp_values))
15171517

1518+
def test_as_blocks(self):
1519+
df = SparseDataFrame({'A': [1.1, 3.3], 'B': [nan, -3.9]},
1520+
dtype='float64')
1521+
1522+
df_blocks = df.blocks
1523+
self.assertEqual(list(df_blocks.keys()), ['float64'])
1524+
assert_frame_equal(df_blocks['float64'], df)
1525+
15181526

15191527
def _dense_series_compare(s, f):
15201528
result = f(s)

0 commit comments

Comments
 (0)