diff --git a/doc/source/whatsnew/v0.18.1.txt b/doc/source/whatsnew/v0.18.1.txt index 073b859f4c9a7..3c622ebc40799 100644 --- a/doc/source/whatsnew/v0.18.1.txt +++ b/doc/source/whatsnew/v0.18.1.txt @@ -333,3 +333,5 @@ Bug Fixes - Bug in ``fill_value`` is ignored if the argument to a binary operator is a constant (:issue `12723`) + +- Bug in index coercion when falling back from ```RangeIndex``` construction (:issue:`12893`) diff --git a/pandas/indexes/multi.py b/pandas/indexes/multi.py index b58c5382f628c..cc4e200d1026f 100644 --- a/pandas/indexes/multi.py +++ b/pandas/indexes/multi.py @@ -666,7 +666,7 @@ def get_level_values(self, level): filled = algos.take_1d(unique.values, labels, fill_value=unique._na_value) _simple_new = unique._simple_new - values = _simple_new(filled, self.names[num], + values = _simple_new(filled, name=self.names[num], freq=getattr(unique, 'freq', None), tz=getattr(unique, 'tz', None)) return values diff --git a/pandas/tests/indexes/test_multi.py b/pandas/tests/indexes/test_multi.py index 390dbdd76a266..bd9ddfee825a9 100644 --- a/pandas/tests/indexes/test_multi.py +++ b/pandas/tests/indexes/test_multi.py @@ -2130,3 +2130,12 @@ def test_partial_string_timestamp_multiindex(self): # Slicing date on first level should break (of course) with assertRaises(KeyError): df_swap.loc['2016-01-01'] + + def test_rangeindex_fallback_coercion_bug(self): + # GH 12893 + foo = pd.DataFrame(np.arange(100).reshape((10, 10))) + bar = pd.DataFrame(np.arange(100).reshape((10, 10))) + df = pd.concat({'foo': foo.stack(), 'bar': bar.stack()}, axis=1) + df.index.names = ['fizz', 'buzz'] + expected = [i for i in range(10) for j in range(10)] + self.assertTrue((df.index.get_level_values('fizz') == expected).all())