MCPcopy
hub / github.com/pydata/xarray / first_items

Method first_items

xarray/core/resample_cftime.py:144–170  ·  view source on GitHub ↗

Meant to reproduce the results of the following grouper = pandas.Grouper(...) first_items = pd.Series(np.arange(len(index)), index).groupby(grouper).first() with index being a CFTimeIndex instead of a DatetimeIndex.

(self, index: CFTimeIndex)

Source from the content-addressed store, hash-verified

142 self.offset = None
143
144 def first_items(self, index: CFTimeIndex):
145 """Meant to reproduce the results of the following
146
147 grouper = pandas.Grouper(...)
148 first_items = pd.Series(np.arange(len(index)),
149 index).groupby(grouper).first()
150
151 with index being a CFTimeIndex instead of a DatetimeIndex.
152 """
153
154 datetime_bins, labels = _get_time_bins(
155 index, self.freq, self.closed, self.label, self.origin, self.offset
156 )
157 # check binner fits data
158 if index[0] < datetime_bins[0]:
159 raise ValueError("Value falls before first bin")
160 if index[-1] > datetime_bins[-1]:
161 raise ValueError("Value falls after last bin")
162
163 integer_bins = np.searchsorted(index, datetime_bins, side=self.closed)
164 counts = np.diff(integer_bins)
165 codes = np.repeat(np.arange(len(labels)), counts)
166 first_items = pd.Series(integer_bins[:-1], labels, copy=False)
167
168 # Mask duplicate values with NaNs, preserving the last values
169 non_duplicate = ~first_items.duplicated("last")
170 return first_items.where(non_duplicate), codes
171
172
173def _get_time_bins(

Callers

nothing calls this directly

Calls 5

_get_time_binsFunction · 0.85
repeatMethod · 0.80
arangeMethod · 0.80
diffMethod · 0.45
whereMethod · 0.45

Tested by

no test coverage detected