Skip to content

Commit

Permalink
BUG: .reset_index() should create a RangeIndex, #12071
Browse files Browse the repository at this point in the history
  • Loading branch information
boombard authored and jreback committed Jan 20, 2016
1 parent 8e29361 commit 16a7799
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.18.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ Range Index

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.

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

Previous Behavior:

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2891,7 +2891,7 @@ def _maybe_casted_values(index, labels=None):
np.nan)
return values

new_index = np.arange(len(new_obj), dtype='int64')
new_index = _default_index(len(new_obj))
if isinstance(self.index, MultiIndex):
if level is not None:
if not isinstance(level, (tuple, list)):
Expand Down
13 changes: 12 additions & 1 deletion pandas/tests/frame/test_alter_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import numpy as np

from pandas.compat import lrange
from pandas import DataFrame, Series, Index, MultiIndex
from pandas import DataFrame, Series, Index, MultiIndex, RangeIndex
import pandas as pd

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

def test_reset_index_range(self):
# GH 12071
df = pd.DataFrame([[0, 0], [1, 1]], columns=['A', 'B'],
index=RangeIndex(stop=2))
result = df.reset_index()
tm.assertIsInstance(result.index, RangeIndex)
expected = pd.DataFrame([[0, 0, 0], [1, 1, 1]],
columns=['index', 'A', 'B'],
index=RangeIndex(stop=2))
assert_frame_equal(result, expected)

def test_set_index_names(self):
df = pd.util.testing.makeDataFrame()
df.index.name = 'name'
Expand Down
12 changes: 11 additions & 1 deletion pandas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from pandas import (Index, Series, DataFrame, isnull, notnull, bdate_range,
NaT, date_range, period_range, timedelta_range,
_np_version_under1p8, _np_version_under1p9)
from pandas.core.index import MultiIndex
from pandas.core.index import MultiIndex, RangeIndex
from pandas.core.indexing import IndexingError
from pandas.tseries.period import PeriodIndex
from pandas.tseries.index import Timestamp, DatetimeIndex
Expand Down Expand Up @@ -8553,6 +8553,16 @@ def test_reset_index(self):
self.assertTrue(rs.index.equals(Index(index.get_level_values(1))))
tm.assertIsInstance(rs, Series)

def test_reset_index_range(self):
# GH 12071
s = pd.Series(range(2), name='A', index=RangeIndex(stop=2))
series_result = s.reset_index()
tm.assertIsInstance(series_result.index, RangeIndex)
series_expected = pd.DataFrame([[0, 0], [1, 1]],
columns=['index', 'A'],
index=RangeIndex(stop=2))
assert_frame_equal(series_result, series_expected)

def test_set_index_makes_timeseries(self):
idx = tm.makeDateIndex(10)

Expand Down

0 comments on commit 16a7799

Please sign in to comment.