Skip to content

Commit 7ff2af4

Browse files
committed
BUG: get_level_values() on int level upcasts to Float64Index if needed
closes pandas-dev#17924
1 parent 6e56195 commit 7ff2af4

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

doc/source/whatsnew/v0.22.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ Indexing
181181
- Bug in :func:`Series.truncate` which raises ``TypeError`` with a monotonic ``PeriodIndex`` (:issue:`17717`)
182182
- Bug in :func:`DataFrame.groupby` where tuples were interpreted as lists of keys rather than as keys (:issue:`17979`, :issue:`18249`)
183183
- Bug in :func:`MultiIndex.remove_unused_levels`` which would fill nan values (:issue:`18417`)
184+
- Bug in :func:`MultiIndex.get_level_values` which would return an invalid index on level of ints with missing values (:issue:`17924`)
184185
- Bug in :func:`MultiIndex.from_tuples`` which would fail to take zipped tuples in python3 (:issue:`18434`)
185186
- Bug in :class:`Index`` construction from list of mixed type tuples (:issue:`18505`)
186187
- Bug in :class:`IntervalIndex` where empty and purely NA data was constructed inconsistently depending on the construction method (:issue:`18421`)

pandas/core/indexes/numeric.py

+7
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ def _maybe_cast_slice_bound(self, label, side, kind):
6262
# we will try to coerce to integers
6363
return self._maybe_cast_indexer(label)
6464

65+
@Appender(_index_shared_docs['_shallow_copy'])
66+
def _shallow_copy(self, values=None, **kwargs):
67+
if values is not None and not self._can_hold_na:
68+
return self._shallow_copy_with_infer(values=values, **kwargs)
69+
return (super(NumericIndex, self)._shallow_copy(values=values,
70+
**kwargs))
71+
6572
def _convert_for_op(self, value):
6673
""" Convert value to be insertable to ndarray """
6774

pandas/tests/indexes/test_multi.py

+17-5
Original file line numberDiff line numberDiff line change
@@ -997,8 +997,8 @@ def test_get_level_values(self):
997997
exp = CategoricalIndex([1, 2, 3, 1, 2, 3])
998998
tm.assert_index_equal(index.get_level_values(1), exp)
999999

1000-
@pytest.mark.xfail(reason='GH 17924 (returns Int64Index with float data)')
10011000
def test_get_level_values_int_with_na(self):
1001+
# GH 17924
10021002
arrays = [['a', 'b', 'b'], [1, np.nan, 2]]
10031003
index = pd.MultiIndex.from_arrays(arrays)
10041004
result = index.get_level_values(1)
@@ -1024,14 +1024,26 @@ def test_get_level_values_na(self):
10241024

10251025
arrays = [['a', 'b', 'b'], pd.DatetimeIndex([0, 1, pd.NaT])]
10261026
index = pd.MultiIndex.from_arrays(arrays)
1027-
values = index.get_level_values(1)
1027+
result = index.get_level_values(1)
10281028
expected = pd.DatetimeIndex([0, 1, pd.NaT])
1029-
tm.assert_index_equal(values, expected)
1029+
tm.assert_index_equal(result, expected)
10301030

10311031
arrays = [[], []]
10321032
index = pd.MultiIndex.from_arrays(arrays)
1033-
values = index.get_level_values(0)
1034-
assert values.shape == (0, )
1033+
result = index.get_level_values(0)
1034+
expected = pd.Index([], dtype=object)
1035+
tm.assert_index_equal(result, expected)
1036+
1037+
def test_get_level_values_all_na(self):
1038+
arrays = [[np.nan, np.nan, np.nan], ['a', np.nan, 1]]
1039+
index = pd.MultiIndex.from_arrays(arrays)
1040+
result = index.get_level_values(0)
1041+
expected = pd.Index([np.nan, np.nan, np.nan], dtype=np.float64)
1042+
tm.assert_index_equal(result, expected)
1043+
1044+
result = index.get_level_values(1)
1045+
expected = pd.Index(['a', np.nan, 1], dtype=object)
1046+
tm.assert_index_equal(result, expected)
10351047

10361048
def test_reorder_levels(self):
10371049
# this blows up

0 commit comments

Comments
 (0)