@@ -1728,19 +1728,32 @@ def _getitem_axis(self, key, axis: int):
1728
1728
self ._validate_key (key , axis )
1729
1729
return self ._get_label (key , axis = axis )
1730
1730
1731
- def _ensure_listlike_indexer (self , key : Iterable , axis : int ):
1731
+ def _ensure_listlike_indexer (self , key , is_indexer_key : bool ):
1732
1732
"""
1733
- Ensure that a list-like of keys are all present by adding them if they
1734
- do not already exist.
1733
+ Ensure that a list-like of column labels are all present by adding them if
1734
+ they do not already exist.
1735
1735
1736
1736
Parameters
1737
1737
----------
1738
- key : list-like
1739
- Target labels
1740
- axis : int
1741
- Dimension on which the indexing is being made
1738
+ key : _LocIndexer key or list-like of column labels
1739
+ Target labels.
1740
+ is_indexer_key : bool
1741
+ Whether key is a _LocIndexer key
1742
1742
"""
1743
- if not isinstance (self .obj ._get_axis (axis ), ABCMultiIndex ) and all (
1743
+ column_axis = 1
1744
+ if is_indexer_key :
1745
+ 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
1751
+ and not com .is_bool_indexer (key [column_axis ])
1752
+ ):
1753
+ return
1754
+ key = key [column_axis ]
1755
+
1756
+ if not isinstance (self .obj ._get_axis (column_axis ), ABCMultiIndex ) and all (
1744
1757
is_hashable (k ) for k in key
1745
1758
):
1746
1759
for k in key :
@@ -1750,13 +1763,7 @@ def _ensure_listlike_indexer(self, key: Iterable, axis: int):
1750
1763
self .obj [k ] = np .nan
1751
1764
1752
1765
def _get_setitem_indexer (self , key ):
1753
- if (
1754
- isinstance (key , tuple )
1755
- and len (key ) >= 2 # key is at least 2-dimensional
1756
- and is_list_like_indexer (key [1 ]) # key indexes multiple columns
1757
- and not com .is_bool_indexer (key [1 ])
1758
- ):
1759
- self ._ensure_listlike_indexer (key [1 ], axis = 1 )
1766
+ self ._ensure_listlike_indexer (key , is_indexer_key = True )
1760
1767
return super ()._get_setitem_indexer (key )
1761
1768
1762
1769
0 commit comments