Skip to content

Commit

Permalink
Backport PR #48397 on branch 1.5.x (WARN: Remove false positive warni…
Browse files Browse the repository at this point in the history
…ng for iloc inplaceness) (#48583)

Backport PR #48397: WARN: Remove false positive warning for iloc inplaceness

Co-authored-by: Patrick Hoefler <[email protected]>
  • Loading branch information
meeseeksmachine and phofl authored Sep 16, 2022
1 parent 3f91207 commit dfc00bf
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 17 deletions.
3 changes: 2 additions & 1 deletion pandas/_testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
5 changes: 4 additions & 1 deletion pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -1970,7 +1970,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
Expand Down
16 changes: 6 additions & 10 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1994,21 +1994,17 @@ def _setitem_single_column(self, loc: int, value, plane_indexer):
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)
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/frame/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
5 changes: 2 additions & 3 deletions pandas/tests/frame/methods/test_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
6 changes: 5 additions & 1 deletion pandas/tests/frame/test_nonunique_indexes.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit dfc00bf

Please sign in to comment.