Skip to content

Commit 5d23a99

Browse files
author
darothen
committed
Applying shoyer's clean-up of figuring out valid coords on Dataset up-sampling
1 parent ed8d5c9 commit 5d23a99

File tree

1 file changed

+26
-32
lines changed

1 file changed

+26
-32
lines changed

xarray/core/resample.py

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,7 @@ def _interpolate(self, kind='linear'):
191191
from .dataarray import DataArray
192192

193193
if isinstance(self._obj.data, dask_array_type):
194-
raise NotImplementedError(
195-
'Resampling by interpolation does not work with dask arrays')
194+
raise TypeError('dask arrays not supported yet in resample.interpolate()')
196195

197196
x = self._obj[self._dim].astype('float')
198197
y = self._obj.data
@@ -302,42 +301,37 @@ def reduce(self, func, dim=None, keep_attrs=False, **kwargs):
302301
func, self._dim, keep_attrs, **kwargs)
303302

304303
def _interpolate(self, kind='linear'):
305-
from .dataarray import DataArray
306304
from .dataset import Dataset
305+
from .variable import Variable
307306

308-
new_x = self._full_index.values.astype('float')
309-
310-
arrs = {}
307+
old_times = self._obj[self._dim].astype(float)
308+
new_times = self._full_index.values.astype(float)
311309

312-
# Prepare coordinates by dropping non-dimension coordinates along the
313-
# resampling dimension.
314-
# note: the lower-level Variable API could be used to speed this up
310+
data_vars = OrderedDict()
315311
coords = OrderedDict()
316-
for k, v in self._obj.coords.items():
317-
# is the resampling dimension
318-
if k == self._dim:
319-
coords[self._dim] = self._full_index
320-
# else, check if resampling dim is in coordinate dimensions
321-
elif self._dim not in v.dims:
322-
coords[k] = v
323312

324313
# Apply the interpolation to each DataArray in our original Dataset
325-
for var in self._obj.data_vars:
326-
da = self._obj[var].copy()
327-
328-
x = da[self._dim].astype('float')
329-
y = da.values
330-
axis = da.get_axis_num(self._dim)
331-
332-
f = interp1d(x, y, kind=kind, axis=axis, bounds_error=True,
333-
assume_sorted=True)
334-
335-
# Construct new DataArray
336-
arrs[var] = DataArray(f(new_x), dims=da.dims, name=da.name,
337-
attrs=da.attrs)
338-
339-
# Merge into a new Dataset to return
340-
return Dataset(arrs, coords=coords)
314+
for name, variable in self._obj.variables.items():
315+
if name in self._obj.coords:
316+
if name == self._dim:
317+
coords[self._dim] = self._full_index
318+
elif self._dim not in variable.dims:
319+
coords[name] = variable
320+
else:
321+
if isinstance(variable.data, dask_array_type):
322+
raise TypeError('dask arrays not supported yet in '
323+
'resample.interpolate()')
324+
325+
axis = variable.get_axis_num(self._dim)
326+
327+
f = interp1d(old_times, variable.data, kind=kind,
328+
axis=axis, bounds_error=True,
329+
assume_sorted=True)
330+
interpolated = Variable(variable.dims, f(new_times))
331+
332+
data_vars[name ] = interpolated
333+
334+
return Dataset(data_vars, coords)
341335

342336

343337
ops.inject_reduce_methods(DatasetResample)

0 commit comments

Comments
 (0)