@@ -64,6 +64,33 @@ You can also iterate over over groups in ``(label, group)`` pairs:
64
64
Just like in pandas, creating a GroupBy object is cheap: it does not actually
65
65
split the data until you access particular values.
66
66
67
+ Binning
68
+ ~~~~~~~
69
+
70
+ Sometimes you don't want to use all the unique values to determine the groups
71
+ but instead want to "bin" the data into coarser groups. You could always create
72
+ a customized coordinate, but xarray facilitates this via the
73
+ :py:meth: `~xarray.Dataset.groupby_bins ` method.
74
+
75
+ .. ipython :: python
76
+
77
+ x_bins = [0 ,25 ,50 ]
78
+ ds.groupby_bins(' x' , x_bins).groups
79
+
80
+ The binning is implemented via `pandas.cut `__, whose documentation details how
81
+ the bins are assigned. As seen in the example above, by default, the bins are
82
+ labeled with strings using set notation to precisely identify the bin limits. To
83
+ override this behavior, you can specify the bin labels explicitly. Here we
84
+ choose `float ` labels which identify the bin centers:
85
+
86
+ .. ipython :: python
87
+
88
+ x_bin_labels = [12.5 ,37.5 ]
89
+ ds.groupby_bins(' x' , x_bins, labels = x_bin_labels).groups
90
+
91
+ __ http://pandas.pydata.org/pandas-docs/version/0.17.1/generated/pandas.cut.html
92
+
93
+
67
94
Apply
68
95
~~~~~
69
96
@@ -170,3 +197,11 @@ __ http://cfconventions.org/cf-conventions/v1.6.0/cf-conventions.html#_two_dimen
170
197
da
171
198
da.groupby(' lon' ).sum()
172
199
da.groupby(' lon' ).apply(lambda x : x - x.mean(), shortcut = False )
200
+
201
+ Because multidimensional groups have the ability to generate a very large
202
+ number of bins, coarse-binning via :py:meth: `~xarray.Dataset.groupby_bins `
203
+ may be desirable:
204
+
205
+ .. ipython :: python
206
+
207
+ da.groupby_bins(' lon' , [0 ,45 ,50 ]).sum()
0 commit comments