Skip to content

BUG: RangeIndex concatenating incorrectly a single object of length 1 #39439

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ Indexing
- Bug in setting ``timedelta64`` values into numeric :class:`Series` failing to cast to object dtype (:issue:`39086`)
- 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`)
- 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`)
- Bug in :meth:`RangeIndex.append` where a single object of length 1 was concatenated incorrectly (:issue:`39401`)

Missing
^^^^^^^
Expand Down
3 changes: 3 additions & 0 deletions pandas/core/indexes/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,9 @@ def _concat(self, indexes, name):
if not all(isinstance(x, RangeIndex) for x in indexes):
return super()._concat(indexes, name)

elif len(indexes) == 1:
return indexes[0]

start = step = next_ = None

# Filter the empty indexes
Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/indexes/ranges/test_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,3 +506,18 @@ def test_format_empty(self):
empty_idx = self._holder(0)
assert empty_idx.format() == []
assert empty_idx.format(name=True) == [""]

@pytest.mark.parametrize(
"RI",
[
RangeIndex(0, -1, -1),
RangeIndex(0, 1, 1),
RangeIndex(1, 3, 2),
RangeIndex(0, -1, -2),
RangeIndex(-3, -5, -2),
],
)
def test_append_len_one(self, RI):
# GH39401
result = RI.append([])
tm.assert_index_equal(result, RI, exact=True)
6 changes: 6 additions & 0 deletions pandas/tests/reshape/concat/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,9 @@ def test_concat_series_partial_columns_names(self):
result = concat([foo, bar, baz], axis=1, ignore_index=True)
expected = DataFrame({0: [1, 2], 1: [1, 2], 2: [4, 5]})
tm.assert_frame_equal(result, expected)

def test_concat_series_length_one_reversed(self, frame_or_series):
# GH39401
obj = frame_or_series([100])
result = pd.concat([obj.iloc[::-1]])
tm.assert_equal(result, obj)