@@ -1091,6 +1091,50 @@ def reduce(self, func, dimension=None, keep_attrs=False, **kwargs):
1091
1091
1092
1092
return Dataset (variables = variables , attributes = attrs )
1093
1093
1094
+ def apply (self , func , to = None , keep_attrs = False , ** kwargs ):
1095
+ """Apply a function over noncoordinates in this dataset.
1096
+
1097
+ Parameters
1098
+ ----------
1099
+ func : function
1100
+ Function which can be called in the form `f(x, **kwargs)` to
1101
+ transform each DataArray `x` in this dataset into another
1102
+ DataArray.
1103
+ to : str or sequence of str, optional
1104
+ Explicit list of noncoordinates in this dataset to which to apply
1105
+ `func`. Unlisted noncoordinates are passed through unchanged. By
1106
+ default, `func` is applied to all noncoordinates in this dataset.
1107
+ keep_attrs : bool, optional
1108
+ If True, the datasets's attributes (`attrs`) will be copied from
1109
+ the original object to the new one. If False, the new object will
1110
+ be returned without attributes.
1111
+ **kwargs : dict
1112
+ Additional keyword arguments passed on to `func`.
1113
+
1114
+ Returns
1115
+ -------
1116
+ applied : Dataset
1117
+ Resulting dataset from applying over each noncoordinate.
1118
+ Coordinates which are no longer used as the dimension of a
1119
+ noncoordinate are dropped.
1120
+ """
1121
+ if to is not None :
1122
+ to = set ([to ] if isinstance (to , basestring ) else to )
1123
+ bad_to = to - set (self .noncoordinates )
1124
+ if bad_to :
1125
+ raise ValueError ('Dataset does not contain the '
1126
+ 'noncoordinates: %r' % list (bad_to ))
1127
+ else :
1128
+ to = set (self .noncoordinates )
1129
+
1130
+ variables = OrderedDict ()
1131
+ for name , var in iteritems (self .noncoordinates ):
1132
+ variables [name ] = func (var , ** kwargs ) if name in to else var
1133
+
1134
+ attrs = self .attrs if keep_attrs else {}
1135
+
1136
+ return Dataset (variables , attrs )
1137
+
1094
1138
@classmethod
1095
1139
def concat (cls , datasets , dimension = 'concat_dimension' , indexers = None ,
1096
1140
mode = 'different' , concat_over = None , compat = 'equals' ):
0 commit comments