From 8995aae4837b87dbd20708740920ba4d8fe20959 Mon Sep 17 00:00:00 2001 From: Michael Wang Date: Wed, 15 Dec 2021 04:41:11 +0000 Subject: [PATCH 1/6] initial --- pandas/core/indexes/multi.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index 0c158d47cfa3b..5be8505fcd417 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -1647,7 +1647,9 @@ def get_level_values(self, level): """ Return vector of label values for requested level. - Length of returned vector is equal to the length of the index. + Length of returned vector is equal to the length of the index. Note + that if the level contains null, the result may be casted to ``float`` + with null rows specified with NaN. Parameters ---------- From 8a6dd0f82f53ddba941f9cad7c216a14c8372ad5 Mon Sep 17 00:00:00 2001 From: Michael Wang Date: Wed, 15 Dec 2021 05:08:18 +0000 Subject: [PATCH 2/6] code highlight nan --- pandas/core/indexes/multi.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index 5be8505fcd417..7174768ecb6e5 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -1649,7 +1649,7 @@ def get_level_values(self, level): Length of returned vector is equal to the length of the index. Note that if the level contains null, the result may be casted to ``float`` - with null rows specified with NaN. + with null rows specified with ``NaN``. Parameters ---------- From 42eab2c8cb6ec24e89216ffb902b927264d826c8 Mon Sep 17 00:00:00 2001 From: Michael Wang Date: Wed, 15 Dec 2021 20:37:04 +0000 Subject: [PATCH 3/6] Address review comments --- pandas/core/indexes/multi.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index 7174768ecb6e5..21c21f1a3f597 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -1648,8 +1648,10 @@ def get_level_values(self, level): Return vector of label values for requested level. Length of returned vector is equal to the length of the index. Note - that if the level contains null, the result may be casted to ``float`` - with null rows specified with ``NaN``. + that if the level contains missing values, the result may be casted to + ``float`` with missing values specified with ``NaN``. This is because + the level is converted to a regular ``Index``, not to a ``MultiIndex`` + when this method is called. Parameters ---------- From 53bf7f4770b92f035c7b694f115cfbc0d63d7f56 Mon Sep 17 00:00:00 2001 From: Michael Wang Date: Wed, 15 Dec 2021 20:50:24 +0000 Subject: [PATCH 4/6] address review comments --- pandas/core/indexes/multi.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index 21c21f1a3f597..5a9cd07013af1 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -1649,9 +1649,8 @@ def get_level_values(self, level): Length of returned vector is equal to the length of the index. Note that if the level contains missing values, the result may be casted to - ``float`` with missing values specified with ``NaN``. This is because - the level is converted to a regular ``Index``, not to a ``MultiIndex`` - when this method is called. + ``float`` with missing values specified as ``NaN``. This is because + the level is converted to a regular ``Index``. Parameters ---------- From 2d7eff141aa7b2991b3d20edfa2ce0fc8226f61a Mon Sep 17 00:00:00 2001 From: Michael Wang Date: Wed, 29 Dec 2021 20:03:51 +0000 Subject: [PATCH 5/6] add example --- pandas/core/indexes/multi.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index 5a9cd07013af1..987d833f13d96 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -1677,6 +1677,16 @@ def get_level_values(self, level): Index(['a', 'b', 'c'], dtype='object', name='level_1') >>> mi.get_level_values('level_2') Index(['d', 'e', 'f'], dtype='object', name='level_2') + + If a level contains missing values, the return type of the level + maybe casted to ``float``. + + >>> pd.MultiIndex.from_arrays([[1, None, 2], [3, 4, 5]]).dtypes + level_0 int64 + level_1 int64 + dtype: object + >>> pd.MultiIndex.from_arrays([[1, None, 2], [3, 4, 5]]).get_level_values(0) + Float64Index([1.0, nan, 2.0], dtype='float64') """ level = self._get_level_number(level) values = self._get_level_values(level) From 8cfcefcb095b7aa9787f9cb3a426c85bbf0f6361 Mon Sep 17 00:00:00 2001 From: Michael Wang Date: Wed, 5 Jan 2022 18:13:45 +0000 Subject: [PATCH 6/6] Move added text into note section --- pandas/core/indexes/multi.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index 987d833f13d96..110e3400a1410 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -1647,10 +1647,7 @@ def get_level_values(self, level): """ Return vector of label values for requested level. - Length of returned vector is equal to the length of the index. Note - that if the level contains missing values, the result may be casted to - ``float`` with missing values specified as ``NaN``. This is because - the level is converted to a regular ``Index``. + Length of returned vector is equal to the length of the index. Parameters ---------- @@ -1664,6 +1661,12 @@ def get_level_values(self, level): Values is a level of this MultiIndex converted to a single :class:`Index` (or subclass thereof). + Notes + ----- + If the level contains missing values, the result may be casted to + ``float`` with missing values specified as ``NaN``. This is because + the level is converted to a regular ``Index``. + Examples -------- Create a MultiIndex: