Skip to content

Commit 206f025

Browse files
committed
Merge pull request #3236 from jreback/GH3235
BUG: GH3235 fix setitem on Series with boolean indexing and rhs of list
2 parents a3f9280 + fbcec5b commit 206f025

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

RELEASE.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ pandas 0.11.0
239239
- Fix implemented ``.xs`` when called with ``axes=1`` and a level parameter (GH2903_)
240240
- Timestamp now supports the class method fromordinal similar to datetimes (GH3042_)
241241
- Fix issue with indexing a series with a boolean key and specifiying a 1-len list on the rhs (GH2745_)
242+
or a list on the rhs (GH3235_)
242243
- Fixed bug in groupby apply when kernel generate list of arrays having unequal len (GH1738_)
243244
- fixed handling of rolling_corr with center=True which could produce corr>1 (GH3155_)
244245
- Fixed issues where indices can be passed as 'index/column' in addition to 0/1 for the axis parameter

pandas/core/series.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,12 +736,24 @@ def where(self, cond, other=nan, inplace=False):
736736
other = other.reindex(ser.index)
737737
elif isinstance(other, (tuple,list)):
738738
other = np.array(other)
739+
739740
if len(other) != len(ser):
741+
icond = ~cond
740742

741743
# GH 2745
742744
# treat like a scalar
743745
if len(other) == 1:
744746
other = np.array(other[0]*len(ser))
747+
748+
# GH 3235
749+
# match True cond to other
750+
elif len(icond[icond]) == len(other):
751+
dtype, fill_value = _maybe_promote(other.dtype)
752+
new_other = np.empty(len(cond),dtype=dtype)
753+
new_other.fill(fill_value)
754+
new_other[icond] = other
755+
other = new_other
756+
745757
else:
746758
raise ValueError('Length of replacements must equal series length')
747759

pandas/tests/test_series.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1092,7 +1092,6 @@ def test_where(self):
10921092

10931093
self.assertRaises(ValueError, s.where, 1)
10941094
self.assertRaises(ValueError, s.where, cond[:3].values, -s)
1095-
self.assertRaises(ValueError, s.where, cond, s[:3].values)
10961095

10971096
# GH 2745
10981097
s = Series([1,2])
@@ -1109,6 +1108,23 @@ def test_where(self):
11091108
self.assertRaises(ValueError, s.__setitem__, tuple([[[True, False]]]), [0,2,3])
11101109
self.assertRaises(ValueError, s.__setitem__, tuple([[[True, False]]]), [])
11111110

1111+
# GH3235
1112+
s = Series(np.arange(10))
1113+
mask = s < 5
1114+
s[mask] = range(5)
1115+
expected = Series(np.arange(10),dtype='float64')
1116+
assert_series_equal(s,expected)
1117+
1118+
s = Series(np.arange(10))
1119+
mask = s > 5
1120+
s[mask] = [0]*4
1121+
expected = Series([0,1,2,3,4,5] + [0]*4,dtype='float64')
1122+
assert_series_equal(s,expected)
1123+
1124+
s = Series(np.arange(10))
1125+
mask = s > 5
1126+
self.assertRaises(ValueError, s.__setitem__, mask, ([0]*5,))
1127+
11121128
def test_where_inplace(self):
11131129
s = Series(np.random.randn(5))
11141130
cond = s > 0

0 commit comments

Comments
 (0)