Skip to content

Commit d02ba1c

Browse files
authored
Merge pull request #15 from eyaltrabelsi/master
support xarray
2 parents 4a14d61 + b682821 commit d02ba1c

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

pandas_flavor/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@
22
register_series_accessor,
33
register_dataframe_method,
44
register_dataframe_accessor)
5+
from .xarray import (register_xarray_dataarray_method,
6+
register_xarray_dataset_method)
7+

pandas_flavor/xarray.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+

setup.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
AUTHOR = 'Zach Sailer'
2020

2121
# What packages are required for this module to be executed?
22-
REQUIRED = ["pandas"]
22+
REQUIRED = ["pandas","xarray"]
2323

2424
# The rest you shouldn't have to touch too much :)
2525
# ------------------------------------------------
@@ -82,12 +82,6 @@ def run(self):
8282
author_email=EMAIL,
8383
url=URL,
8484
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-
# },
9185
install_requires=REQUIRED,
9286
include_package_data=True,
9387
license='MIT',

0 commit comments

Comments
 (0)