Skip to content

BUG: GH2745 Fix issue with indexing a series with a boolean key a 1-len list on rhs #3139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 22, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions RELEASE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 0 additions & 4 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
12 changes: 10 additions & 2 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down