Skip to content

Commit 686a664

Browse files
Backport PR #44894: DOC: MultiIndex.get_level_values mentions implicit type casting if level contains null. (#45210)
Co-authored-by: Michael Wang <[email protected]>
1 parent bee33c7 commit 686a664

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

pandas/core/indexes/multi.py

+16
Original file line numberDiff line numberDiff line change
@@ -1658,6 +1658,12 @@ def get_level_values(self, level):
16581658
Values is a level of this MultiIndex converted to
16591659
a single :class:`Index` (or subclass thereof).
16601660
1661+
Notes
1662+
-----
1663+
If the level contains missing values, the result may be casted to
1664+
``float`` with missing values specified as ``NaN``. This is because
1665+
the level is converted to a regular ``Index``.
1666+
16611667
Examples
16621668
--------
16631669
Create a MultiIndex:
@@ -1671,6 +1677,16 @@ def get_level_values(self, level):
16711677
Index(['a', 'b', 'c'], dtype='object', name='level_1')
16721678
>>> mi.get_level_values('level_2')
16731679
Index(['d', 'e', 'f'], dtype='object', name='level_2')
1680+
1681+
If a level contains missing values, the return type of the level
1682+
maybe casted to ``float``.
1683+
1684+
>>> pd.MultiIndex.from_arrays([[1, None, 2], [3, 4, 5]]).dtypes
1685+
level_0 int64
1686+
level_1 int64
1687+
dtype: object
1688+
>>> pd.MultiIndex.from_arrays([[1, None, 2], [3, 4, 5]]).get_level_values(0)
1689+
Float64Index([1.0, nan, 2.0], dtype='float64')
16741690
"""
16751691
level = self._get_level_number(level)
16761692
values = self._get_level_values(level)

0 commit comments

Comments
 (0)