Skip to content

Commit 499dc68

Browse files
committed
BUG: duplicate indexing with embedded non-orderables (#17610)
1 parent 288bf6e commit 499dc68

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

doc/source/whatsnew/v0.22.0.txt

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

272274
I/O
273275
^^^

pandas/core/series.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -666,11 +666,13 @@ def __getitem__(self, key):
666666

667667
# we need to box if we have a non-unique index 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+
return result
674676
return result
675677
except InvalidIndexError:
676678
pass

pandas/tests/series/test_indexing.py

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

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

0 commit comments

Comments
 (0)