Skip to content

Commit 8897021

Browse files
committed
Merge pull request #3585 from cpcloud/fillna-raise-on-value-is-list-3435
raise on fillna passed a list or tuple
2 parents e6cdd46 + f294eae commit 8897021

File tree

8 files changed

+30
-1
lines changed

8 files changed

+30
-1
lines changed

RELEASE.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ pandas 0.11.1
106106
- Fixed platform bug in ``PeriodIndex.take`` (GH3579_)
107107
- Fixed bud in incorrect conversion of datetime64[ns] in ``combine_first`` (GH3593_)
108108
- Fixed bug in reset_index with ``NaN`` in a multi-index (GH3586_)
109+
- ``fillna`` methods now raise a ``TypeError`` when the ``value`` parameter
110+
is a ``list`` or ``tuple``.
109111

110112
.. _GH3164: https://github.com/pydata/pandas/issues/3164
111113
.. _GH2786: https://github.com/pydata/pandas/issues/2786
@@ -148,6 +150,7 @@ pandas 0.11.1
148150
.. _GH3579: https://github.com/pydata/pandas/issues/3579
149151
.. _GH3593: https://github.com/pydata/pandas/issues/3593
150152
.. _GH3556: https://github.com/pydata/pandas/issues/3556
153+
.. _GH3435: https://github.com/pydata/pandas/issues/3435
151154

152155

153156
pandas 0.11.0

doc/source/v0.11.1.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ Enhancements
2121
an index with a different frequency than the existing, or attempting
2222
to append an index with a different name than the existing
2323
- support datelike columns with a timezone as data_columns (GH2852_)
24+
- ``fillna`` methods now raise a ``TypeError`` if the ``value`` parameter is
25+
a list or tuple.
2426

2527
See the `full release notes
2628
<https://github.com/pydata/pandas/blob/master/RELEASE.rst>`__ or issue tracker
@@ -31,3 +33,4 @@ on GitHub for a complete list.
3133
.. _GH3477: https://github.com/pydata/pandas/issues/3477
3234
.. _GH3492: https://github.com/pydata/pandas/issues/3492
3335
.. _GH3499: https://github.com/pydata/pandas/issues/3499
36+
.. _GH3435: https://github.com/pydata/pandas/issues/3435

pandas/core/frame.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3319,7 +3319,7 @@ def fillna(self, value=None, method=None, axis=0, inplace=False,
33193319
value : scalar or dict
33203320
Value to use to fill holes (e.g. 0), alternately a dict of values
33213321
specifying which value to use for each column (columns not in the
3322-
dict will not be filled)
3322+
dict will not be filled). This value cannot be a list.
33233323
axis : {0, 1}, default 0
33243324
0: fill column-by-column
33253325
1: fill row-by-row
@@ -3341,6 +3341,9 @@ def fillna(self, value=None, method=None, axis=0, inplace=False,
33413341
-------
33423342
filled : DataFrame
33433343
"""
3344+
if isinstance(value, (list, tuple)):
3345+
raise TypeError('"value" parameter must be a scalar or dict, but '
3346+
'you passed a "{0}"'.format(type(value).__name__))
33443347
self._consolidate_inplace()
33453348

33463349
axis = self._get_axis_number(axis)

pandas/core/panel.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,6 +1007,9 @@ def fillna(self, value=None, method=None):
10071007
--------
10081008
DataFrame.reindex, DataFrame.asfreq
10091009
"""
1010+
if isinstance(value, (list, tuple)):
1011+
raise TypeError('"value" parameter must be a scalar or dict, but '
1012+
'you passed a "{0}"'.format(type(value).__name__))
10101013
if value is None:
10111014
if method is None:
10121015
raise ValueError('must specify a fill method or value')

pandas/core/series.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2734,6 +2734,9 @@ def fillna(self, value=None, method=None, inplace=False,
27342734
-------
27352735
filled : Series
27362736
"""
2737+
if isinstance(value, (list, tuple)):
2738+
raise TypeError('"value" parameter must be a scalar or dict, but '
2739+
'you passed a "{0}"'.format(type(value).__name__))
27372740
if not self._can_hold_na:
27382741
return self.copy() if not inplace else None
27392742

pandas/tests/test_frame.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6039,6 +6039,12 @@ def test_fillna_invalid_method(self):
60396039
except ValueError, inst:
60406040
self.assert_('ffil' in str(inst))
60416041

6042+
def test_fillna_invalid_value(self):
6043+
# list
6044+
self.assertRaises(TypeError, self.frame.fillna, [1, 2])
6045+
# tuple
6046+
self.assertRaises(TypeError, self.frame.fillna, (1, 2))
6047+
60426048
def test_replace_inplace(self):
60436049
self.tsframe['A'][:5] = nan
60446050
self.tsframe['A'][-5:] = nan

pandas/tests/test_panel.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,6 +1099,9 @@ def test_fillna(self):
10991099
self.assertRaises(ValueError, self.panel.fillna)
11001100
self.assertRaises(ValueError, self.panel.fillna, 5, method='ffill')
11011101

1102+
self.assertRaises(TypeError, self.panel.fillna, [1, 2])
1103+
self.assertRaises(TypeError, self.panel.fillna, (1, 2))
1104+
11021105
def test_ffill_bfill(self):
11031106
assert_panel_equal(self.panel.ffill(),
11041107
self.panel.fillna(method='ffill'))

pandas/tests/test_series.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3822,6 +3822,11 @@ def test_fillna_int(self):
38223822
s.fillna(method='ffill', inplace=True)
38233823
assert_series_equal(s.fillna(method='ffill', inplace=False), s)
38243824

3825+
def test_fillna_raise(self):
3826+
s = Series(np.random.randint(-100, 100, 50))
3827+
self.assertRaises(TypeError, s.fillna, [1, 2])
3828+
self.assertRaises(TypeError, s.fillna, (1, 2))
3829+
38253830
#------------------------------------------------------------------------------
38263831
# TimeSeries-specific
38273832

0 commit comments

Comments
 (0)