Skip to content

BUG: isetitem coercing df columns to object #50175

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 6 commits into from
Jan 4, 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,7 @@ Indexing
- Bug in :meth:`DataFrame.iloc` raising ``IndexError`` when indexer is a :class:`Series` with numeric extension array dtype (:issue:`49521`)
- Bug in :func:`~DataFrame.describe` when formatting percentiles in the resulting index showed more decimals than needed (:issue:`46362`)
- Bug in :meth:`DataFrame.compare` does not recognize differences when comparing ``NA`` with value in nullable dtypes (:issue:`48939`)
- Bug in :meth:`DataFrame.isetitem` coercing extension array dtypes in :class:`DataFrame` to object (:issue:`49922`)
-

Missing
Expand Down
9 changes: 9 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3822,6 +3822,15 @@ def isetitem(self, loc, value) -> None:
In cases where `frame.columns` is unique, this is equivalent to
`frame[frame.columns[i]] = value`.
"""
if isinstance(value, DataFrame):
if is_scalar(loc):
loc = [loc]

for i, idx in enumerate(loc):
arraylike = self._sanitize_column(value.iloc[:, i])
self._iset_item_mgr(idx, arraylike, inplace=False)
Comment on lines +3829 to +3831
Copy link
Member

Choose a reason for hiding this comment

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

in the tests you've added, i and idx are always the same - is it possible to add a test where i and idx differ?

Copy link
Member

@MarcoGorelli MarcoGorelli Dec 30, 2022

Choose a reason for hiding this comment

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

as in, if I changed this to

                arraylike = self._sanitize_column(value.iloc[:, i])
                self._iset_item_mgr(i, arraylike, inplace=False)

then there should be a test which fails

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch, adjusted one of the tests

return

arraylike = self._sanitize_column(value)
self._iset_item_mgr(loc, arraylike, inplace=False)

Expand Down
37 changes: 37 additions & 0 deletions pandas/tests/frame/methods/test_isetitem.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from pandas import (
DataFrame,
Series,
)
import pandas._testing as tm


class TestDataFrameSetItem:
def test_isetitem_ea_df(self):
# GH#49922
df = DataFrame([[1, 2, 3], [4, 5, 6]])
rhs = DataFrame([[11, 12], [13, 14]], dtype="Int64")

df.isetitem([0, 1], rhs)
expected = DataFrame(
{
0: Series([11, 13], dtype="Int64"),
1: Series([12, 14], dtype="Int64"),
2: [3, 6],
}
)
tm.assert_frame_equal(df, expected)

def test_isetitem_ea_df_scalar_indexer(self):
# GH#49922
df = DataFrame([[1, 2, 3], [4, 5, 6]])
rhs = DataFrame([[11], [13]], dtype="Int64")

df.isetitem(2, rhs)
expected = DataFrame(
{
0: [1, 4],
1: [2, 5],
2: Series([11, 13], dtype="Int64"),
}
)
tm.assert_frame_equal(df, expected)