Skip to content

Commit 0c02a7f

Browse files
committed
BUG: Fixes Issue pandas-dev#34318
1 parent fc41f92 commit 0c02a7f

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

pandas/core/indexing.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -766,9 +766,11 @@ def _getitem_lowerdim(self, tup: Tuple):
766766
# ...but iloc should handle the tuple as simple integer-location
767767
# instead of checking it as multiindex representation (GH 13797)
768768
if isinstance(ax0, ABCMultiIndex) and self.name != "iloc":
769-
result = self._handle_lowerdim_multi_index_axis0(tup)
770-
if result is not None:
769+
try:
770+
result = self._handle_lowerdim_multi_index_axis0(tup)
771771
return result
772+
except Exception:
773+
pass
772774

773775
if len(tup) > self.ndim:
774776
raise IndexingError("Too many indexers. handle elsewhere")
@@ -816,9 +818,11 @@ def _getitem_nested_tuple(self, tup: Tuple):
816818
if self.name != "loc":
817819
# This should never be reached, but lets be explicit about it
818820
raise ValueError("Too many indices")
819-
result = self._handle_lowerdim_multi_index_axis0(tup)
820-
if result is not None:
821+
try:
822+
result = self._handle_lowerdim_multi_index_axis0(tup)
821823
return result
824+
except Exception:
825+
pass
822826

823827
# this is a series with a multi-index specified a tuple of
824828
# selectors
@@ -1065,7 +1069,7 @@ def _handle_lowerdim_multi_index_axis0(self, tup: Tuple):
10651069
if len(tup) <= self.obj.index.nlevels and len(tup) > self.ndim:
10661070
raise ek
10671071

1068-
return None
1072+
raise Exception("No label returned")
10691073

10701074
def _getitem_axis(self, key, axis: int):
10711075
key = item_from_zerodim(key)

pandas/tests/series/indexing/test_indexing.py

+11
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,17 @@ def test_setitem_slice_into_readonly_backing_data():
853853
assert not array.any()
854854

855855

856+
def test_access_none_value_in_multiindex():
857+
# GH34318: test that you can access a None value using .loc through a Multiindex
858+
859+
expected = None
860+
result = pd.Series([None], pd.MultiIndex.from_arrays([["Level1"], ["Level2"]])).loc[
861+
("Level1", "Level2")
862+
]
863+
864+
assert expected == result
865+
866+
856867
"""
857868
miscellaneous methods
858869
"""

0 commit comments

Comments
 (0)