diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index 3707e141bc447..b25e7a5afd660 100644 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -1646,10 +1646,11 @@ def _setitem_with_indexer(self, indexer, value, name="iloc"): if self.ndim > 1 and i == info_axis: # add the new item, and set the value - # must have all defined axes if we have a scalar - # or a list-like on the non-info axes if we have a - # list-like - if not len(self.obj): + # If we have a scalar as values, must have all defined + # axes or be an empty slice. + # If we have a list-like as value, must have a + # list-like on the non-info axes + if not len(self.obj) and not com.is_null_slice(indexer[0]): if not is_list_like_indexer(value): raise ValueError( "cannot set a frame with no " diff --git a/pandas/tests/indexing/test_partial.py b/pandas/tests/indexing/test_partial.py index 693e67652c912..b16f802a982dd 100644 --- a/pandas/tests/indexing/test_partial.py +++ b/pandas/tests/indexing/test_partial.py @@ -384,7 +384,11 @@ def test_partial_set_empty_frame(self): msg = "cannot set a frame with no defined index and a scalar" with pytest.raises(ValueError, match=msg): - df.loc[:, 1] = 1 + df.loc[3:4, 1] = 1 + + df.loc[:, 1] = 1 + assert df.columns.to_list() == [1] + assert len(df) == 0 def test_partial_set_empty_frame2(self): # these work as they don't really change