Skip to content

Commit 5b1f2d7

Browse files
phoflmeeseeksmachine
authored andcommitted
Backport PR pandas-dev#48397: WARN: Remove false positive warning for iloc inplaceness
1 parent 9e8d859 commit 5b1f2d7

File tree

6 files changed

+21
-17
lines changed

6 files changed

+21
-17
lines changed

pandas/_testing/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,8 @@ def all_timeseries_index_generator(k: int = 10) -> Iterable[Index]:
459459
def make_rand_series(name=None, dtype=np.float64) -> Series:
460460
index = makeStringIndex(_N)
461461
data = np.random.randn(_N)
462-
data = data.astype(dtype, copy=False)
462+
with np.errstate(invalid="ignore"):
463+
data = data.astype(dtype, copy=False)
463464
return Series(data, index=index, name=name)
464465

465466

pandas/core/dtypes/cast.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1970,7 +1970,10 @@ def np_can_hold_element(dtype: np.dtype, element: Any) -> Any:
19701970
if tipo.kind not in ["i", "u"]:
19711971
if isinstance(element, np.ndarray) and element.dtype.kind == "f":
19721972
# If all can be losslessly cast to integers, then we can hold them
1973-
casted = element.astype(dtype)
1973+
with np.errstate(invalid="ignore"):
1974+
# We check afterwards if cast was losslessly, so no need to show
1975+
# the warning
1976+
casted = element.astype(dtype)
19741977
comp = casted == element
19751978
if comp.all():
19761979
# Return the casted values bc they can be passed to

pandas/core/indexing.py

+6-10
Original file line numberDiff line numberDiff line change
@@ -1994,21 +1994,17 @@ def _setitem_single_column(self, loc: int, value, plane_indexer):
19941994
self.obj._clear_item_cache()
19951995
return
19961996

1997+
self.obj._iset_item(loc, value)
1998+
19971999
# We will not operate in-place, but will attempt to in the future.
19982000
# To determine whether we need to issue a FutureWarning, see if the
19992001
# setting in-place would work, i.e. behavior will change.
2000-
if isinstance(value, ABCSeries):
2001-
warn = can_hold_element(orig_values, value._values)
2002-
else:
2003-
warn = can_hold_element(orig_values, value)
20042002

2005-
# Don't issue the warning yet, as we can still trim a few cases where
2006-
# behavior will not change.
2007-
2008-
self.obj._iset_item(loc, value)
2003+
new_values = self.obj._get_column_array(loc)
20092004

2010-
if warn:
2011-
new_values = self.obj._get_column_array(loc)
2005+
if can_hold_element(orig_values, new_values):
2006+
# Don't issue the warning yet, as we can still trim a few cases where
2007+
# behavior will not change.
20122008

20132009
if (
20142010
isinstance(new_values, np.ndarray)

pandas/tests/frame/indexing/test_indexing.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1326,7 +1326,8 @@ def test_loc_expand_empty_frame_keep_midx_names(self):
13261326
def test_loc_setitem_rhs_frame(self, idxr, val):
13271327
# GH#47578
13281328
df = DataFrame({"a": [1, 2]})
1329-
df.loc[:, "a"] = DataFrame({"a": [val, 11]}, index=[1, 2])
1329+
with tm.assert_produces_warning(None):
1330+
df.loc[:, idxr] = DataFrame({"a": [val, 11]}, index=[1, 2])
13301331
expected = DataFrame({"a": [np.nan, val]})
13311332
tm.assert_frame_equal(df, expected)
13321333

pandas/tests/frame/methods/test_diff.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def test_diff_datetime_axis0_with_nat(self, tz):
8282
expected = Series(ex_index).to_frame()
8383
tm.assert_frame_equal(result, expected)
8484

85-
@pytest.mark.parametrize("tz", [None, "UTC"])
85+
@pytest.mark.parametrize("tz", [pytest.param(None, marks=pytest.mark.xfail), "UTC"])
8686
def test_diff_datetime_with_nat_zero_periods(self, tz):
8787
# diff on NaT values should give NaT, not timedelta64(0)
8888
dti = date_range("2016-01-01", periods=4, tz=tz)
@@ -91,8 +91,7 @@ def test_diff_datetime_with_nat_zero_periods(self, tz):
9191

9292
df[1] = ser.copy()
9393

94-
msg = "will attempt to set the values inplace instead"
95-
with tm.assert_produces_warning(FutureWarning, match=msg):
94+
with tm.assert_produces_warning(None):
9695
df.iloc[:, 0] = pd.NaT
9796

9897
expected = df - df

pandas/tests/frame/test_nonunique_indexes.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import numpy as np
22
import pytest
33

4+
from pandas.compat import is_platform_windows
5+
46
import pandas as pd
57
from pandas import (
68
DataFrame,
@@ -320,7 +322,9 @@ def test_dup_columns_across_dtype(self):
320322

321323
def test_set_value_by_index(self, using_array_manager):
322324
# See gh-12344
323-
warn = FutureWarning if using_array_manager else None
325+
warn = (
326+
FutureWarning if using_array_manager and not is_platform_windows() else None
327+
)
324328
msg = "will attempt to set the values inplace"
325329

326330
df = DataFrame(np.arange(9).reshape(3, 3).T)

0 commit comments

Comments
 (0)