Skip to content

Commit ed90130

Browse files
committed
BUG: fillna bug in Series GH#54
1 parent f835f94 commit ed90130

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

RELEASE.rst

+3-2
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,9 @@ Release notes
103103
* Cython is now required to build `pandas` from a development branch. This was
104104
done to avoid continuing to check in cythonized C files into source
105105
control. Builds from released source distributions will not require Cython
106-
* Cython extension modules have been renamed and promoted from the `lib`
107-
subpackage to the top level, i.e.
106+
* Cython code has been moved up to a top level `pandas/src` directory. Cython
107+
extension modules have been renamed and promoted from the `lib` subpackage to
108+
the top level, i.e.
108109
* `pandas.lib.tseries` -> `pandas._tseries`
109110
* `pandas.lib.sparse` -> `pandas._sparse`
110111
* A `copy` argument has been added to the `DataFrame` constructor to avoid

pandas/core/series.py

+19-2
Original file line numberDiff line numberDiff line change
@@ -1062,8 +1062,25 @@ def fillna(self, value=None, method='pad'):
10621062
newSeries[isnull(newSeries)] = value
10631063
return newSeries
10641064
else: # Using reindex to pad / backfill
1065-
withoutna = remove_na(self)
1066-
return withoutna.reindex(self.index, method=method)
1065+
if method is None: # pragma: no cover
1066+
raise ValueError('must specify a fill method')
1067+
1068+
method = method.lower()
1069+
1070+
if method == 'ffill':
1071+
method = 'pad'
1072+
if method == 'bfill':
1073+
method = 'backfill'
1074+
1075+
mask = isnull(self.values)
1076+
1077+
if method == 'pad':
1078+
indexer = _tseries.get_pad_indexer(mask)
1079+
elif method == 'backfill':
1080+
indexer = _tseries.get_backfill_indexer(mask)
1081+
1082+
new_values = self.values.take(indexer)
1083+
return Series(new_values, index=self.index)
10671084

10681085
#-------------------------------------------------------------------------------
10691086
# Miscellaneous

pandas/tests/test_series.py

+10
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,16 @@ def test_fillna(self):
915915

916916
self.assert_(np.array_equal(ts.fillna(value=5), [0., 1., 5., 3., 4.]))
917917

918+
def test_fillna_bug(self):
919+
x = Series([nan, 1., nan, 3., nan],['z','a','b','c','d'])
920+
filled = x.fillna(method='ffill')
921+
expected = Series([nan, 1., 1., 3., 3.], x.index)
922+
assert_series_equal(filled, expected)
923+
924+
filled = x.fillna(method='bfill')
925+
expected = Series([1., 1., 3., 3., nan], x.index)
926+
assert_series_equal(filled, expected)
927+
918928
def test_asfreq(self):
919929
ts = Series([0., 1., 2.], index=[datetime(2009, 10, 30),
920930
datetime(2009, 11, 30),

0 commit comments

Comments
 (0)