Skip to content

Commit 7da9f80

Browse files
authored
Fixes for pandas v3 compatibility (#6948)
* Update env reqs to pandas 3 * Update env reqs to pandas 3 * Add copy kwarg checking * Add pylance ignores for decorator * Improve check copy decorator * Updates and fixes to test_pandas.py * Type checking fixes * Ruff fixes * Type checking fixes * Remove extraneous lock file * Further mypy clarification fixes * Yet more mypy-pytest intereaction fixing * Rebuild lock files * Test fixes * Whatsnew update * Whatsnew update 2 * MORE type fixing * Revert get_dimensional_metadata check * Code review responses * Update Whats New to reflect code reviewer * Further clarification of typing comment
1 parent 207d28d commit 7da9f80

File tree

9 files changed

+285
-191
lines changed

9 files changed

+285
-191
lines changed

docs/src/whatsnew/latest.rst

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ This document explains the changes made to Iris for this release
7575
testing conveniences in favour of the conveniences found in :mod:`iris/tests/_shared_utils.py`.
7676
(:pull:`6950`)
7777

78+
#. `@hsteptoe`_ has deprecated the use of the `copy` kwarg across :mod:`iris.pandas` to reflect changes
79+
to the default behaviour of pandas v3 `New pandas v3 copy behaviour`_. (:pull:`6948`)
80+
7881

7982
🔗 Dependencies
8083
===============
@@ -122,13 +125,21 @@ This document explains the changes made to Iris for this release
122125
from unittest to pytest. Iris is now also ruff-PT compliant, save for PT019.
123126
(:issue:`6212`, :pull:`6939`)
124127

128+
#. `@hsteptoe`_ and `@ESadek-MO`_ (reviewer) updated chained assignment useage within the tests
129+
associated with :mod:`iris.pandas` to reflect changes in pandas v3 `New pandas v3 copy behaviour`_.
130+
(:pull:`6948`, :issue:`6761`)
131+
132+
#. `@hsteptoe`_ and `@ESadek-MO`_ (reviewer) added static type hinting to :mod:`iris.pandas`. (:pull:`6948`)
133+
125134
.. comment
126135
Whatsnew author names (@github name) in alphabetical order. Note that,
127136
core dev names are automatically included by the common_links.inc:
128137
129138
.. _@hdyson: https://github.com/hdyson
130-
131-
.. _SPEC0 Minimum Supported Dependencies: https://scientific-python.org/specs/spec-0000/
139+
.. _@hsteptoe: https://github.com/hsteptoe
132140

133141
.. comment
134142
Whatsnew resources in alphabetical order:
143+
144+
.. _New pandas v3 copy behaviour: https://pandas.pydata.org/docs/whatsnew/v3.0.0.html#consistent-copy-view-behaviour-with-copy-on-write
145+
.. _SPEC0 Minimum Supported Dependencies: https://scientific-python.org/specs/spec-0000/

lib/iris/_deprecation.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,82 @@
44
# See LICENSE in the root of the repository for full licensing details.
55
"""Utilities for producing runtime deprecation messages."""
66

7+
from functools import wraps
8+
import inspect
79
import warnings
810

11+
from iris.warnings import IrisUserWarning
12+
13+
14+
def explicit_copy_checker(f):
15+
"""Check for explicitly set parameters in a function.
16+
17+
This is intended to be used as a decorator for functions that take a
18+
variable number of parameters, to allow the function to determine which
19+
parameters were explicitly set by the caller.
20+
21+
This can be helpful when wanting raise DeprecationWarning of function
22+
parameters, but only when they are explicitly set by the caller, and not
23+
when they are left at their default value.
24+
25+
Parameters
26+
----------
27+
f : function
28+
The function to be decorated. The function must have a signature that
29+
allows for variable parameters (e.g. ``*args`` and/or ``**kwargs``), and
30+
the parameters to be checked must be explicitly listed in the function
31+
signature (i.e. not just passed via ``**kwargs``).
32+
33+
Returns
34+
-------
35+
function
36+
The decorated function, which will have an additional keyword argument
37+
``explicit_params`` added to its signature. This argument will be a set
38+
of the names of the parameters that were explicitly set by the caller when
39+
calling the function.
40+
41+
Examples
42+
--------
43+
The following example shows how to use the ``explicit_copy_checker`` decorator to
44+
check for explicitly set parameters in a function, and raise a DeprecationWarning
45+
if a deprecated parameter is explicitly set by the caller.
46+
47+
>>> from iris._deprecation import explicit_copy_checker, IrisDeprecation
48+
>>> @explicit_copy_checker
49+
... def my_function(a, b=1):
50+
... print(f"a={a}, b={b}")
51+
... if "b" in kwargs["explicit_params"]:
52+
... warnings.warn("Parameter 'b' is deprecated.", IrisDeprecation)
53+
>>> my_function(1) # No warning, 'b' is not explicitly set
54+
>>> my_function(1, b=3) # Warning, 'b' is explicitly set
55+
56+
"""
57+
varnames = inspect.getfullargspec(f)[0]
58+
59+
@wraps(f)
60+
def wrapper(*a, **kw):
61+
explicit_params = set(list(varnames[: len(a)]) + list(kw.keys()))
62+
if "copy" in explicit_params:
63+
if kw["copy"] is False:
64+
msg = (
65+
"Pandas v3 behaviour defaults to copy=True. The `copy`"
66+
f" parameter in `{f.__name__}` is deprecated and"
67+
"will be removed in a future release."
68+
)
69+
warnings.warn(msg, category=IrisUserWarning)
70+
else:
71+
msg = (
72+
f"The `copy` parameter in `{f.__name__}` is deprecated and"
73+
" will be removed in a future release. The function will"
74+
" always make a copy of the data array, to ensure that the"
75+
" returned Cubes are independent of the input pandas data."
76+
)
77+
warn_deprecated(msg)
78+
else:
79+
return f(*a, **kw)
80+
81+
return wrapper
82+
983

1084
class IrisDeprecation(UserWarning):
1185
"""An Iris deprecation warning.

0 commit comments

Comments
 (0)