Skip to content

Commit e461793

Browse files
committed
Merge pull request pandas-dev#4779 from jreback/dups_ops
TST/BUG: duplicate indexing ops with a Series using where and inplace add buggy (GH4550/GH4548)
2 parents 7beb06c + a9e5419 commit e461793

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

doc/source/release.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ Bug Fixes
368368
- Bug in concatenation with duplicate columns across dtypes not merging with axis=0 (:issue:`4771`)
369369
- Bug in ``iloc`` with a slice index failing (:issue:`4771`)
370370
- Incorrect error message with no colspecs or width in ``read_fwf``. (:issue:`4774`)
371+
- Fix bugs in indexing in a Series with a duplicate index (:issue:`4548`, :issue:`4550`)
371372

372373
pandas 0.12.0
373374
-------------

pandas/core/generic.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

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

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

2311+
# if we are NOT aligned, raise as we cannot where index
2312+
if not all([ other._get_axis(i).equals(ax) for i, ax in enumerate(self.axes) ]):
2313+
raise InvalidIndexError
2314+
23112315
# slice me out of the other
23122316
else:
23132317
raise NotImplemented

pandas/core/series.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,9 +1037,13 @@ def __setitem__(self, key, value):
10371037

10381038
if _is_bool_indexer(key):
10391039
key = _check_bool_indexer(self.index, key)
1040-
self.where(~key, value, inplace=True)
1041-
else:
1042-
self._set_with(key, value)
1040+
try:
1041+
self.where(~key, value, inplace=True)
1042+
return
1043+
except (InvalidIndexError):
1044+
pass
1045+
1046+
self._set_with(key, value)
10431047

10441048
def _set_with_engine(self, key, value):
10451049
values = self.values

pandas/tests/test_series.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,6 +1373,26 @@ def test_where_inplace(self):
13731373
rs.where(cond, -s, inplace=True)
13741374
assert_series_equal(rs, s.where(cond, -s))
13751375

1376+
def test_where_dups(self):
1377+
# GH 4550
1378+
# where crashes with dups in index
1379+
s1 = Series(list(range(3)))
1380+
s2 = Series(list(range(3)))
1381+
comb = pd.concat([s1,s2])
1382+
result = comb.where(comb < 2)
1383+
expected = Series([0,1,np.nan,0,1,np.nan],index=[0,1,2,0,1,2])
1384+
assert_series_equal(result, expected)
1385+
1386+
# GH 4548
1387+
# inplace updating not working with dups
1388+
comb[comb<1] = 5
1389+
expected = Series([5,1,2,5,1,2],index=[0,1,2,0,1,2])
1390+
assert_series_equal(comb, expected)
1391+
1392+
comb[comb<2] += 10
1393+
expected = Series([5,11,2,5,11,2],index=[0,1,2,0,1,2])
1394+
assert_series_equal(comb, expected)
1395+
13761396
def test_mask(self):
13771397
s = Series(np.random.randn(5))
13781398
cond = s > 0

0 commit comments

Comments
 (0)