Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a9e5419

Browse files
committed
BUG: (GH4548) inplace updating of a duplicate Series with a boolean aligning incorrectly
1 parent 4b6eefb commit a9e5419

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
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: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,11 +1376,22 @@ def test_where_inplace(self):
13761376
def test_where_dups(self):
13771377
# GH 4550
13781378
# where crashes with dups in index
1379-
s1 = Series(range(3))
1380-
s2 = Series(range(3))
1379+
s1 = Series(list(range(3)))
1380+
s2 = Series(list(range(3)))
13811381
comb = pd.concat([s1,s2])
13821382
result = comb.where(comb < 2)
13831383
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)
13841395

13851396
def test_mask(self):
13861397
s = Series(np.random.randn(5))

0 commit comments

Comments
 (0)