Skip to content

Commit 2486fcd

Browse files
Matt Wittmannjreback
Matt Wittmann
authored andcommitted
BUG: TypeError in index coercion
closes #12916 closes #12893
1 parent 1320ef7 commit 2486fcd

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

doc/source/whatsnew/v0.18.1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ Bug Fixes
305305
- Bug in ``Series.map`` raises ``TypeError`` if its dtype is ``category`` or tz-aware ``datetime`` (:issue:`12473`)
306306

307307

308+
- Bug in index coercion when falling back from ```RangeIndex``` construction (:issue:`12893`)
308309

309310
- Bug in slicing subclassed ``DataFrame`` defined to return subclassed ``Series`` may return normal ``Series`` (:issue:`11559`)
310311

pandas/indexes/multi.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ def get_level_values(self, level):
666666
filled = algos.take_1d(unique.values, labels,
667667
fill_value=unique._na_value)
668668
_simple_new = unique._simple_new
669-
values = _simple_new(filled, self.names[num],
669+
values = _simple_new(filled, name=self.names[num],
670670
freq=getattr(unique, 'freq', None),
671671
tz=getattr(unique, 'tz', None))
672672
return values

pandas/tests/indexes/test_multi.py

+23
Original file line numberDiff line numberDiff line change
@@ -2147,3 +2147,26 @@ def test_partial_string_timestamp_multiindex(self):
21472147
# Slicing date on first level should break (of course)
21482148
with assertRaises(KeyError):
21492149
df_swap.loc['2016-01-01']
2150+
2151+
def test_rangeindex_fallback_coercion_bug(self):
2152+
# GH 12893
2153+
foo = pd.DataFrame(np.arange(100).reshape((10, 10)))
2154+
bar = pd.DataFrame(np.arange(100).reshape((10, 10)))
2155+
df = pd.concat({'foo': foo.stack(), 'bar': bar.stack()}, axis=1)
2156+
df.index.names = ['fizz', 'buzz']
2157+
2158+
str(df)
2159+
expected = pd.DataFrame({'bar': np.arange(100),
2160+
'foo': np.arange(100)},
2161+
index=pd.MultiIndex.from_product(
2162+
[range(10), range(10)],
2163+
names=['fizz', 'buzz']))
2164+
tm.assert_frame_equal(df, expected, check_like=True)
2165+
2166+
result = df.index.get_level_values('fizz')
2167+
expected = pd.Int64Index(np.arange(10), name='fizz').repeat(10)
2168+
tm.assert_index_equal(result, expected)
2169+
2170+
result = df.index.get_level_values('buzz')
2171+
expected = pd.Int64Index(np.tile(np.arange(10), 10), name='buzz')
2172+
tm.assert_index_equal(result, expected)

0 commit comments

Comments
 (0)