1+ """XArray support for pandas_flavor."""
12from xarray import register_dataarray_accessor , register_dataset_accessor
23from functools import wraps
34
@@ -6,29 +7,67 @@ def make_accessor_wrapper(method):
67 """
78 Makes an XArray-compatible accessor to wrap a method to be added to an
89 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``.
1116 """
1217
1318 class XRAccessor :
19+ """XArray accessor for a method."""
20+
1421 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+ """
1527 self ._xr_obj = xr_obj
1628
1729 @wraps (method )
1830 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+
1941 return method (self ._xr_obj , * args , ** kwargs )
2042
2143 return XRAccessor
2244
2345
2446def 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+ """
2555 accessor_wrapper = make_accessor_wrapper (method )
2656 register_dataarray_accessor (method .__name__ )(accessor_wrapper )
2757
2858 return method
2959
3060
3161def 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+
3271 accessor_wrapper = make_accessor_wrapper (method )
3372 register_dataset_accessor (method .__name__ )(accessor_wrapper )
3473
0 commit comments