Skip to content

TST/BUG: duplicate indexing ops with a Series using where and inplace add buggy (GH4550/GH4548) #4779

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 2 commits into from
Sep 9, 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
1 change: 1 addition & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ Bug Fixes
- Bug in concatenation with duplicate columns across dtypes not merging with axis=0 (:issue:`4771`)
- Bug in ``iloc`` with a slice index failing (:issue:`4771`)
- Incorrect error message with no colspecs or width in ``read_fwf``. (:issue:`4774`)
- Fix bugs in indexing in a Series with a duplicate index (:issue:`4548`, :issue:`4550`)

pandas 0.12.0
-------------
Expand Down
6 changes: 5 additions & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import pandas as pd
from pandas.core.base import PandasObject
from pandas.core.index import Index, MultiIndex, _ensure_index
from pandas.core.index import Index, MultiIndex, _ensure_index, InvalidIndexError
import pandas.core.indexing as indexing
from pandas.core.indexing import _maybe_convert_indices
from pandas.tseries.index import DatetimeIndex
Expand Down Expand Up @@ -2308,6 +2308,10 @@ def where(self, cond, other=np.nan, inplace=False, try_cast=False, raise_on_erro

_, other = self.align(other, join='left', fill_value=np.nan)

# if we are NOT aligned, raise as we cannot where index
if not all([ other._get_axis(i).equals(ax) for i, ax in enumerate(self.axes) ]):
raise InvalidIndexError

# slice me out of the other
else:
raise NotImplemented
Expand Down
10 changes: 7 additions & 3 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1037,9 +1037,13 @@ def __setitem__(self, key, value):

if _is_bool_indexer(key):
key = _check_bool_indexer(self.index, key)
self.where(~key, value, inplace=True)
else:
self._set_with(key, value)
try:
self.where(~key, value, inplace=True)
return
except (InvalidIndexError):
pass

self._set_with(key, value)

def _set_with_engine(self, key, value):
values = self.values
Expand Down
20 changes: 20 additions & 0 deletions pandas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1373,6 +1373,26 @@ def test_where_inplace(self):
rs.where(cond, -s, inplace=True)
assert_series_equal(rs, s.where(cond, -s))

def test_where_dups(self):
# GH 4550
# where crashes with dups in index
s1 = Series(list(range(3)))
s2 = Series(list(range(3)))
comb = pd.concat([s1,s2])
result = comb.where(comb < 2)
expected = Series([0,1,np.nan,0,1,np.nan],index=[0,1,2,0,1,2])
assert_series_equal(result, expected)

# GH 4548
# inplace updating not working with dups
comb[comb<1] = 5
expected = Series([5,1,2,5,1,2],index=[0,1,2,0,1,2])
assert_series_equal(comb, expected)

comb[comb<2] += 10
expected = Series([5,11,2,5,11,2],index=[0,1,2,0,1,2])
assert_series_equal(comb, expected)

def test_mask(self):
s = Series(np.random.randn(5))
cond = s > 0
Expand Down