Skip to content

Resample interpolate failing on tutorial dataset #1605

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jhamman opened this issue Oct 4, 2017 · 3 comments
Closed

Resample interpolate failing on tutorial dataset #1605

jhamman opened this issue Oct 4, 2017 · 3 comments

Comments

@jhamman
Copy link
Member

jhamman commented Oct 4, 2017

I'm getting some unexpected behavior/errors from the new resample/interpolate methods.

@darothen - any idea what's going on here?

In [1]: import xarray as xr

In [2]: ds = xr.tutorial.load_dataset('air_temperature')

In [3]: ds.resample(time='15d').interpolate(kind='linear')
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-3-ef931d7ebbda> in <module>()
----> 1 ds.resample(time='15d').interpolate(kind='linear')

/glade/p/work/jhamman/storylines/src/xarray/xarray/core/resample.py in interpolate(self, kind)
    110
    111         """
--> 112         return self._interpolate(kind=kind)
    113
    114     def _interpolate(self, kind='linear'):

/glade/p/work/jhamman/storylines/src/xarray/xarray/core/resample.py in _interpolate(self, kind)
    312
    313         old_times = self._obj[self._dim].astype(float)
--> 314         new_times = self._full_index.values.astype(float)
    315
    316         data_vars = OrderedDict()

AttributeError: 'NoneType' object has no attribute 'values'
@darothen
Copy link

darothen commented Oct 4, 2017

(sorry, originally commented from my work account)

The tutorial dataset is ~6-hourly, so your operation is a downsampling operation. We don't actually support interpolation on downsampling operations - just aggregations/reductions. Upsampling supports interpolation since there is no implicit way to estimate data between the gaps at the lower temporal frequency. If you just want to estimate a given field at 15-day intervals, for 00Z on those days, then I think you should use ds.reindex(), but at the moment I do not think it will work with timeseries. That would be a critical feature to implement.

@jhamman
Copy link
Member Author

jhamman commented Oct 4, 2017

@darothen - Thanks and interesting. I'm getting the above error in a real world resample operation so I figured they were the same issue. I'll dig into this and add some more detail in a bit.

@jhamman
Copy link
Member Author

jhamman commented Oct 4, 2017

Okay, I got it now. Consider this example

dates = pd.date_range('2016-01-01', '2016-12-31', freq='D')
# orig = dates[dates != '2016-02-29'] # drop feb 29 and this example will work
orig = dates

da = xr.DataArray(np.random.random((len(orig), 2, 3)), dims=('time', 'x', 'y'), coords={'time': orig})
print(da)

da.resample(time='1D').interpolate('linear')
<xarray.DataArray (time: 366, x: 2, y: 3)>
array([[[ 0.390107,  0.257026,  0.155619],
        [ 0.151772,  0.98012 ,  0.61582 ]],

       [[ 0.081488,  0.038706,  0.627044],
        [ 0.840926,  0.778831,  0.102756]],

       ..., 
       [[ 0.94791 ,  0.274371,  0.582416],
        [ 0.544428,  0.351174,  0.603062]],

       [[ 0.166722,  0.507593,  0.841115],
        [ 0.099317,  0.649383,  0.842175]]])
Coordinates:
  * time     (time) datetime64[ns] 2016-01-01 2016-01-02 2016-01-03 ...
Dimensions without coordinates: x, y
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-12-e187a9baeec8> in <module>()
      6 print(da)
      7 
----> 8 da.resample(time='1d').interpolate('linear')

~/Dropbox/src/xarray/xarray/core/resample.py in interpolate(self, kind)
    110 
    111         """
--> 112         return self._interpolate(kind=kind)
    113 
    114     def _interpolate(self, kind='linear'):

~/Dropbox/src/xarray/xarray/core/resample.py in _interpolate(self, kind)
    204         f = interp1d(x, y, kind=kind, axis=axis, bounds_error=True,
    205                      assume_sorted=True)
--> 206         new_x = self._full_index.values.astype('float')
    207 
    208         # construct new up-sampled DataArray

AttributeError: 'NoneType' object has no attribute 'values'

The application here is that I'm doing a QC check on a dataset that is sometimes missing Feb 29. It is sufficient for my application to always resample and fill Feb 29 when its missing. The pandas equivalent works:

s = pd.Series(np.random.random((len(orig))), index=orig)
new = s.resample('1D').interpolate('linear')
new.equals(s)
True

I think I have a fix for this which I'll push up quickly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants