Skip to content

Commit 45be21e

Browse files
authored
BUG: RangeIndex concatenating incorrectly a single object of length 1 (#39439)
1 parent 179fe3a commit 45be21e

File tree

4 files changed

+25
-0
lines changed

4 files changed

+25
-0
lines changed

doc/source/whatsnew/v1.3.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ Indexing
336336
- Bug in setting ``timedelta64`` values into numeric :class:`Series` failing to cast to object dtype (:issue:`39086`)
337337
- Bug in setting :class:`Interval` values into a :class:`Series` or :class:`DataFrame` with mismatched :class:`IntervalDtype` incorrectly casting the new values to the existing dtype (:issue:`39120`)
338338
- Bug in incorrectly raising in :meth:`Index.insert`, when setting a new column that cannot be held in the existing ``frame.columns``, or in :meth:`Series.reset_index` or :meth:`DataFrame.reset_index` instead of casting to a compatible dtype (:issue:`39068`)
339+
- Bug in :meth:`RangeIndex.append` where a single object of length 1 was concatenated incorrectly (:issue:`39401`)
339340

340341
Missing
341342
^^^^^^^

pandas/core/indexes/range.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,9 @@ def _concat(self, indexes, name):
697697
if not all(isinstance(x, RangeIndex) for x in indexes):
698698
return super()._concat(indexes, name)
699699

700+
elif len(indexes) == 1:
701+
return indexes[0]
702+
700703
start = step = next_ = None
701704

702705
# Filter the empty indexes

pandas/tests/indexes/ranges/test_range.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,3 +506,18 @@ def test_format_empty(self):
506506
empty_idx = self._holder(0)
507507
assert empty_idx.format() == []
508508
assert empty_idx.format(name=True) == [""]
509+
510+
@pytest.mark.parametrize(
511+
"RI",
512+
[
513+
RangeIndex(0, -1, -1),
514+
RangeIndex(0, 1, 1),
515+
RangeIndex(1, 3, 2),
516+
RangeIndex(0, -1, -2),
517+
RangeIndex(-3, -5, -2),
518+
],
519+
)
520+
def test_append_len_one(self, RI):
521+
# GH39401
522+
result = RI.append([])
523+
tm.assert_index_equal(result, RI, exact=True)

pandas/tests/reshape/concat/test_series.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,9 @@ def test_concat_series_partial_columns_names(self):
143143
result = concat([foo, bar, baz], axis=1, ignore_index=True)
144144
expected = DataFrame({0: [1, 2], 1: [1, 2], 2: [4, 5]})
145145
tm.assert_frame_equal(result, expected)
146+
147+
def test_concat_series_length_one_reversed(self, frame_or_series):
148+
# GH39401
149+
obj = frame_or_series([100])
150+
result = pd.concat([obj.iloc[::-1]])
151+
tm.assert_equal(result, obj)

0 commit comments

Comments
 (0)