Skip to content

BUG: Setting frame into df with dup cols loses dtypes #53143

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

Merged
merged 3 commits into from
May 9, 2023
Merged
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.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ Interval

Indexing
^^^^^^^^
-
- Bug in :meth:`DataFrame.__setitem__` losing dtype when setting a :class:`DataFrame` into duplicated columns (:issue:`53143`)
-

Missing
Expand Down
13 changes: 9 additions & 4 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -4101,10 +4101,15 @@ def _set_item_frame_value(self, key, value: DataFrame) -> None:
self[cols] = value[value.columns[0]]
return

# now align rows
arraylike, _ = _reindex_for_setitem(value, self.index)
self._set_item_mgr(key, arraylike)
return
locs: np.ndarray | list
if isinstance(loc, slice):
locs = np.arange(loc.start, loc.stop, loc.step)
elif is_scalar(loc):
locs = [loc]
else:
locs = loc.nonzero()[0]

return self.isetitem(locs, value)

if len(value.columns) != 1:
raise ValueError(
Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/frame/indexing/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -1283,3 +1283,19 @@ def test_setitem_iloc_with_numpy_array(self, dtype):

expected = DataFrame({"a": [2, 1, 1]}, dtype=dtype)
tm.assert_frame_equal(df, expected)

def test_setitem_frame_dup_cols_dtype(self):
# GH#53143
df = DataFrame([[1, 2, 3, 4], [4, 5, 6, 7]], columns=["a", "b", "a", "c"])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would happen if df has unique columns and rhs had duplicate columns?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we only get there with a scalar key, e.g. if columns are unique you'll end up with a single column. This would raise since your rhs dimension is too large

rhs = DataFrame([[0, 1.5], [2, 2.5]], columns=["a", "a"])
df["a"] = rhs
expected = DataFrame(
[[0, 2, 1.5, 4], [2, 5, 2.5, 7]], columns=["a", "b", "a", "c"]
)
tm.assert_frame_equal(df, expected)

df = DataFrame([[1, 2, 3], [4, 5, 6]], columns=["a", "a", "b"])
rhs = DataFrame([[0, 1.5], [2, 2.5]], columns=["a", "a"])
df["a"] = rhs
expected = DataFrame([[0, 1.5, 3], [2, 2.5, 6]], columns=["a", "a", "b"])
tm.assert_frame_equal(df, expected)