Skip to content

Fix #12695 : RangeIndex generated with pd.concat with ignore_index as True #12696

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

Closed
wants to merge 3 commits into from
Closed
Changes from 2 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
17 changes: 7 additions & 10 deletions pandas/tools/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
from pandas.core.frame import DataFrame, _merge_doc
from pandas.core.generic import NDFrame
from pandas.core.series import Series
from pandas.core.index import (Index, MultiIndex, _get_combined_index,
_ensure_index, _get_consensus_names,
_all_indexes_same)
from pandas.core.index import (Index, MultiIndex, RangeIndex,
_get_combined_index, _ensure_index,
_get_consensus_names, _all_indexes_same)
from pandas.core.internals import (items_overlap_with_suffix,
concatenate_block_managers)
from pandas.util.decorators import Appender, Substitution
Expand Down Expand Up @@ -992,7 +992,7 @@ def get_result(self):
# names (because set via the 'key' argument in the 'concat'
# function call. If that's not the case, use the series names
# as column names
if (columns.equals(Index(np.arange(len(self.objs)))) and
if (columns.equals(RangeIndex(len(self.objs))) and
not self.ignore_index):
columns = np.array([data[i].name
for i in range(len(data))],
Expand Down Expand Up @@ -1079,8 +1079,7 @@ def _get_concat_axis(self):
if self.axis == 0:
indexes = [x.index for x in self.objs]
elif self.ignore_index:
idx = Index(np.arange(len(self.objs)))
idx.is_unique = True # arange is always unique
idx = RangeIndex(len(self.objs))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use com._default_index(...) (you can create above and use in the comparison as well)

return idx
elif self.keys is None:
names = []
Expand All @@ -1092,8 +1091,7 @@ def _get_concat_axis(self):
if x.name is not None:
names.append(x.name)
else:
idx = Index(np.arange(len(self.objs)))
idx.is_unique = True
idx = RangeIndex(len(self.objs))
return idx

return Index(names)
Expand All @@ -1103,8 +1101,7 @@ def _get_concat_axis(self):
indexes = [x._data.axes[self.axis] for x in self.objs]

if self.ignore_index:
idx = Index(np.arange(sum(len(i) for i in indexes)))
idx.is_unique = True
idx = RangeIndex(sum(len(i) for i in indexes))
return idx

if self.keys is None:
Expand Down