-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG raise pdep6 warning for loc full setter #56146
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 43 commits
4644c00
da7efad
9e400c2
003b993
3ff469f
e561aa5
cd9408e
58ce78e
6564773
4c345e4
ea51b08
388ded7
14c9ff6
846b529
4f5495c
9c26bb3
3f82a91
874b596
162586c
467742c
5d24fb0
a43b737
24afe06
20c89a2
7a25a86
3240bb9
755ded6
28a731b
27bf409
b1a4777
5b43509
191ce85
428c146
12cd484
cd208ca
3dd4d36
45dd43b
54f3459
96ee0ae
0bc2205
e3b04b7
c81c486
9d604da
c1fa7f3
7b0e3c8
183a609
ee0f35d
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 |
---|---|---|
|
@@ -2134,6 +2134,26 @@ def _setitem_single_column(self, loc: int, value, plane_indexer) -> None: | |
# If we're setting an entire column and we can't do it inplace, | ||
# then we can use value's dtype (or inferred dtype) | ||
# instead of object | ||
dtype = self.obj.dtypes.iloc[loc] | ||
if dtype not in (np.void, object) and not self.obj.empty: | ||
# - Exclude np.void, as that is a special case for expansion. | ||
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. big fan of this very clear comment! |
||
# We want to warn for | ||
# df = pd.DataFrame({'a': [1, 2]}) | ||
# df.loc[:, 'a'] = .3 | ||
# but not for | ||
# df = pd.DataFrame({'a': [1, 2]}) | ||
# df.loc[:, 'b'] = .3 | ||
# - Exclude `object`, as then no upcasting happens. | ||
# - Exclude empty initial object with enlargement, | ||
# as then there's nothing to be inconsistent with. | ||
warnings.warn( | ||
f"Setting an item of incompatible dtype is deprecated " | ||
"and will raise in a future error of pandas. " | ||
f"Value '{value}' has dtype incompatible with {dtype}, " | ||
"please explicitly cast to a compatible dtype first.", | ||
FutureWarning, | ||
stacklevel=find_stack_level(), | ||
) | ||
self.obj.isetitem(loc, value) | ||
else: | ||
# set value into the column (first attempting to operate inplace, then | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -498,6 +498,9 @@ def coerce_to_target_dtype(self, other, warn_on_upcast: bool = False) -> Block: | |
and is_integer_dtype(self.values.dtype) | ||
and isna(other) | ||
and other is not NaT | ||
and not ( | ||
isinstance(other, (np.datetime64, np.timedelta64)) and np.isnat(other) | ||
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. i think the isnat here is unnecessary since we already have isna(other) above 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. the idea of the this This is the case if |
||
) | ||
): | ||
warn_on_upcast = False | ||
elif ( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -199,7 +199,7 @@ def f_1(grp): | |
with tm.assert_produces_warning(FutureWarning, match=msg): | ||
result = df.groupby("A").apply(f_1)[["B"]] | ||
e = expected.copy() | ||
e.loc["Tiger"] = np.nan | ||
e["B"] = [3, 5, float("nan")] | ||
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. why is this changing? any reason for 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. indeed, it didn't need to change, thanks |
||
tm.assert_frame_equal(result, e) | ||
|
||
def f_2(grp): | ||
|
@@ -211,7 +211,7 @@ def f_2(grp): | |
with tm.assert_produces_warning(FutureWarning, match=msg): | ||
result = df.groupby("A").apply(f_2)[["B"]] | ||
e = expected.copy() | ||
e.loc["Pony"] = np.nan | ||
e["B"] = [3, float("nan"), 0] | ||
tm.assert_frame_equal(result, e) | ||
|
||
# 5592 revisited, with datetimes | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -491,7 +491,7 @@ def _check_setitem_invalid(self, ser, invalid, indexer, warn): | |
np.datetime64("NaT"), | ||
np.timedelta64("NaT"), | ||
] | ||
_indexers = [0, [0], slice(0, 1), [True, False, False]] | ||
_indexers = [0, [0], slice(0, 1), [True, False, False], slice(None, None, None)] | ||
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 looks similar to the DataFrame test above. lets try to de-duplicate it one of these days |
||
|
||
@pytest.mark.parametrize( | ||
"invalid", _invalid_scalars + [1, 1.0, np.int64(1), np.float64(1)] | ||
|
@@ -505,7 +505,7 @@ def test_setitem_validation_scalar_bool(self, invalid, indexer): | |
@pytest.mark.parametrize("indexer", _indexers) | ||
def test_setitem_validation_scalar_int(self, invalid, any_int_numpy_dtype, indexer): | ||
ser = Series([1, 2, 3], dtype=any_int_numpy_dtype) | ||
if isna(invalid) and invalid is not NaT: | ||
if isna(invalid) and invalid is not NaT and not np.isnat(invalid): | ||
warn = None | ||
else: | ||
warn = FutureWarning | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should "PDEP6" here get some type of colons or backticks or something to become a link? (i dont care that much)