Skip to content

BUG: Scalar assignment to empty dataframe with loc. Closes GH41891 #41921

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 "
Expand Down
6 changes: 5 additions & 1 deletion pandas/tests/indexing/test_partial.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down