diff --git a/pandas/_testing/__init__.py b/pandas/_testing/__init__.py index 1035fd08a1a36..c15e597558221 100644 --- a/pandas/_testing/__init__.py +++ b/pandas/_testing/__init__.py @@ -459,7 +459,8 @@ def all_timeseries_index_generator(k: int = 10) -> Iterable[Index]: def make_rand_series(name=None, dtype=np.float64) -> Series: index = makeStringIndex(_N) data = np.random.randn(_N) - data = data.astype(dtype, copy=False) + with np.errstate(invalid="ignore"): + data = data.astype(dtype, copy=False) return Series(data, index=index, name=name) diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index f543efe20bf3c..21e04bc1badf3 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -1975,7 +1975,10 @@ def np_can_hold_element(dtype: np.dtype, element: Any) -> Any: if tipo.kind not in ["i", "u"]: if isinstance(element, np.ndarray) and element.dtype.kind == "f": # If all can be losslessly cast to integers, then we can hold them - casted = element.astype(dtype) + with np.errstate(invalid="ignore"): + # We check afterwards if cast was losslessly, so no need to show + # the warning + casted = element.astype(dtype) comp = casted == element if comp.all(): # Return the casted values bc they can be passed to diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index d415cbd035cd1..09737901cbbc6 100644 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -1990,21 +1990,17 @@ def _setitem_single_column(self, loc: int, value, plane_indexer) -> None: self.obj._clear_item_cache() return + self.obj._iset_item(loc, value) + # We will not operate in-place, but will attempt to in the future. # To determine whether we need to issue a FutureWarning, see if the # setting in-place would work, i.e. behavior will change. - if isinstance(value, ABCSeries): - warn = can_hold_element(orig_values, value._values) - else: - warn = can_hold_element(orig_values, value) - # Don't issue the warning yet, as we can still trim a few cases where - # behavior will not change. - - self.obj._iset_item(loc, value) + new_values = self.obj._get_column_array(loc) - if warn: - new_values = self.obj._get_column_array(loc) + if can_hold_element(orig_values, new_values): + # Don't issue the warning yet, as we can still trim a few cases where + # behavior will not change. if ( isinstance(new_values, np.ndarray) diff --git a/pandas/tests/frame/indexing/test_indexing.py b/pandas/tests/frame/indexing/test_indexing.py index 65ac5e2c2bd1e..7f84d8a367eac 100644 --- a/pandas/tests/frame/indexing/test_indexing.py +++ b/pandas/tests/frame/indexing/test_indexing.py @@ -1326,7 +1326,8 @@ def test_loc_expand_empty_frame_keep_midx_names(self): def test_loc_setitem_rhs_frame(self, idxr, val): # GH#47578 df = DataFrame({"a": [1, 2]}) - df.loc[:, "a"] = DataFrame({"a": [val, 11]}, index=[1, 2]) + with tm.assert_produces_warning(None): + df.loc[:, idxr] = DataFrame({"a": [val, 11]}, index=[1, 2]) expected = DataFrame({"a": [np.nan, val]}) tm.assert_frame_equal(df, expected) diff --git a/pandas/tests/frame/methods/test_diff.py b/pandas/tests/frame/methods/test_diff.py index fc804836f9a9b..9a9fea3462752 100644 --- a/pandas/tests/frame/methods/test_diff.py +++ b/pandas/tests/frame/methods/test_diff.py @@ -82,7 +82,7 @@ def test_diff_datetime_axis0_with_nat(self, tz): expected = Series(ex_index).to_frame() tm.assert_frame_equal(result, expected) - @pytest.mark.parametrize("tz", [None, "UTC"]) + @pytest.mark.parametrize("tz", [pytest.param(None, marks=pytest.mark.xfail), "UTC"]) def test_diff_datetime_with_nat_zero_periods(self, tz): # diff on NaT values should give NaT, not timedelta64(0) dti = date_range("2016-01-01", periods=4, tz=tz) @@ -91,8 +91,7 @@ def test_diff_datetime_with_nat_zero_periods(self, tz): df[1] = ser.copy() - msg = "will attempt to set the values inplace instead" - with tm.assert_produces_warning(FutureWarning, match=msg): + with tm.assert_produces_warning(None): df.iloc[:, 0] = pd.NaT expected = df - df diff --git a/pandas/tests/frame/test_nonunique_indexes.py b/pandas/tests/frame/test_nonunique_indexes.py index cd6397b053803..2c28800fb181f 100644 --- a/pandas/tests/frame/test_nonunique_indexes.py +++ b/pandas/tests/frame/test_nonunique_indexes.py @@ -1,6 +1,8 @@ import numpy as np import pytest +from pandas.compat import is_platform_windows + import pandas as pd from pandas import ( DataFrame, @@ -320,7 +322,9 @@ def test_dup_columns_across_dtype(self): def test_set_value_by_index(self, using_array_manager): # See gh-12344 - warn = FutureWarning if using_array_manager else None + warn = ( + FutureWarning if using_array_manager and not is_platform_windows() else None + ) msg = "will attempt to set the values inplace" df = DataFrame(np.arange(9).reshape(3, 3).T)