Skip to content

BUG: Allow DatetimeIndex for scipy interpolate #5977

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

Merged
merged 1 commit into from
Jan 16, 2014

Conversation

TomAugspurger
Copy link
Contributor

Closes #5975

Is there a better way to check for a DatetimeIndex than with an isinstance()?

@jreback
Copy link
Contributor

jreback commented Jan 16, 2014

you could try getattr(v,'asi8',v)

hayd added a commit that referenced this pull request Jan 16, 2014
BUG: Allow DatetimeIndex for scipy interpolate
@hayd hayd merged commit cc2c70b into pandas-dev:master Jan 16, 2014
@hayd
Copy link
Contributor

hayd commented Jan 16, 2014

should this be posted upstream (scipy) too ?

@TomAugspurger
Copy link
Contributor Author

I wasn't sure about that. I looked and didn't see any existing issues about interpolation with datetime arrays.

On Jan 16, 2014, at 3:09 PM, "Andy Hayden" <[email protected]mailto:[email protected]> wrote:

should this be posted upstream (scipy) too ?


Reply to this email directly or view it on GitHubhttps://github.com//pull/5977#issuecomment-32547121.

@jreback
Copy link
Contributor

jreback commented Jan 17, 2014

this is pretty standard to use only the base types (e.g. float/int64) for most routines (in scipy/numpy), unless they are specific datetime like routines. Pandas just handles these things in a nicer way.

@ghost
Copy link

ghost commented Jan 18, 2014

I believe this introduced an error when compiling the docs:

TypeError                                 Traceback (most recent call last)
<ipython-input-70-c36e7738e696> in <module>()
----> 1 interp_s = ser.reindex(new_index).interpolate(method='pchip')

/home/user1/src/pandas/pandas/core/generic.pyc in interpolate(self, method, axis, limit, inplace, downcast, **kwargs)
   2334                                           inplace=inplace,
   2335                                           downcast=downcast,
-> 2336                                           **kwargs)
   2337 
   2338         if inplace:

/home/user1/src/pandas/pandas/core/internals.pyc in interpolate(self, *args, **kwargs)
   2322 
   2323     def interpolate(self, *args, **kwargs):
-> 2324         return self.apply('interpolate', *args, **kwargs)
   2325 
   2326     def shift(self, *args, **kwargs):

/home/user1/src/pandas/pandas/core/internals.pyc in apply(self, f, *args, **kwargs)
   2293 
   2294             else:
-> 2295                 applied = getattr(blk, f)(*args, **kwargs)
   2296 
   2297             if isinstance(applied, list):

/home/user1/src/pandas/pandas/core/internals.pyc in interpolate(self, method, axis, index, values, inplace, limit, fill_value, coerce, downcast, **kwargs)
    788                                      inplace=inplace,
    789                                      downcast=downcast,
--> 790                                      **kwargs)
    791 
    792         raise ValueError("invalid method '{0}' to interpolate.".format(method))

/home/user1/src/pandas/pandas/core/internals.pyc in _interpolate(self, method, index, values, fill_value, axis, limit, inplace, downcast, **kwargs)
    849 
    850         # interp each column independently
--> 851         interp_values = np.apply_along_axis(func, axis, data)
    852 
    853         blocks = [make_block(interp_values, self.items, self.ref_items,

/usr/lib64/python2.7/site-packages/numpy/lib/shape_base.pyc in apply_along_axis(func1d, axis, arr, *args)
     78     outshape = asarray(arr.shape).take(indlist)
     79     i.put(indlist, ind)
---> 80     res = func1d(arr[tuple(i.tolist())],*args)
     81     #  if res is a number, then we have a smaller output array
     82     if isscalar(res):

/home/user1/src/pandas/pandas/core/internals.pyc in func(x)
    846             return com.interpolate_1d(index, x, method=method, limit=limit,
    847                                       fill_value=fill_value,
--> 848                                       bounds_error=False, **kwargs)
    849 
    850         # interp each column independently

/home/user1/src/pandas/pandas/core/common.pyc in interpolate_1d(xvalues, yvalues, method, limit, fill_value, bounds_error, **kwargs)
   1387         result[firstIndex:][invalid] = _interpolate_scipy_wrapper(
   1388             valid_x, valid_y, new_x, method=method, fill_value=fill_value,
-> 1389             bounds_error=bounds_error, **kwargs)
   1390         if limit:
   1391             result[violate_limit] = np.nan

/home/user1/src/pandas/pandas/core/common.pyc in _interpolate_scipy_wrapper(x, y, new_x, method, fill_value, bounds_error, order, **kwargs)
   1417     if hasattr(x, 'asi8'):
   1418         # GH 5975, scipy.interp1d can't hande datetime64s
-> 1419         x, new_x = x.values.view('i8'), new_x.view('i8')
   1420 
   1421     try:

TypeError: Cannot change data-type for object array.
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-71-b33d2ca06c23> in <module>()
----> 1 interp_s[49:51]


@jreback
Copy link
Contributor

jreback commented Jan 18, 2014

@TomAugspurger ahh...instead of using x.values.view('i8'), just astype it, e.g. x.values.astype('i8')
I think will fix this

ghost pushed a commit that referenced this pull request Jan 19, 2014
@TomAugspurger TomAugspurger deleted the interpolate-datetime branch November 3, 2016 12:37
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

Successfully merging this pull request may close these issues.

BUG: Scipy interpolate methods are not datetime64 aware
3 participants