Skip to content

BUG: Rows are not inserted into DataFrame from lists of one element i… #53329

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 16 commits 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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v2.0.3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ including other versions of pandas.
Fixed regressions
~~~~~~~~~~~~~~~~~
- Fixed performance regression in merging on datetime-like columns (:issue:`53231`)
-
- Fixed regression in :meth:`DataFrame.loc` rows are not inserted into DataFrame from lists of one element (:issue:`52825`)

.. ---------------------------------------------------------------------------
.. _whatsnew_203.bug_fixes:
Expand Down
8 changes: 8 additions & 0 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2024,10 +2024,18 @@ def _setitem_single_column(self, loc: int, value, plane_indexer) -> None:

is_null_setter = com.is_empty_slice(pi) or is_array_like(pi) and len(pi) == 0

is_empty_df_setter = com.is_null_slice(pi) and len(self.obj) == 0

if is_null_setter:
# no-op, don't cast dtype later
return

elif is_empty_df_setter:
# If we're setting a column to an empty df with null slice,
# we shouldn't do it inplace.
# GH#52825
self.obj.isetitem(loc, value)

elif is_full_setter:
try:
self.obj._mgr.column_setitem(
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,17 @@ def test_index_type_coercion(self, indexer):
indexer(s2)["0"] = 0
assert is_object_dtype(s2.index)

def test_setitem_one_element_list(self):
# GH#52825
x_values = ["x_value"]
y_values = ["y_value"]
df = DataFrame(columns=["x", "y"])
df.loc[:, "x"] = x_values
df.loc[:, "y"] = y_values

expected = DataFrame({"x": ["x_value"], "y": ["y_value"]})
tm.assert_frame_equal(df, expected)


class TestMisc:
def test_float_index_to_mixed(self):
Expand Down
6 changes: 2 additions & 4 deletions pandas/tests/indexing/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -614,18 +614,16 @@ def test_loc_setitem_consistency_single_row(self):

def test_loc_setitem_consistency_empty(self):
# empty (essentially noops)
# before the enforcement of #45333 in 2.0, the loc.setitem here would
# change the dtype of df.x to int64
# the loc.setitem here changes the dtype of df.x to int64
expected = DataFrame(columns=["x", "y"])
expected["x"] = expected["x"].astype(np.int64)
df = DataFrame(columns=["x", "y"])
with tm.assert_produces_warning(None):
df.loc[:, "x"] = 1
tm.assert_frame_equal(df, expected)

# setting with setitem swaps in a new array, so changes the dtype
df = DataFrame(columns=["x", "y"])
df["x"] = 1
expected["x"] = expected["x"].astype(np.int64)
tm.assert_frame_equal(df, expected)

def test_loc_setitem_consistency_slice_column_len(self):
Expand Down