Skip to content

Commit de2e68b

Browse files
differentiate between generic warning and inplace methods -> Py314 still skips check for inplace methods
1 parent 798fcdc commit de2e68b

File tree

18 files changed

+55
-48
lines changed

18 files changed

+55
-48
lines changed

pandas/_testing/contexts.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
import uuid
1414

1515
from pandas.compat import (
16-
PYPY,
17-
WARNING_CHECK_DISABLED,
16+
CHAINED_WARNING_DISABLED,
17+
CHAINED_WARNING_DISABLED_INPLACE_METHOD,
1818
)
1919
from pandas.errors import ChainedAssignmentError
2020

@@ -163,10 +163,18 @@ def with_csv_dialect(name: str, **kwargs) -> Generator[None]:
163163
csv.unregister_dialect(name)
164164

165165

166-
def raises_chained_assignment_error(extra_warnings=(), extra_match=()):
166+
def raises_chained_assignment_error(
167+
extra_warnings=(), extra_match=(), inplace_method=False
168+
):
167169
from pandas._testing import assert_produces_warning
168170

169-
if PYPY or WARNING_CHECK_DISABLED:
171+
WARNING_DISABLED = (
172+
CHAINED_WARNING_DISABLED_INPLACE_METHOD
173+
if inplace_method
174+
else CHAINED_WARNING_DISABLED
175+
)
176+
177+
if WARNING_DISABLED:
170178
if not extra_warnings:
171179
from contextlib import nullcontext
172180

pandas/compat/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616
from typing import TYPE_CHECKING
1717

1818
from pandas.compat._constants import (
19+
CHAINED_WARNING_DISABLED,
20+
CHAINED_WARNING_DISABLED_INPLACE_METHOD,
1921
IS64,
2022
ISMUSL,
2123
PY312,
2224
PY314,
2325
PYPY,
24-
WARNING_CHECK_DISABLED,
2526
WASM,
2627
)
2728
from pandas.compat.numpy import is_numpy_dev
@@ -152,14 +153,15 @@ def is_ci_environment() -> bool:
152153

153154

