Skip to content

Commit 3f34b95

Browse files
committed
Allow calling Dataset.map with a str method name
1 parent 4261fa1 commit 3f34b95

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

doc/whats-new.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ New Features
5959
:py:meth:`core.groupby.DatasetGroupBy.quantile`, :py:meth:`core.groupby.DataArrayGroupBy.quantile`
6060
(:issue:`3843`, :pull:`3844`)
6161
By `Aaron Spring <https://github.com/aaronspring>`_.
62+
- Allow calling :py:meth:`Dataset.map` with a str for the function, which
63+
will call the method with that name on each variable.
64+
- Implement :py:meth:`Dataset.idxmax`, :py:meth:`Dataset.idxmin`,
65+
:py:meth:`DataArray.idxmax`, and :py:meth:`DataArray.idxmin`.
66+
(:issue:`60`, :pull:`xxxx`)
67+
By `Todd Jennings <https://github.com/toddrjen>`_.
6268

6369

6470
Bug fixes

xarray/core/dataset.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4284,7 +4284,7 @@ def reduce(
42844284

42854285
def map(
42864286
self,
4287-
func: Callable,
4287+
func: Union[Callable, str],
42884288
keep_attrs: bool = None,
42894289
args: Iterable[Any] = (),
42904290
**kwargs: Any,
@@ -4293,10 +4293,11 @@ def map(
42934293
42944294
Parameters
42954295
----------
4296-
func : callable
4296+
func : callable or str
42974297
Function which can be called in the form `func(x, *args, **kwargs)`
42984298
to transform each DataArray `x` in this dataset into another
4299-
DataArray.
4299+
DataArray. Alternatively the string name of a method can be used,
4300+
which will be called individually on each data variable.
43004301
keep_attrs : bool, optional
43014302
If True, the dataset's attributes (`attrs`) will be copied from
43024303
the original object to the new one. If False, the new object will
@@ -4330,10 +4331,16 @@ def map(
43304331
foo (dim_0, dim_1) float64 0.3751 1.951 1.945 0.2948 0.711 0.3948
43314332
bar (x) float64 1.0 2.0
43324333
"""
4333-
variables = {
4334-
k: maybe_wrap_array(v, func(v, *args, **kwargs))
4335-
for k, v in self.data_vars.items()
4336-
}
4334+
if isinstance(func, str):
4335+
variables = {
4336+
k: maybe_wrap_array(v, getattr(v, func)(*args, **kwargs))
4337+
for k, v in self.data_vars.items()
4338+
}
4339+
else:
4340+
variables = {
4341+
k: maybe_wrap_array(v, func(v, *args, **kwargs))
4342+
for k, v in self.data_vars.items()
4343+
}
43374344
if keep_attrs is None:
43384345
keep_attrs = _get_keep_attrs(default=False)
43394346
attrs = self.attrs if keep_attrs else None

xarray/tests/test_dataset.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4782,6 +4782,7 @@ def test_map(self):
47824782
data.attrs["foo"] = "bar"
47834783

47844784
assert_identical(data.map(np.mean), data.mean())
4785+
assert_identical(data.map("mean"), data.mean())
47854786

47864787
expected = data.mean(keep_attrs=True)
47874788
actual = data.map(lambda x: x.mean(keep_attrs=True), keep_attrs=True)

0 commit comments

Comments
 (0)