Skip to content

Commit 16a7799

Browse files
boombardjreback
authored andcommitted
BUG: .reset_index() should create a RangeIndex, #12071
1 parent 8e29361 commit 16a7799

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

doc/source/whatsnew/v0.18.0.txt

+1-1
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`, :issue:`12070`)
113+
This will now be the default constructed index for ``NDFrame`` objects, rather than previous an ``Int64Index``. (:issue:`939`, :issue:`12070`, :issue:`12071`)
114114

115115
Previous Behavior:
116116

pandas/core/frame.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -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

+12-1
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

+11-1
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,16 @@ 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]],
8562+
columns=['index', 'A'],
8563+
index=RangeIndex(stop=2))
8564+
assert_frame_equal(series_result, series_expected)
8565+
85568566
def test_set_index_makes_timeseries(self):
85578567
idx = tm.makeDateIndex(10)
85588568

0 commit comments

Comments
 (0)