Skip to content

Commit 81261d4

Browse files
committed
BUG: duplicate indexing with embedded non-orderables (#17610)
1 parent 0e16818 commit 81261d4

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

doc/source/whatsnew/v0.22.0.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ Indexing
183183
- Bug in :class:`IntervalIndex` where empty and purely NA data was constructed inconsistently depending on the construction method (:issue:`18421`)
184184
- Bug in ``IntervalIndex.symmetric_difference()`` where the symmetric difference with a non-``IntervalIndex`` did not raise (:issue:`18475`)
185185
- Bug in indexing a datetimelike ``Index`` that raised ``ValueError`` instead of ``IndexError`` (:issue:`18386`).
186+
- Bug in ``Series`` containing duplicate indexing when gets embedded non-orderables or orderables, raises error or returns unexpected result. (:issue:`17610`)
186187

187188

188189
I/O

pandas/core/series.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -657,12 +657,12 @@ def __getitem__(self, key):
657657
try:
658658
result = self.index.get_value(self, key)
659659

660-
if not is_scalar(result):
660+
if not is_scalar(result) and key in self.index:
661661
if is_list_like(result) and not isinstance(result, Series):
662662

663663
# we need to box if we have a non-unique index here
664664
# otherwise have inline ndarray/lists
665-
if not self.index.is_unique:
665+
if not is_scalar(self.index.get_loc(key)):
666666
result = self._constructor(
667667
result, index=[key] * len(result),
668668
dtype=self.dtype).__finalize__(self)

pandas/tests/series/test_indexing.py

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

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

0 commit comments

Comments
 (0)