Skip to content

Commit 7a1b0ee

Browse files
gloryfromcajreback
authored andcommitted
BUG: duplicate indexing with embedded non-orderables (#17610)
closes #17610 Author: zhanghui <[email protected]> Closes #18609 from gloryfromca/master and squashes the following commits: ed4dcaa [zhanghui] BUG: duplicate indexing with embedded non-orderables (#17610)
1 parent aa67207 commit 7a1b0ee

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

doc/source/whatsnew/v0.22.0.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,8 @@ Indexing
299299
- Bug in tz-aware :class:`DatetimeIndex` where addition/subtraction with a :class:`TimedeltaIndex` or array with ``dtype='timedelta64[ns]'`` was incorrect (:issue:`17558`)
300300
- :func:`Index.to_series` now accepts ``index`` and ``name`` kwargs (:issue:`18699`)
301301
- :func:`DatetimeIndex.to_series` now accepts ``index`` and ``name`` kwargs (:issue:`18699`)
302+
- Bug in indexing non-scalar value from ``Series`` having non-unique ``Index`` will return value flattened (:issue:`17610`)
303+
302304

303305
I/O
304306
^^^

pandas/core/series.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -664,13 +664,15 @@ def __getitem__(self, key):
664664
if not is_scalar(result):
665665
if is_list_like(result) and not isinstance(result, Series):
666666

667-
# we need to box if we have a non-unique index here
667+
# we need to box if loc of the key isn't scalar here
668668
# otherwise have inline ndarray/lists
669-
if not self.index.is_unique:
670-
result = self._constructor(
671-
result, index=[key] * len(result),
672-
dtype=self.dtype).__finalize__(self)
673-
669+
try:
670+
if not is_scalar(self.index.get_loc(key)):
671+
result = self._constructor(
672+
result, index=[key] * len(result),
673+
dtype=self.dtype).__finalize__(self)
674+
except KeyError:
675+
pass
674676
return result
675677
except InvalidIndexError:
676678
pass

pandas/tests/series/test_indexing.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,26 @@ def test_getitem_setitem_periodindex(self):
547547
result[4:8] = ts[4:8]
548548
assert_series_equal(result, ts)
549549

550+
@pytest.mark.parametrize(
551+
'result_1, duplicate_item, expected_1',
552+
[
553+
[
554+
pd.Series({1: 12, 2: [1, 2, 2, 3]}), pd.Series({1: 313}),
555+
pd.Series({1: 12, }, dtype=object),
556+
],
557+
[
558+
pd.Series({1: [1, 2, 3], 2: [1, 2, 2, 3]}),
559+
pd.Series({1: [1, 2, 3]}), pd.Series({1: [1, 2, 3], }),
560+
],
561+
])
562+
def test_getitem_with_duplicates_indices(
563+
self, result_1, duplicate_item, expected_1):
564+
# GH 17610
565+
result = result_1.append(duplicate_item)
566+
expected = expected_1.append(duplicate_item)
567+
assert_series_equal(result[1], expected)
568+
assert result[2] == result_1[2]
569+
550570
def test_getitem_median_slice_bug(self):
551571
index = date_range('20090415', '20090519', freq='2B')
552572
s = Series(np.random.randn(13), index=index)

0 commit comments

Comments
 (0)