@@ -160,6 +160,9 @@ def _slice(self, obj, axis: int, kind=None):
160
160
return self .obj ._slice (obj , axis = axis , kind = kind )
161
161
162
162
def _get_setitem_indexer (self , key ):
163
+ if self .name == "loc" :
164
+ self ._ensure_listlike_indexer (key )
165
+
163
166
if self .axis is not None :
164
167
return self ._convert_tuple (key )
165
168
@@ -1728,7 +1731,7 @@ def _getitem_axis(self, key, axis: int):
1728
1731
self ._validate_key (key , axis )
1729
1732
return self ._get_label (key , axis = axis )
1730
1733
1731
- def _ensure_listlike_indexer (self , key , is_indexer_key : bool ):
1734
+ def _ensure_listlike_indexer (self , key ):
1732
1735
"""
1733
1736
Ensure that a list-like of column labels are all present by adding them if
1734
1737
they do not already exist.
@@ -1737,36 +1740,38 @@ def _ensure_listlike_indexer(self, key, is_indexer_key: bool):
1737
1740
----------
1738
1741
key : _LocIndexer key or list-like of column labels
1739
1742
Target labels.
1740
- is_indexer_key : bool
1741
- Whether key is a _LocIndexer key
1742
1743
"""
1743
1744
column_axis = 1
1744
- if is_indexer_key :
1745
+
1746
+ # check if self.obj is at least 2-dimensional
1747
+ if len (self .obj .shape ) <= column_axis :
1748
+ return
1749
+
1750
+ if isinstance (key , tuple ):
1751
+ # key is a _LocIndexer key
1752
+
1745
1753
if not (
1746
- isinstance (key , tuple )
1747
- and len (key ) >= 2 # key is at least 2-dimensional
1748
- and is_list_like_indexer (
1749
- key [column_axis ]
1750
- ) # key indexes multiple columns
1754
+ # key indexes multiple columns
1755
+ is_list_like_indexer (key [column_axis ])
1751
1756
and not com .is_bool_indexer (key [column_axis ])
1752
1757
):
1753
1758
return
1759
+
1760
+ # set key to the column part of key which is a list-like of column
1761
+ # labels
1754
1762
key = key [column_axis ]
1755
1763
1756
- if not isinstance (self .obj ._get_axis (column_axis ), ABCMultiIndex ) and all (
1757
- is_hashable (k ) for k in key
1764
+ if (
1765
+ not isinstance (self .obj ._get_axis (column_axis ), ABCMultiIndex )
1766
+ and is_list_like (key )
1767
+ and all (is_hashable (k ) for k in key )
1758
1768
):
1759
1769
for k in key :
1760
1770
try :
1761
1771
self .obj [k ]
1762
1772
except KeyError :
1763
1773
self .obj [k ] = np .nan
1764
1774
1765
- def _get_setitem_indexer (self , key ):
1766
- self ._ensure_listlike_indexer (key , is_indexer_key = True )
1767
- return super ()._get_setitem_indexer (key )
1768
-
1769
-
1770
1775
class _iLocIndexer (_LocationIndexer ):
1771
1776
"""
1772
1777
Purely integer-location based indexing for selection by position.
0 commit comments