Skip to content

Commit e426844

Browse files
committed
CLN: clean up block validation for empty blocks
1 parent c738317 commit e426844

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

pandas/core/internals.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3191,13 +3191,6 @@ def reindex_items(self, new_items, indexer=None, copy=True,
31913191
new_axes = [new_items] + self.axes[1:]
31923192

31933193
# could have so me pathological (MultiIndex) issues here
3194-
def _valid_blocks(newb):
3195-
if newb is None:
3196-
return []
3197-
if not isinstance(newb, list):
3198-
newb = [ newb ]
3199-
return [ b for b in newb if len(b.items) > 0 ]
3200-
32013194
new_blocks = []
32023195
if indexer is None:
32033196
for blk in self.blocks:
@@ -3423,7 +3416,11 @@ def __init__(self, block, axis, do_integrity_check=False, fastpath=True):
34233416
if fastpath:
34243417
self.axes = [axis]
34253418
if isinstance(block, list):
3426-
if len(block) != 1:
3419+
3420+
# empty block
3421+
if len(block) == 0:
3422+
block = [np.array([])]
3423+
elif len(block) != 1:
34273424
raise ValueError('Cannot create SingleBlockManager with '
34283425
'more than 1 block')
34293426
block = block[0]
@@ -3887,6 +3884,13 @@ def _consolidate(blocks, items):
38873884
return new_blocks
38883885

38893886

3887+
def _valid_blocks(newb):
3888+
if newb is None:
3889+
return []
3890+
if not isinstance(newb, list):
3891+
newb = [ newb ]
3892+
return [ b for b in newb if len(b.items) > 0 ]
3893+
38903894
def _merge_blocks(blocks, items, dtype=None, _can_consolidate=True):
38913895
if len(blocks) == 1:
38923896
return blocks[0]

pandas/tests/test_series.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2783,6 +2783,12 @@ def test_fillna(self):
27832783
result = s1.fillna(Series({ 0 : 1, 1 : 1},index=[4,5]))
27842784
assert_series_equal(result,s1)
27852785

2786+
s1 = Series([0, 1, 2], list('abc'))
2787+
s2 = Series([0, np.nan, 2], list('bac'))
2788+
result = s2.fillna(s1)
2789+
expected = Series([0,0,2.], list('bac'))
2790+
assert_series_equal(result,expected)
2791+
27862792
def test_fillna_bug(self):
27872793
x = Series([nan, 1., nan, 3., nan], ['z', 'a', 'b', 'c', 'd'])
27882794
filled = x.fillna(method='ffill')

0 commit comments

Comments
 (0)