File tree Expand file tree Collapse file tree 3 files changed +43
-7
lines changed Expand file tree Collapse file tree 3 files changed +43
-7
lines changed Original file line number Diff line number Diff line change 2
2
register_series_accessor ,
3
3
register_dataframe_method ,
4
4
register_dataframe_accessor )
5
+ from .xarray import (register_xarray_dataarray_method ,
6
+ register_xarray_dataset_method )
7
+
Original file line number Diff line number Diff line change
1
+
2
+ import xarray as xr
3
+
4
+ from xarray import register_dataarray_accessor , register_dataset_accessor
5
+ from functools import wraps
6
+
7
+
8
+ def make_accessor_wrapper (method ):
9
+ """
10
+ Makes an XArray-compatible accessor to wrap a method to be added to an
11
+ xr.DataArray, xr.Dataset, or both.
12
+ :param method: A method which takes an XArray object and needed parameters.
13
+ :return: The result of calling ``method``.
14
+ """
15
+
16
+ class XRAccessor :
17
+ def __init__ (self , xr_obj ):
18
+ self ._xr_obj = xr_obj
19
+
20
+ @wraps (method )
21
+ def __call__ (self , * args , ** kwargs ):
22
+ return method (self ._xr_obj , * args , ** kwargs )
23
+
24
+ return XRAccessor
25
+
26
+
27
+ def register_xarray_dataarray_method (method : callable ):
28
+ accessor_wrapper = make_accessor_wrapper (method )
29
+ register_dataarray_accessor (method .__name__ )(accessor_wrapper )
30
+
31
+ return method
32
+
33
+
34
+ def register_xarray_dataset_method (method : callable ):
35
+ accessor_wrapper = make_accessor_wrapper (method )
36
+ register_dataset_accessor (method .__name__ )(accessor_wrapper )
37
+
38
+ return method
39
+
Original file line number Diff line number Diff line change 19
19
AUTHOR = 'Zach Sailer'
20
20
21
21
# What packages are required for this module to be executed?
22
- REQUIRED = ["pandas" ]
22
+ REQUIRED = ["pandas" , "xarray" ]
23
23
24
24
# The rest you shouldn't have to touch too much :)
25
25
# ------------------------------------------------
@@ -82,12 +82,6 @@ def run(self):
82
82
author_email = EMAIL ,
83
83
url = URL ,
84
84
packages = find_packages (exclude = ('tests' ,)),
85
- # If your package is a single module, use this instead of 'packages':
86
- # py_modules=['mypackage'],
87
-
88
- # entry_points={
89
- # 'console_scripts': ['mycli=mymodule:cli'],
90
- # },
91
85
install_requires = REQUIRED ,
92
86
include_package_data = True ,
93
87
license = 'MIT' ,
You can’t perform that action at this time.
0 commit comments