1
+ """XArray support for pandas_flavor."""
1
2
from xarray import register_dataarray_accessor , register_dataset_accessor
2
3
from functools import wraps
3
4
@@ -6,29 +7,67 @@ def make_accessor_wrapper(method):
6
7
"""
7
8
Makes an XArray-compatible accessor to wrap a method to be added to an
8
9
xr.DataArray, xr.Dataset, or both.
9
- :param method: A method which takes an XArray object and needed parameters.
10
- :return: The result of calling ``method``.
10
+
11
+ Args:
12
+ method: A method which takes an XArray object and needed parameters.
13
+
14
+ Returns:
15
+ The result of calling ``method``.
11
16
"""
12
17
13
18
class XRAccessor :
19
+ """XArray accessor for a method."""
20
+
14
21
def __init__ (self , xr_obj ):
22
+ """Initialize the accessor.
23
+
24
+ Args:
25
+ xr_obj: The XArray object to which the accessor is attached.
26
+ """
15
27
self ._xr_obj = xr_obj
16
28
17
29
@wraps (method )
18
30
def __call__ (self , * args , ** kwargs ):
31
+ """Call the method.
32
+
33
+ Args:
34
+ *args: Positional arguments to pass to the method.
35
+ **kwargs: Keyword arguments to pass to the method.
36
+
37
+ Returns:
38
+ The result of calling ``method``.
39
+ """
40
+
19
41
return method (self ._xr_obj , * args , ** kwargs )
20
42
21
43
return XRAccessor
22
44
23
45
24
46
def register_xarray_dataarray_method (method : callable ):
47
+ """Register a method on an XArray DataArray object.
48
+
49
+ Args:
50
+ method: A method which takes an XArray object and needed parameters.
51
+
52
+ Returns:
53
+ The method.
54
+ """
25
55
accessor_wrapper = make_accessor_wrapper (method )
26
56
register_dataarray_accessor (method .__name__ )(accessor_wrapper )
27
57
28
58
return method
29
59
30
60
31
61
def register_xarray_dataset_method (method : callable ):
62
+ """Register a method on an XArray Dataset object.
63
+
64
+ Args:
65
+ method: A method which takes an XArray object and needed parameters.
66
+
67
+ Returns:
68
+ The method.
69
+ """
70
+
32
71
accessor_wrapper = make_accessor_wrapper (method )
33
72
register_dataset_accessor (method .__name__ )(accessor_wrapper )
34
73
0 commit comments