154155
__all__ = [
156+
"CHAINED_WARNING_DISABLED",
157+
"CHAINED_WARNING_DISABLED_INPLACE_METHOD",
155158
"HAS_PYARROW",
156159
"IS64",
157160
"ISMUSL",
158161
"PY312",
159162
"PY314",
160163
"PYARROW_MIN_VERSION",
161164
"PYPY",
162-
"WARNING_CHECK_DISABLED",
163165
"WASM",
164166
"is_numpy_dev",
165167
"pa_version_under14p0",

pandas/compat/_constants.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
WASM = (sys.platform == "emscripten") or (platform.machine() in ["wasm32", "wasm64"])
2020
ISMUSL = "musl" in (sysconfig.get_config_var("HOST_GNU_TYPE") or "")
2121
REF_COUNT = 2
22-
WARNING_CHECK_DISABLED = PY314
22+
CHAINED_WARNING_DISABLED = PYPY
23+
CHAINED_WARNING_DISABLED_INPLACE_METHOD = PYPY or PY314
2324

2425

2526
__all__ = [

pandas/core/frame.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,9 @@
5050
from pandas._libs.hashtable import duplicated
5151
from pandas._libs.internals import SetitemMixin
5252
from pandas._libs.lib import is_range_indexer
53-
from pandas.compat import PYPY
5453
from pandas.compat._constants import (
54+
CHAINED_WARNING_DISABLED_INPLACE_METHOD,
5555
REF_COUNT,
56-
WARNING_CHECK_DISABLED,
5756
)
5857
from pandas.compat._optional import import_optional_dependency
5958
from pandas.compat.numpy import function as nv
@@ -9314,7 +9313,7 @@ def update(
93149313
1 2 500.0
93159314
2 3 6.0
93169315
"""
9317-
if not PYPY and not WARNING_CHECK_DISABLED:
9316+
if not CHAINED_WARNING_DISABLED_INPLACE_METHOD:
93189317
if sys.getrefcount(self) <= REF_COUNT:
93199318
warnings.warn(
93209319
_chained_assignment_method_msg,

pandas/core/generic.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,9 @@
8282
WriteExcelBuffer,
8383
npt,
8484
)
85-
from pandas.compat import PYPY
8685
from pandas.compat._constants import (
86+
CHAINED_WARNING_DISABLED_INPLACE_METHOD,
8787
REF_COUNT,
88-
WARNING_CHECK_DISABLED,
8988
)
9089
from pandas.compat._optional import import_optional_dependency
9190
from pandas.compat.numpy import function as nv
@@ -7090,7 +7089,7 @@ def fillna(
70907089
"""
70917090
inplace = validate_bool_kwarg(inplace, "inplace")
70927091
if inplace:
7093-
if not PYPY and not WARNING_CHECK_DISABLED:
7092+
if not CHAINED_WARNING_DISABLED_INPLACE_METHOD:
70947093
if sys.getrefcount(self) <= REF_COUNT:
70957094
warnings.warn(
70967095
_chained_assignment_method_msg,
@@ -7337,7 +7336,7 @@ def ffill(
73377336
"""
73387337
inplace = validate_bool_kwarg(inplace, "inplace")
73397338
if inplace:
7340-
if not PYPY and not WARNING_CHECK_DISABLED:
7339+
if not CHAINED_WARNING_DISABLED_INPLACE_METHOD:
73417340
if sys.getrefcount(self) <= REF_COUNT:
73427341
warnings.warn(
73437342
_chained_assignment_method_msg,
@@ -7477,7 +7476,7 @@ def bfill(
74777476
"""
74787477
inplace = validate_bool_kwarg(inplace, "inplace")
74797478
if inplace:
7480-
if not PYPY and not WARNING_CHECK_DISABLED:
7479+
if not CHAINED_WARNING_DISABLED_INPLACE_METHOD:
74817480
if sys.getrefcount(self) <= REF_COUNT:
74827481
warnings.warn(
74837482
_chained_assignment_method_msg,
@@ -7562,7 +7561,7 @@ def replace(
75627561

75637562
inplace = validate_bool_kwarg(inplace, "inplace")
75647563
if inplace:
7565-
if not PYPY and not WARNING_CHECK_DISABLED:
7564+
if not CHAINED_WARNING_DISABLED_INPLACE_METHOD:
75667565
if sys.getrefcount(self) <= REF_COUNT:
75677566
warnings.warn(
75687567
_chained_assignment_method_msg,
@@ -7925,7 +7924,7 @@ def interpolate(
79257924
inplace = validate_bool_kwarg(inplace, "inplace")
79267925

79277926
if inplace:
7928-
if not PYPY and not WARNING_CHECK_DISABLED:
7927+
if not CHAINED_WARNING_DISABLED_INPLACE_METHOD:
79297928
if sys.getrefcount(self) <= REF_COUNT:
79307929
warnings.warn(
79317930
_chained_assignment_method_msg,
@@ -8509,7 +8508,7 @@ def clip(
85098508
inplace = validate_bool_kwarg(inplace, "inplace")
85108509

85118510
if inplace:
8512-
if not PYPY and not WARNING_CHECK_DISABLED:
8511+
if not CHAINED_WARNING_DISABLED_INPLACE_METHOD:
85138512
if sys.getrefcount(self) <= REF_COUNT:
85148513
warnings.warn(
85158514
_chained_assignment_method_msg,
@@ -10152,7 +10151,7 @@ def where(
1015210151
"""
1015310152
inplace = validate_bool_kwarg(inplace, "inplace")
1015410153
if inplace:
10155-
if not PYPY and not WARNING_CHECK_DISABLED:
10154+
if not CHAINED_WARNING_DISABLED_INPLACE_METHOD:
1015610155
if sys.getrefcount(self) <= REF_COUNT:
1015710156
warnings.warn(
1015810157
_chained_assignment_method_msg,
@@ -10216,7 +10215,7 @@ def mask(
1021610215
) -> Self | None:
1021710216
inplace = validate_bool_kwarg(inplace, "inplace")
1021810217
if inplace:
10219-
if not PYPY and not WARNING_CHECK_DISABLED:
10218+
if not CHAINED_WARNING_DISABLED_INPLACE_METHOD:
1022010219
if sys.getrefcount(self) <= REF_COUNT:
1022110220
warnings.warn(
1022210221
_chained_assignment_method_msg,

pandas/core/indexing.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@
1515

1616
from pandas._libs.indexing import NDFrameIndexerBase
1717
from pandas._libs.lib import item_from_zerodim
18-
from pandas.compat import PYPY
1918
from pandas.compat._constants import (
19+
CHAINED_WARNING_DISABLED,
2020
REF_COUNT,
21-
WARNING_CHECK_DISABLED,
2221
)
2322
from pandas.errors import (
2423
AbstractMethodError,
@@ -920,7 +919,7 @@ def _ensure_listlike_indexer(self, key, axis=None, value=None) -> None:
920919

921920
@final
922921
def __setitem__(self, key, value) -> None:
923-
if not PYPY and not WARNING_CHECK_DISABLED:
922+
if not CHAINED_WARNING_DISABLED:
924923
if sys.getrefcount(self.obj) <= REF_COUNT:
925924
warnings.warn(
926925
_chained_assignment_msg, ChainedAssignmentError, stacklevel=2
@@ -2588,7 +2587,7 @@ def __getitem__(self, key):
25882587
return super().__getitem__(key)
25892588

25902589
def __setitem__(self, key, value) -> None:
2591-
if not PYPY and not WARNING_CHECK_DISABLED:
2590+
if not CHAINED_WARNING_DISABLED:
25922591
if sys.getrefcount(self.obj) <= REF_COUNT:
25932592
warnings.warn(
25942593
_chained_assignment_msg, ChainedAssignmentError, stacklevel=2
@@ -2619,7 +2618,7 @@ def _convert_key(self, key):
26192618
return key
26202619

26212620
def __setitem__(self, key, value) -> None:
2622-
if not PYPY and not WARNING_CHECK_DISABLED:
2621+
if not CHAINED_WARNING_DISABLED:
26232622
if sys.getrefcount(self.obj) <= REF_COUNT:
26242623
warnings.warn(
26252624
_chained_assignment_msg, ChainedAssignmentError, stacklevel=2

pandas/core/series.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,9 @@
3535
)
3636
from pandas._libs.internals import SetitemMixin
3737
from pandas._libs.lib import is_range_indexer
38-
from pandas.compat import PYPY
3938
from pandas.compat._constants import (
39+
CHAINED_WARNING_DISABLED_INPLACE_METHOD,
4040
REF_COUNT,
41-
WARNING_CHECK_DISABLED,
4241
)
4342
from pandas.compat._optional import import_optional_dependency
4443
from pandas.compat.numpy import function as nv
@@ -3327,7 +3326,7 @@ def update(self, other: Series | Sequence | Mapping) -> None:
33273326
2 3
33283327
dtype: int64
33293328
"""
3330-
if not PYPY and not WARNING_CHECK_DISABLED:
3329+
if not CHAINED_WARNING_DISABLED_INPLACE_METHOD:
33313330
if sys.getrefcount(self) <= REF_COUNT:
33323331
warnings.warn(
33333332
_chained_assignment_method_msg,

pandas/tests/copy_view/test_chained_assignment_deprecation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import numpy as np
22
import pytest
33

4-
from pandas.compat import WARNING_CHECK_DISABLED
4+
from pandas.compat import CHAINED_WARNING_DISABLED
55
from pandas.errors import ChainedAssignmentError
66

77
from pandas import DataFrame
@@ -18,7 +18,7 @@ def test_series_setitem(indexer):
1818

1919
# using custom check instead of tm.assert_produces_warning because that doesn't
2020
# fail if multiple warnings are raised
21-
if WARNING_CHECK_DISABLED:
21+
if CHAINED_WARNING_DISABLED:
2222
return
2323
with pytest.warns() as record: # noqa: TID251
2424
df["a"][indexer] = 0

pandas/tests/copy_view/test_clip.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ def test_clip_no_op():
6363
def test_clip_chained_inplace():
6464
df = DataFrame({"a": [1, 4, 2], "b": 1})
6565
df_orig = df.copy()
66-
with tm.raises_chained_assignment_error():
66+
with tm.raises_chained_assignment_error(inplace_method=True):
6767
df["a"].clip(1, 2, inplace=True)
6868
tm.assert_frame_equal(df, df_orig)
6969

70-
with tm.raises_chained_assignment_error():
70+
with tm.raises_chained_assignment_error(inplace_method=True):
7171
df[["a"]].clip(1, 2, inplace=True)
7272
tm.assert_frame_equal(df, df_orig)

pandas/tests/copy_view/test_interp_fillna.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,11 +278,11 @@ def test_fillna_inplace_ea_noop_shares_memory(any_numeric_ea_and_arrow_dtype):
278278
def test_fillna_chained_assignment():
279279
df = DataFrame({"a": [1, np.nan, 2], "b": 1})
280280
df_orig = df.copy()
281-
with tm.raises_chained_assignment_error():
281+
with tm.raises_chained_assignment_error(inplace_method=True):
282282
df["a"].fillna(100, inplace=True)
283283
tm.assert_frame_equal(df, df_orig)
284284

285-
with tm.raises_chained_assignment_error():
285+
with tm.raises_chained_assignment_error(inplace_method=True):
286286
df[["a"]].fillna(100, inplace=True)
287287
tm.assert_frame_equal(df, df_orig)
288288

@@ -291,10 +291,10 @@ def test_fillna_chained_assignment():
291291
def test_interpolate_chained_assignment(func):
292292
df = DataFrame({"a": [1, np.nan, 2], "b": 1})
293293
df_orig = df.copy()
294-
with tm.raises_chained_assignment_error():
294+
with tm.raises_chained_assignment_error(inplace_method=True):
295295
getattr(df["a"], func)(inplace=True)
296296
tm.assert_frame_equal(df, df_orig)
297297

298-
with tm.raises_chained_assignment_error():
298+
with tm.raises_chained_assignment_error(inplace_method=True):
299299
getattr(df[["a"]], func)(inplace=True)
300300
tm.assert_frame_equal(df, df_orig)

0 commit comments

Comments
 (0)