-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: Bug in loc did not change dtype when complete column was assigned #37749
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
Changes from 13 commits
6450a2c
1599c5c
4d39612
f9f37cb
5cf355b
8d203f9
e35e009
4c391da
71fbf9f
babcd38
caa6046
8b95236
3b98ee0
f9b8a59
4bef38e
27ea3e2
f94277b
279e812
d5f6150
706dc6a
66d4b4e
fa25075
3c06ba6
a33659c
0f556c4
181e62a
b759ac9
a353930
d28e1e1
1aa8522
1bc0d46
61aab16
14fe5a8
26b5d6f
913ffea
e6e22f3
23f6f3b
99b87c9
f97a252
700ce6c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
|
||
from pandas.core.dtypes.common import ( | ||
is_array_like, | ||
is_dtype_equal, | ||
is_hashable, | ||
is_integer, | ||
is_iterator, | ||
|
@@ -1550,6 +1551,14 @@ def _setitem_with_indexer(self, indexer, value): | |
val = list(value.values()) if isinstance(value, dict) else value | ||
blk = self.obj._mgr.blocks[0] | ||
take_split_path = not blk._can_hold_element(val) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this can be the else condtiion There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, value can be anything from int, float to numpy array. I think this check is only necessary if we have Series or DataFrame. Maybe with an array? |
||
if not take_split_path: | ||
if isinstance(value, ABCSeries): | ||
take_split_path = not (is_dtype_equal(value.dtype, blk.dtype)) | ||
elif isinstance(value, ABCDataFrame): | ||
dtypes = list(value.dtypes.unique()) | ||
take_split_path = not ( | ||
len(dtypes) == 1 and is_dtype_equal(dtypes[0], blk.dtype) | ||
) | ||
|
||
# if we have any multi-indexes that have non-trivial slices | ||
# (not null slices) then we must take the split path, xref | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
date_range, | ||
notna, | ||
period_range, | ||
to_datetime, | ||
) | ||
import pandas._testing as tm | ||
from pandas.core.arrays import SparseArray | ||
|
@@ -298,6 +299,40 @@ def test_iloc_setitem_bool_indexer(self, klass): | |
expected = DataFrame({"flag": ["x", "y", "z"], "value": [2, 3, 4]}) | ||
tm.assert_frame_equal(df, expected) | ||
|
||
@pytest.mark.parametrize("dtype", ["int64", "Int64"]) | ||
def test_setitem_complete_columns_different_dtypes(self, dtype): | ||
jbrockmendel marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you make these There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I don't understand it. I should rename the tests to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one would be basically the goal is to establish a pattern in test naming for indexing tests with test_{method_name}{key_description}... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thx for the explanation. Could we maybe document this somewhere a bit more extensively? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
at some point i hope. the pattern isnt exactly in place yet |
||
# GH: 20635 | ||
df = DataFrame({"A": ["a", "b"], "B": ["1", "2"], "C": ["3", "4"], "D": [1, 2]}) | ||
rhs = df.loc[:, ["B", "C"]].astype("int64").astype(dtype) | ||
df.loc[:, ["B", "C"]] = rhs | ||
expected = DataFrame({"A": ["a", "b"], "B": [1, 2], "C": [3, 4], "D": [1, 2]}) | ||
expected[["B", "C"]] = expected[["B", "C"]].astype(dtype) | ||
tm.assert_frame_equal(df, expected) | ||
|
||
def test_setitem_single_column_as_series_different_dtype(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same comment about (add to the original frame) and test Int64 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
# GH: 20635 | ||
df = DataFrame({"A": ["a", "b"], "B": ["1", "2"], "C": ["3", "4"]}) | ||
df.loc[:, "C"] = df.loc[:, "C"].astype("int64") | ||
expected = DataFrame({"A": ["a", "b"], "B": ["1", "2"], "C": [3, 4]}) | ||
tm.assert_frame_equal(df, expected) | ||
|
||
def test_setitem_conversion_to_datetime(self): | ||
jbrockmendel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# GH: 20511 | ||
df = DataFrame( | ||
[["2015-01-01", "2016-01-01"], ["2016-01-01", "2015-01-01"]], | ||
columns=["date0", "date1"], | ||
) | ||
df.iloc[:, [0]] = df.iloc[:, [0]].apply( | ||
lambda x: to_datetime(x, errors="coerce") | ||
) | ||
jbrockmendel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
expected = DataFrame( | ||
{ | ||
"date0": [to_datetime("2015-01-01"), to_datetime("2016-01-01")], | ||
"date1": ["2016-01-01", "2015-01-01"], | ||
} | ||
) | ||
tm.assert_frame_equal(df, expected) | ||
|
||
|
||
class TestDataFrameSetItemSlicing: | ||
def test_setitem_slice_position(self): | ||
|
Uh oh!
There was an error while loading. Please reload this page.