Skip to content

Commit 986bda2

Browse files
committed
Merge pull request #5705 from jreback/series_fillna
BUG: Bug in fillna with Series and a passed series/dict (GH5703)
2 parents 6d78d52 + e426844 commit 986bda2

File tree

4 files changed

+65
-10
lines changed

4 files changed

+65
-10
lines changed

doc/source/release.rst

+1
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,7 @@ Bug Fixes
822822
- Bug in groupby returning non-consistent types when user function returns a ``None``, (:issue:`5592`)
823823
- Work around regression in numpy 1.7.0 which erroneously raises IndexError from ``ndarray.item`` (:issue:`5666`)
824824
- Bug in repeated indexing of object with resultant non-unique index (:issue:`5678`)
825+
- Bug in fillna with Series and a passed series/dict (:issue:`5703`)
825826

826827
pandas 0.12.0
827828
-------------

pandas/core/generic.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -1905,7 +1905,16 @@ def fillna(self, value=None, method=None, axis=0, inplace=False,
19051905

19061906
if len(self._get_axis(axis)) == 0:
19071907
return self
1908-
if isinstance(value, (dict, com.ABCSeries)):
1908+
1909+
if self.ndim == 1 and value is not None:
1910+
if isinstance(value, (dict, com.ABCSeries)):
1911+
from pandas import Series
1912+
value = Series(value)
1913+
1914+
new_data = self._data.fillna(value, inplace=inplace,
1915+
downcast=downcast)
1916+
1917+
elif isinstance(value, (dict, com.ABCSeries)):
19091918
if axis == 1:
19101919
raise NotImplementedError('Currently only can fill '
19111920
'with dict/Series column '

pandas/core/internals.py

+25-9
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ def putmask(self, mask, new, align=True, inplace=False):
664664

665665
# if we are passed a scalar None, convert it here
666666
if not is_list_like(new) and isnull(new):
667-
new = np.nan
667+
new = self.fill_value
668668

669669
if self._can_hold_element(new):
670670
new = self._try_cast(new)
@@ -830,7 +830,7 @@ def _interpolate(self, method=None, index=None, values=None,
830830
data = data.astype(np.float64)
831831

832832
if fill_value is None:
833-
fill_value = np.nan
833+
fill_value = self.fill_value
834834

835835
if method in ('krogh', 'piecewise_polynomial', 'pchip'):
836836
if not index.is_monotonic:
@@ -1196,6 +1196,10 @@ class TimeDeltaBlock(IntBlock):
11961196
_can_hold_na = True
11971197
is_numeric = False
11981198

1199+
@property
1200+
def fill_value(self):
1201+
return tslib.iNaT
1202+
11991203
def _try_fill(self, value):
12001204
""" if we are a NaT, return the actual fill value """
12011205
if isinstance(value, type(tslib.NaT)) or isnull(value):
@@ -1532,6 +1536,10 @@ def _try_coerce_result(self, result):
15321536
result = lib.Timestamp(result)
15331537
return result
15341538

1539+
@property
1540+
def fill_value(self):
1541+
return tslib.iNaT
1542+
15351543
def _try_fill(self, value):
15361544
""" if we are a NaT, return the actual fill value """
15371545
if isinstance(value, type(tslib.NaT)) or isnull(value):
@@ -3190,18 +3198,15 @@ def reindex_items(self, new_items, indexer=None, copy=True,
31903198
blk = blk.reindex_items_from(new_items)
31913199
else:
31923200
blk.ref_items = new_items
3193-
if blk is not None:
3194-
new_blocks.append(blk)
3201+
new_blocks.extend(_valid_blocks(blk))
31953202
else:
31963203

31973204
# unique
31983205
if self.axes[0].is_unique and new_items.is_unique:
31993206

32003207
for block in self.blocks:
3201-
3202-
newb = block.reindex_items_from(new_items, copy=copy)
3203-
if newb is not None and len(newb.items) > 0:
3204-
new_blocks.append(newb)
3208+
blk = block.reindex_items_from(new_items, copy=copy)
3209+
new_blocks.extend(_valid_blocks(blk))
32053210

32063211
# non-unique
32073212
else:
@@ -3411,7 +3416,11 @@ def __init__(self, block, axis, do_integrity_check=False, fastpath=True):
34113416
if fastpath:
34123417
self.axes = [axis]
34133418
if isinstance(block, list):
3414-
if len(block) != 1:
3419+
3420+
# empty block
3421+
if len(block) == 0:
3422+
block = [np.array([])]
3423+
elif len(block) != 1:
34153424
raise ValueError('Cannot create SingleBlockManager with '
34163425
'more than 1 block')
34173426
block = block[0]
@@ -3875,6 +3884,13 @@ def _consolidate(blocks, items):
38753884
return new_blocks
38763885

38773886

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+
38783894
def _merge_blocks(blocks, items, dtype=None, _can_consolidate=True):
38793895
if len(blocks) == 1:
38803896
return blocks[0]

pandas/tests/test_series.py

+29
Original file line numberDiff line numberDiff line change
@@ -2760,6 +2760,35 @@ def test_fillna(self):
27602760
self.assertRaises(ValueError, ts.fillna)
27612761
self.assertRaises(ValueError, self.ts.fillna, value=0, method='ffill')
27622762

2763+
# GH 5703
2764+
s1 = Series([np.nan])
2765+
s2 = Series([1])
2766+
result = s1.fillna(s2)
2767+
expected = Series([1.])
2768+
assert_series_equal(result,expected)
2769+
result = s1.fillna({})
2770+
assert_series_equal(result,s1)
2771+
result = s1.fillna(Series(()))
2772+
assert_series_equal(result,s1)
2773+
result = s2.fillna(s1)
2774+
assert_series_equal(result,s2)
2775+
result = s1.fillna({ 0 : 1})
2776+
assert_series_equal(result,expected)
2777+
result = s1.fillna({ 1 : 1})
2778+
assert_series_equal(result,Series([np.nan]))
2779+
result = s1.fillna({ 0 : 1, 1 : 1})
2780+
assert_series_equal(result,expected)
2781+
result = s1.fillna(Series({ 0 : 1, 1 : 1}))
2782+
assert_series_equal(result,expected)
2783+
result = s1.fillna(Series({ 0 : 1, 1 : 1},index=[4,5]))
2784+
assert_series_equal(result,s1)
2785+
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+
27632792
def test_fillna_bug(self):
27642793
x = Series([nan, 1., nan, 3., nan], ['z', 'a', 'b', 'c', 'd'])
27652794
filled = x.fillna(method='ffill')

0 commit comments

Comments
 (0)