-
-
Notifications
You must be signed in to change notification settings - Fork 19.4k
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 7 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,13 @@ 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) | ||
|
||
| if isinstance(value, ABCSeries): | ||
jbrockmendel marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| take_split_path = not (is_dtype_equal(value.dtype, blk.dtype)) | ||
jbrockmendel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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,36 @@ def test_iloc_setitem_bool_indexer(self, klass): | |
| expected = DataFrame({"flag": ["x", "y", "z"], "value": [2, 3, 4]}) | ||
| tm.assert_frame_equal(df, expected) | ||
|
|
||
| def test_setitem_complete_columns_different_dtypes(self): | ||
|
||
| # GH: 20635 | ||
| df = DataFrame({"A": ["a", "b"], "B": ["1", "2"], "C": ["3", "4"]}) | ||
| df.loc[:, ["B", "C"]] = df.loc[:, ["B", "C"]].astype("int64") | ||
| expected = DataFrame({"A": ["a", "b"], "B": [1, 2], "C": [3, 4]}, dtype="int64") | ||
| tm.assert_frame_equal(df, expected) | ||
|
|
||
| def test_setitem_single_column_as_series_different_dtype(self): | ||
|
||
| # 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.
Outdated
Show resolved
Hide resolved
|
||
| # GH: 20511 | ||
| df = DataFrame( | ||
| [["2015-01-01", "2016-01-01"], ["2016-01-01", "2015-01-01"]] | ||
| ).add_prefix("date") | ||
jbrockmendel marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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.