Skip to content

Commit 5a82391

Browse files
committed
BUG: duplicate indexing with embedded non-orderables (#17610)
1 parent 34a8d36 commit 5a82391

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

doc/source/whatsnew/v0.22.0.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,8 @@ Indexing
269269
- Bug in :func:`IntervalIndex.symmetric_difference` where the symmetric difference with a non-``IntervalIndex`` did not raise (:issue:`18475`)
270270
- Bug in indexing a datetimelike ``Index`` that raised ``ValueError`` instead of ``IndexError`` (:issue:`18386`).
271271
- Bug in tz-aware :class:`DatetimeIndex` where addition/subtraction with a :class:`TimedeltaIndex` or array with ``dtype='timedelta64[ns]'`` was incorrect (:issue:`17558`)
272+
- Bug in indexing non_scalar item with unique index in ``Series`` containing duplicate index, returns ``Series`` wrapping value flatted. (:issue:`17610`)
273+
272274

273275
I/O
274276
^^^

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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,29 @@ 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, duplicate_key, unique_key',
552+
[
553+
[
554+
pd.Series({1: 12, 2: [1, 2, 2, 3]}), pd.Series({1: 313}),
555+
pd.Series({1: 12, }, dtype=object),
556+
1, 2,
557+
],
558+
[
559+
pd.Series({1: [1, 2, 3], 2: [1, 2, 2, 3]}),
560+
pd.Series({1: [1, 2, 3]}), pd.Series({1: [1, 2, 3], }),
561+
1, 2,
562+
],
563+
])
564+
def test_getitem_with_duplicates_indices(
565+
self, result_1, duplicate_item,
566+
expected_1, duplicate_key, unique_key):
567+
# GH 17610
568+
result = result_1.append(duplicate_item)
569+
expected = expected_1.append(duplicate_item)
570+
assert_series_equal(result[duplicate_key], expected)
571+
assert result[unique_key] == result_1[unique_key]
572+
550573
def test_getitem_median_slice_bug(self):
551574
index = date_range('20090415', '20090519', freq='2B')
552575
s = Series(np.random.randn(13), index=index)

0 commit comments

Comments
 (0)