Skip to content

Commit 5655571

Browse files
committed
add a apply_to_dataset method
1 parent a4bb7e1 commit 5655571

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

xarray/core/common.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,21 @@ def pipe(
635635
else:
636636
return func(self, *args, **kwargs)
637637

638+
def apply_to_dataset(self, f):
639+
from .dataarray import DataArray
640+
641+
if isinstance(self, DataArray):
642+
ds = self._to_temp_dataset()
643+
else:
644+
ds = self
645+
646+
result = f(ds)
647+
648+
if isinstance(self, DataArray):
649+
return self._from_temp_dataset(result, name=self.name)
650+
else:
651+
return result
652+
638653
def groupby(self, group, squeeze: bool = True, restore_coord_dims: bool = None):
639654
"""Returns a GroupBy object for performing grouped operations.
640655

0 commit comments

Comments
 (0)