Skip to content

Commit 26bd7cd

Browse files
committed
Make minor edits and add comments
1 parent 293df1f commit 26bd7cd

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

pandas/core/indexing.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,13 @@ def _get_setitem_indexer(self, key):
202202
def __setitem__(self, key, value):
203203
if isinstance(key, tuple):
204204
key = tuple(com.apply_if_callable(x, self.obj) for x in key)
205+
# if `key` is an index to multiple columns by name, add each column
206+
# if it does not already exist
205207
if (
206-
self.name == "loc"
207-
and len(key) > 1
208-
and is_list_like_indexer(key[1])
209-
and not isinstance(key[1], tuple)
208+
self.name == "loc" # column is indexed by name
209+
and len(key) >= 2 # key is at least 2-dimensional
210+
and is_list_like_indexer(key[1]) # key indexes multiple columns
211+
and not isinstance(self.obj._get_axis(1), ABCMultiIndex)
210212
and not com.is_bool_indexer(key[1])
211213
and all(is_hashable(k) for k in key[1])
212214
):

pandas/tests/frame/test_indexing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ def test_setitem_list_missing_columns(self, float_frame, columns, box):
225225
if col not in expected.columns:
226226
expected[col] = np.nan
227227
expected[columns] = box(float_frame)
228-
assert_frame_equal(result, expected)
228+
tm.assert_frame_equal(result, expected)
229229

230230
def test_setitem_mulit_index(self):
231231
# GH7655, test that assigning to a sub-frame of a frame

0 commit comments

Comments
 (0)