Skip to content

Commit 74fb42f

Browse files
committed
Merge pull request #4951 from jreback/fillna
CLN: removed Panel.fillna, replacing within core/generic.py/fillna
2 parents b5a1a18 + 6e82392 commit 74fb42f

File tree

4 files changed

+16
-63
lines changed

4 files changed

+16
-63
lines changed

doc/source/release.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,8 @@ See :ref:`Internal Refactoring<whatsnew_0130.refactoring>`
254254

255255
- ``swapaxes`` on a ``Panel`` with the same axes specified now return a copy
256256
- support attribute access for setting
257-
- filter supports same api as original ``DataFrame`` filter
257+
- ``filter`` supports same api as original ``DataFrame`` filter
258+
- ``fillna`` refactored to ``core/generic.py``, while > 3ndim is ``NotImplemented``
258259

259260
- Series now inherits from ``NDFrame`` rather than directly from ``ndarray``.
260261
There are several minor changes that affect the API.

pandas/core/generic.py

+12
Original file line numberDiff line numberDiff line change
@@ -1569,6 +1569,18 @@ def fillna(self, value=None, method=None, axis=0, inplace=False,
15691569

15701570
return result
15711571

1572+
# > 3d
1573+
if self.ndim > 3:
1574+
raise NotImplementedError('cannot fillna with a method for > 3dims')
1575+
1576+
# 3d
1577+
elif self.ndim == 3:
1578+
1579+
# fill in 2d chunks
1580+
result = dict([ (col,s.fillna(method=method, value=value)) for col, s in compat.iteritems(self) ])
1581+
return self._constructor.from_dict(result)
1582+
1583+
# 2d or less
15721584
method = com._clean_fill_method(method)
15731585
new_data = self._data.interpolate(method=method,
15741586
axis=axis,

pandas/core/panel.py

-48
Original file line numberDiff line numberDiff line change
@@ -750,54 +750,6 @@ def _combine_panel(self, other, func):
750750

751751
return self._constructor(result_values, items, major, minor)
752752

753-
def fillna(self, value=None, method=None):
754-
"""
755-
Fill NaN values using the specified method.
756-
757-
Member Series / TimeSeries are filled separately.
758-
759-
Parameters
760-
----------
761-
value : any kind (should be same type as array)
762-
Value to use to fill holes (e.g. 0)
763-
764-
method : {'backfill', 'bfill', 'pad', 'ffill', None}, default 'pad'
765-
Method to use for filling holes in reindexed Series
766-
767-
pad / ffill: propagate last valid observation forward to next valid
768-
backfill / bfill: use NEXT valid observation to fill gap
769-
770-
Returns
771-
-------
772-
y : DataFrame
773-
774-
See also
775-
--------
776-
DataFrame.reindex, DataFrame.asfreq
777-
"""
778-
if isinstance(value, (list, tuple)):
779-
raise TypeError('"value" parameter must be a scalar or dict, but '
780-
'you passed a "{0}"'.format(type(value).__name__))
781-
if value is None:
782-
if method is None:
783-
raise ValueError('must specify a fill method or value')
784-
result = {}
785-
for col, s in compat.iteritems(self):
786-
result[col] = s.fillna(method=method, value=value)
787-
788-
return self._constructor.from_dict(result)
789-
else:
790-
if method is not None:
791-
raise ValueError('cannot specify both a fill method and value')
792-
new_data = self._data.fillna(value)
793-
return self._constructor(new_data)
794-
795-
def ffill(self):
796-
return self.fillna(method='ffill')
797-
798-
def bfill(self):
799-
return self.fillna(method='bfill')
800-
801753
def major_xs(self, key, copy=True):
802754
"""
803755
Return slice of panel along major axis

pandas/tests/test_panel4d.py

+2-14
Original file line numberDiff line numberDiff line change
@@ -849,23 +849,11 @@ def test_sort_index(self):
849849
# assert_panel_equal(sorted_panel, self.panel)
850850

851851
def test_fillna(self):
852+
self.assert_(not np.isfinite(self.panel4d.values).all())
852853
filled = self.panel4d.fillna(0)
853854
self.assert_(np.isfinite(filled.values).all())
854855

855-
filled = self.panel4d.fillna(method='backfill')
856-
assert_panel_equal(filled['l1'],
857-
self.panel4d['l1'].fillna(method='backfill'))
858-
859-
panel4d = self.panel4d.copy()
860-
panel4d['str'] = 'foo'
861-
862-
filled = panel4d.fillna(method='backfill')
863-
assert_panel_equal(filled['l1'],
864-
panel4d['l1'].fillna(method='backfill'))
865-
866-
empty = self.panel4d.reindex(labels=[])
867-
filled = empty.fillna(0)
868-
assert_panel4d_equal(filled, empty)
856+
self.assertRaises(NotImplementedError, self.panel4d.fillna, method='pad')
869857

870858
def test_swapaxes(self):
871859
result = self.panel4d.swapaxes('labels', 'items')

0 commit comments

Comments
 (0)