From f294eaee461e79c34b7e6bab85ba2a73383e261f Mon Sep 17 00:00:00 2001 From: Phillip Cloud Date: Mon, 13 May 2013 16:10:10 -0400 Subject: [PATCH] ENH: raise on fillna passed a list or tuple --- RELEASE.rst | 3 +++ doc/source/v0.11.1.txt | 3 +++ pandas/core/frame.py | 5 ++++- pandas/core/panel.py | 3 +++ pandas/core/series.py | 3 +++ pandas/tests/test_frame.py | 6 ++++++ pandas/tests/test_panel.py | 3 +++ pandas/tests/test_series.py | 5 +++++ 8 files changed, 30 insertions(+), 1 deletion(-) diff --git a/RELEASE.rst b/RELEASE.rst index 4085d350f3766..042b827617327 100644 --- a/RELEASE.rst +++ b/RELEASE.rst @@ -105,6 +105,8 @@ pandas 0.11.1 - Fixed bug with ``Panel.transpose`` argument aliases (GH3556_) - Fixed platform bug in ``PeriodIndex.take`` (GH3579_) - Fixed bug in reset_index with ``NaN`` in a multi-index (GH3586_) + - ``fillna`` methods now raise a ``TypeError`` when the ``value`` parameter + is a ``list`` or ``tuple``. .. _GH3164: https://github.com/pydata/pandas/issues/3164 .. _GH2786: https://github.com/pydata/pandas/issues/2786 @@ -146,6 +148,7 @@ pandas 0.11.1 .. _GH3493: https://github.com/pydata/pandas/issues/3493 .. _GH3579: https://github.com/pydata/pandas/issues/3579 .. _GH3556: https://github.com/pydata/pandas/issues/3556 +.. _GH3435: https://github.com/pydata/pandas/issues/3435 pandas 0.11.0 diff --git a/doc/source/v0.11.1.txt b/doc/source/v0.11.1.txt index 76565df8f593c..74818f9542cae 100644 --- a/doc/source/v0.11.1.txt +++ b/doc/source/v0.11.1.txt @@ -21,6 +21,8 @@ Enhancements an index with a different frequency than the existing, or attempting to append an index with a different name than the existing - support datelike columns with a timezone as data_columns (GH2852_) + - ``fillna`` methods now raise a ``TypeError`` if the ``value`` parameter is + a list or tuple. See the `full release notes `__ or issue tracker @@ -31,3 +33,4 @@ on GitHub for a complete list. .. _GH3477: https://github.com/pydata/pandas/issues/3477 .. _GH3492: https://github.com/pydata/pandas/issues/3492 .. _GH3499: https://github.com/pydata/pandas/issues/3499 +.. _GH3435: https://github.com/pydata/pandas/issues/3435 diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 3df95b27f8736..777749c6b35dc 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -3319,7 +3319,7 @@ def fillna(self, value=None, method=None, axis=0, inplace=False, value : scalar or dict Value to use to fill holes (e.g. 0), alternately a dict of values specifying which value to use for each column (columns not in the - dict will not be filled) + dict will not be filled). This value cannot be a list. axis : {0, 1}, default 0 0: fill column-by-column 1: fill row-by-row @@ -3341,6 +3341,9 @@ def fillna(self, value=None, method=None, axis=0, inplace=False, ------- filled : DataFrame """ + if isinstance(value, (list, tuple)): + raise TypeError('"value" parameter must be a scalar or dict, but ' + 'you passed a "{0}"'.format(type(value).__name__)) self._consolidate_inplace() axis = self._get_axis_number(axis) diff --git a/pandas/core/panel.py b/pandas/core/panel.py index 869bb31acad6b..44b62991cf7a3 100644 --- a/pandas/core/panel.py +++ b/pandas/core/panel.py @@ -1007,6 +1007,9 @@ def fillna(self, value=None, method=None): -------- DataFrame.reindex, DataFrame.asfreq """ + if isinstance(value, (list, tuple)): + raise TypeError('"value" parameter must be a scalar or dict, but ' + 'you passed a "{0}"'.format(type(value).__name__)) if value is None: if method is None: raise ValueError('must specify a fill method or value') diff --git a/pandas/core/series.py b/pandas/core/series.py index cebf2f4ef9d1f..d9aacf1b0b080 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -2729,6 +2729,9 @@ def fillna(self, value=None, method=None, inplace=False, ------- filled : Series """ + if isinstance(value, (list, tuple)): + raise TypeError('"value" parameter must be a scalar or dict, but ' + 'you passed a "{0}"'.format(type(value).__name__)) if not self._can_hold_na: return self.copy() if not inplace else None diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py index 7e7813e048bd1..ce284b6b72f24 100644 --- a/pandas/tests/test_frame.py +++ b/pandas/tests/test_frame.py @@ -6039,6 +6039,12 @@ def test_fillna_invalid_method(self): except ValueError, inst: self.assert_('ffil' in str(inst)) + def test_fillna_invalid_value(self): + # list + self.assertRaises(TypeError, self.frame.fillna, [1, 2]) + # tuple + self.assertRaises(TypeError, self.frame.fillna, (1, 2)) + def test_replace_inplace(self): self.tsframe['A'][:5] = nan self.tsframe['A'][-5:] = nan diff --git a/pandas/tests/test_panel.py b/pandas/tests/test_panel.py index 081af101b643b..3640025bbf95c 100644 --- a/pandas/tests/test_panel.py +++ b/pandas/tests/test_panel.py @@ -1099,6 +1099,9 @@ def test_fillna(self): self.assertRaises(ValueError, self.panel.fillna) self.assertRaises(ValueError, self.panel.fillna, 5, method='ffill') + self.assertRaises(TypeError, self.panel.fillna, [1, 2]) + self.assertRaises(TypeError, self.panel.fillna, (1, 2)) + def test_ffill_bfill(self): assert_panel_equal(self.panel.ffill(), self.panel.fillna(method='ffill')) diff --git a/pandas/tests/test_series.py b/pandas/tests/test_series.py index 6fbce9df753d8..915becec8d7ff 100644 --- a/pandas/tests/test_series.py +++ b/pandas/tests/test_series.py @@ -3822,6 +3822,11 @@ def test_fillna_int(self): s.fillna(method='ffill', inplace=True) assert_series_equal(s.fillna(method='ffill', inplace=False), s) + def test_fillna_raise(self): + s = Series(np.random.randint(-100, 100, 50)) + self.assertRaises(TypeError, s.fillna, [1, 2]) + self.assertRaises(TypeError, s.fillna, (1, 2)) + #------------------------------------------------------------------------------ # TimeSeries-specific