Skip to content

Commit 11d48bf

Browse files
committed
BUG: GH12071 .reset_index() should create a RangeIndex
1 parent 69f794f commit 11d48bf

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed

doc/source/whatsnew/v0.18.0.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ Range Index
110110

111111
A ``RangeIndex`` has been added to the ``Int64Index`` sub-classes to support a memory saving alternative for common use cases. This has a similar implementation to the python ``range`` object (``xrange`` in python 2), in that it only stores the start, stop, and step values for the index. It will transparently interact with the user API, converting to ``Int64Index`` if needed.
112112

113-
This will now be the default constructed index for ``NDFrame`` objects, rather than previous an ``Int64Index``. (:issue:`939`)
113+
This will now be the default constructed index for ``NDFrame`` objects, rather than previous an ``Int64Index``. (:issue:`939`, :issue:`12071`)
114114

115115
Previous Behavior:
116116

pandas/core/frame.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
is_internal_type, is_datetimetz, _possibly_infer_to_datetimelike,
3131
_dict_compat)
3232
from pandas.core.generic import NDFrame, _shared_docs
33-
from pandas.core.index import Index, MultiIndex, _ensure_index
33+
from pandas.core.index import Index, MultiIndex, _ensure_index, RangeIndex
3434
from pandas.core.indexing import (maybe_droplevels, convert_to_index_sliceable,
3535
check_bool_indexer)
3636
from pandas.core.internals import (BlockManager,
@@ -2891,7 +2891,7 @@ def _maybe_casted_values(index, labels=None):
28912891
np.nan)
28922892
return values
28932893

2894-
new_index = np.arange(len(new_obj), dtype='int64')
2894+
new_index = _default_index(len(new_obj))
28952895
if isinstance(self.index, MultiIndex):
28962896
if level is not None:
28972897
if not isinstance(level, (tuple, list)):

pandas/tests/frame/test_alter_axes.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import numpy as np
88

99
from pandas.compat import lrange
10-
from pandas import DataFrame, Series, Index, MultiIndex
10+
from pandas import DataFrame, Series, Index, MultiIndex, RangeIndex
1111
import pandas as pd
1212

1313
from pandas.util.testing import (assert_almost_equal,
@@ -578,6 +578,17 @@ def test_reset_index_with_datetimeindex_cols(self):
578578
datetime(2013, 1, 2)])
579579
assert_frame_equal(result, expected)
580580

581+
def test_reset_index_range(self):
582+
# GH 12071
583+
df = pd.DataFrame([[0, 0], [1, 1]], columns=['A', 'B'],
584+
index=RangeIndex(stop=2))
585+
result = df.reset_index()
586+
tm.assertIsInstance(result.index, RangeIndex)
587+
expected = pd.DataFrame([[0, 0, 0], [1, 1, 1]],
588+
columns=['index', 'A', 'B'],
589+
index=RangeIndex(stop=2))
590+
assert_frame_equal(result, expected)
591+
581592
def test_set_index_names(self):
582593
df = pd.util.testing.makeDataFrame()
583594
df.index.name = 'name'

pandas/tests/test_series.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from pandas import (Index, Series, DataFrame, isnull, notnull, bdate_range,
2121
NaT, date_range, period_range, timedelta_range,
2222
_np_version_under1p8, _np_version_under1p9)
23-
from pandas.core.index import MultiIndex
23+
from pandas.core.index import MultiIndex, RangeIndex
2424
from pandas.core.indexing import IndexingError
2525
from pandas.tseries.period import PeriodIndex
2626
from pandas.tseries.index import Timestamp, DatetimeIndex
@@ -8553,6 +8553,15 @@ def test_reset_index(self):
85538553
self.assertTrue(rs.index.equals(Index(index.get_level_values(1))))
85548554
tm.assertIsInstance(rs, Series)
85558555

8556+
def test_reset_index_range(self):
8557+
# GH 12071
8558+
s = pd.Series(range(2), name='A', index=RangeIndex(stop=2))
8559+
series_result = s.reset_index()
8560+
tm.assertIsInstance(series_result.index, RangeIndex)
8561+
series_expected = pd.DataFrame([[0, 0], [1, 1]], columns=['index', 'A'],
8562+
index=RangeIndex(stop=2))
8563+
assert_frame_equal(series_result, series_expected)
8564+
85568565
def test_set_index_makes_timeseries(self):
85578566
idx = tm.makeDateIndex(10)
85588567

0 commit comments

Comments
 (0)