Skip to content

Commit 5816d0e

Browse files
committed
Merge pull request #3139 from jreback/GH2745
BUG: GH2745 Fix issue with indexing a series with a boolean key a 1-len list on rhs
2 parents d31e05f + 12167d1 commit 5816d0e

File tree

4 files changed

+27
-6
lines changed

4 files changed

+27
-6
lines changed

RELEASE.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ pandas 0.11.0
220220
to an *ordered* timeseries (GH2437_).
221221
- Fix implemented ``.xs`` when called with ``axes=1`` and a level parameter (GH2903_)
222222
- Timestamp now supports the class method fromordinal similar to datetimes (GH3042_)
223+
- Fix issue with indexing a series with a boolean key and specifiying a 1-len list on the rhs (GH2745_)
223224

224225
.. _GH622: https://github.com/pydata/pandas/issues/622
225226
.. _GH797: https://github.com/pydata/pandas/issues/797
@@ -240,6 +241,7 @@ pandas 0.11.0
240241
.. _GH3011: https://github.com/pydata/pandas/issues/3011
241242
.. _GH2681: https://github.com/pydata/pandas/issues/2681
242243
.. _GH2719: https://github.com/pydata/pandas/issues/2719
244+
.. _GH2745: https://github.com/pydata/pandas/issues/2745
243245
.. _GH2746: https://github.com/pydata/pandas/issues/2746
244246
.. _GH2747: https://github.com/pydata/pandas/issues/2747
245247
.. _GH2751: https://github.com/pydata/pandas/issues/2751

pandas/core/common.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -766,10 +766,6 @@ def changeit():
766766

767767
return r, True
768768

769-
new_dtype, fill_value = _maybe_promote(result.dtype,other)
770-
if new_dtype != result.dtype:
771-
return changeit()
772-
773769
try:
774770
np.putmask(result, mask, other)
775771
except:

pandas/core/series.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -729,11 +729,19 @@ def where(self, cond, other=nan, inplace=False):
729729

730730
if isinstance(other, Series):
731731
other = other.reindex(ser.index)
732+
elif isinstance(other, (tuple,list)):
733+
other = np.array(other)
732734
if len(other) != len(ser):
733-
raise ValueError('Length of replacements must equal series length')
735+
736+
# GH 2745
737+
# treat like a scalar
738+
if len(other) == 1:
739+
other = np.array(other[0]*len(ser))
740+
else:
741+
raise ValueError('Length of replacements must equal series length')
734742

735743
change = ser if inplace else None
736-
result, changed = com._maybe_upcast_putmask(ser,~cond,other,change=change)
744+
com._maybe_upcast_putmask(ser,~cond,other,change=change)
737745

738746
return None if inplace else ser
739747

pandas/tests/test_series.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,6 +1087,21 @@ def test_where(self):
10871087
self.assertRaises(ValueError, s.where, cond[:3].values, -s)
10881088
self.assertRaises(ValueError, s.where, cond, s[:3].values)
10891089

1090+
# GH 2745
1091+
s = Series([1,2])
1092+
s[[True, False]] = [0,1]
1093+
expected = Series([0,2])
1094+
assert_series_equal(s,expected)
1095+
1096+
s = Series([1,2])
1097+
s[[True, False]] = [0]
1098+
expected = Series([0,2])
1099+
assert_series_equal(s,expected)
1100+
1101+
# failures
1102+
self.assertRaises(ValueError, s.__setitem__, tuple([[[True, False]]]), [0,2,3])
1103+
self.assertRaises(ValueError, s.__setitem__, tuple([[[True, False]]]), [])
1104+
10901105
def test_where_inplace(self):
10911106
s = Series(np.random.randn(5))
10921107
cond = s > 0

0 commit comments

Comments
 (0)