Skip to content

Commit 9bd8b5d

Browse files
authored
BUG: Setting frame into df with dup cols loses dtypes (#53143)
* BUG: Setting frame into df with dup cols loses dtypes * BUG: Setting frame into df with dup cols loses dtypes * Fix mypy
1 parent 94e868a commit 9bd8b5d

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

doc/source/whatsnew/v2.1.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ Interval
363363

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

369369
Missing

pandas/core/frame.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -4101,10 +4101,15 @@ def _set_item_frame_value(self, key, value: DataFrame) -> None:
41014101
self[cols] = value[value.columns[0]]
41024102
return
41034103

4104-
# now align rows
4105-
arraylike, _ = _reindex_for_setitem(value, self.index)
4106-
self._set_item_mgr(key, arraylike)
4107-
return
4104+
locs: np.ndarray | list
4105+
if isinstance(loc, slice):
4106+
locs = np.arange(loc.start, loc.stop, loc.step)
4107+
elif is_scalar(loc):
4108+
locs = [loc]
4109+
else:
4110+
locs = loc.nonzero()[0]
4111+
4112+
return self.isetitem(locs, value)
41084113

41094114
if len(value.columns) != 1:
41104115
raise ValueError(

pandas/tests/frame/indexing/test_setitem.py

+16
Original file line numberDiff line numberDiff line change
@@ -1283,3 +1283,19 @@ def test_setitem_iloc_with_numpy_array(self, dtype):
12831283

12841284
expected = DataFrame({"a": [2, 1, 1]}, dtype=dtype)
12851285
tm.assert_frame_equal(df, expected)
1286+
1287+
def test_setitem_frame_dup_cols_dtype(self):
1288+
# GH#53143
1289+
df = DataFrame([[1, 2, 3, 4], [4, 5, 6, 7]], columns=["a", "b", "a", "c"])
1290+
rhs = DataFrame([[0, 1.5], [2, 2.5]], columns=["a", "a"])
1291+
df["a"] = rhs
1292+
expected = DataFrame(
1293+
[[0, 2, 1.5, 4], [2, 5, 2.5, 7]], columns=["a", "b", "a", "c"]
1294+
)
1295+
tm.assert_frame_equal(df, expected)
1296+
1297+
df = DataFrame([[1, 2, 3], [4, 5, 6]], columns=["a", "a", "b"])
1298+
rhs = DataFrame([[0, 1.5], [2, 2.5]], columns=["a", "a"])
1299+
df["a"] = rhs
1300+
expected = DataFrame([[0, 1.5, 3], [2, 2.5, 6]], columns=["a", "a", "b"])
1301+
tm.assert_frame_equal(df, expected)

0 commit comments

Comments
 (0)