diff --git a/RELEASE.rst b/RELEASE.rst index 3927d1e7b0122..2b1708855db49 100644 --- a/RELEASE.rst +++ b/RELEASE.rst @@ -220,6 +220,7 @@ pandas 0.11.0 to an *ordered* timeseries (GH2437_). - Fix implemented ``.xs`` when called with ``axes=1`` and a level parameter (GH2903_) - Timestamp now supports the class method fromordinal similar to datetimes (GH3042_) + - Fix issue with indexing a series with a boolean key and specifiying a 1-len list on the rhs (GH2745_) .. _GH622: https://github.com/pydata/pandas/issues/622 .. _GH797: https://github.com/pydata/pandas/issues/797 @@ -240,6 +241,7 @@ pandas 0.11.0 .. _GH3011: https://github.com/pydata/pandas/issues/3011 .. _GH2681: https://github.com/pydata/pandas/issues/2681 .. _GH2719: https://github.com/pydata/pandas/issues/2719 +.. _GH2745: https://github.com/pydata/pandas/issues/2745 .. _GH2746: https://github.com/pydata/pandas/issues/2746 .. _GH2747: https://github.com/pydata/pandas/issues/2747 .. _GH2751: https://github.com/pydata/pandas/issues/2751 diff --git a/pandas/core/common.py b/pandas/core/common.py index 332e38012fc25..586f36cbd0b0f 100644 --- a/pandas/core/common.py +++ b/pandas/core/common.py @@ -766,10 +766,6 @@ def changeit(): return r, True - new_dtype, fill_value = _maybe_promote(result.dtype,other) - if new_dtype != result.dtype: - return changeit() - try: np.putmask(result, mask, other) except: diff --git a/pandas/core/series.py b/pandas/core/series.py index 53793d503403e..6efa4026641c0 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -729,11 +729,19 @@ def where(self, cond, other=nan, inplace=False): if isinstance(other, Series): other = other.reindex(ser.index) + elif isinstance(other, (tuple,list)): + other = np.array(other) if len(other) != len(ser): - raise ValueError('Length of replacements must equal series length') + + # GH 2745 + # treat like a scalar + if len(other) == 1: + other = np.array(other[0]*len(ser)) + else: + raise ValueError('Length of replacements must equal series length') change = ser if inplace else None - result, changed = com._maybe_upcast_putmask(ser,~cond,other,change=change) + com._maybe_upcast_putmask(ser,~cond,other,change=change) return None if inplace else ser diff --git a/pandas/tests/test_series.py b/pandas/tests/test_series.py index fbbb48966f754..cc69649f24cdf 100644 --- a/pandas/tests/test_series.py +++ b/pandas/tests/test_series.py @@ -1087,6 +1087,21 @@ def test_where(self): self.assertRaises(ValueError, s.where, cond[:3].values, -s) self.assertRaises(ValueError, s.where, cond, s[:3].values) + # GH 2745 + s = Series([1,2]) + s[[True, False]] = [0,1] + expected = Series([0,2]) + assert_series_equal(s,expected) + + s = Series([1,2]) + s[[True, False]] = [0] + expected = Series([0,2]) + assert_series_equal(s,expected) + + # failures + self.assertRaises(ValueError, s.__setitem__, tuple([[[True, False]]]), [0,2,3]) + self.assertRaises(ValueError, s.__setitem__, tuple([[[True, False]]]), []) + def test_where_inplace(self): s = Series(np.random.randn(5)) cond = s > 0