Skip to content

np.diff fails when called on a Series #816

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
wesm opened this issue Feb 23, 2012 · 6 comments
Closed

np.diff fails when called on a Series #816

wesm opened this issue Feb 23, 2012 · 6 comments
Labels
Bug Internals Related to non-user accessible pandas implementation
Milestone

Comments

@wesm
Copy link
Member

wesm commented Feb 23, 2012

In [13]: s = Series(np.arange(10))

In [14]: np.diff(s)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/home/wesm/code/pandas/<ipython-input-14-b5f9fe77ab7c> in <module>()
----> 1 np.diff(s)

/usr/lib/epd-7.1/lib/python2.7/site-packages/numpy/lib/function_base.pyc in diff(a, n, axis)
    975         return diff(a[slice1]-a[slice2], n-1, axis=axis)
    976     else:
--> 977         return a[slice1]-a[slice2]
    978 
    979 def interp(x, xp, fp, left=None, right=None):

/home/wesm/code/pandas/pandas/core/series.pyc in __getitem__(self, key)
    392             key = np.asarray(key, dtype=bool)
    393 
--> 394         return self._get_with(key)
    395 
    396     def _get_with(self, key):

/home/wesm/code/pandas/pandas/core/series.pyc in _get_with(self, key)
    406         else:
    407             if isinstance(key, tuple):
--> 408                 return self._get_values_tuple(key)
    409 
    410             if not isinstance(key, (list, np.ndarray)):  # pragma: no cover

/home/wesm/code/pandas/pandas/core/series.pyc in _get_values_tuple(self, key)
    437 
    438         if not isinstance(self.index, MultiIndex):
--> 439             raise ValueError('Can only tuple-index with a MultiIndex')
    440 
    441         # If key is contained, would have returned by now

ValueError: Can only tuple-index with a MultiIndex
@ghost ghost assigned adamklein Feb 23, 2012
@adamklein adamklein reopened this Feb 24, 2012
@changhiskhan
Copy link
Contributor

On Dec 3, 2012, at 1:31 PM, Dan Allan [email protected] wrote:

There is still trouble here.

In [163]: s = Series(np.arange(10))

In [164]: np.diff(s)
Out[164]:
0 NaN
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 NaN

Reply to this email directly or view it on GitHub.

Hi Dan, you should be using s.diff() instead.
np.diff treats the input argument as index unaware so it does not do the right thing for a Series.
If you want to duplicate np.diff results, you can do np.diff(s.values)

@wesm
Copy link
Member Author

wesm commented Dec 3, 2012

I'm reopening and moving this to at minimum 0.11. The solution here is to make Series not an ndarray, once and for all. The issue is the numpy.diff uses asanyarray instead of asarray, and Series has different array behavior than diff

@wesm wesm reopened this Dec 3, 2012
@jseabold
Copy link
Contributor

jseabold commented Dec 3, 2012

I just ask that downstream packages have plenty of time to look at this before it becomes released / merged. I don't know the implications yet, but I'm almost positive that I abuse the Series is an array sub-class more than I think.

@michaelaye
Copy link
Contributor

Do I understand it right that you want to make Series not a subclass of ndarray? Does that mean that you have to re-implement all the statistical helper functions inside? Or would this imply a lot of API changes for Series?

@jreback
Copy link
Contributor

jreback commented Jul 24, 2013

current behavior (0.12):

In [6]: np.diff(Series(np.arange(10)))
Out[6]: 
0   NaN
1     0
2     0
3     0
4     0
5     0
6     0
7     0
8     0
9   NaN
dtype: float64

after #3482

In [1]: s = Series(np.arange(10))

In [2]: np.diff(s)
Out[2]: array([1, 1, 1, 1, 1, 1, 1, 1, 1])

In [3]: np.diff(s.values)
Out[3]: array([1, 1, 1, 1, 1, 1, 1, 1, 1])
In [4]: s.diff()

Out[4]: 
0   NaN
1     1
2     1
3     1
4     1
5     1
6     1
7     1
8     1
9     1
dtype: float64

@jreback
Copy link
Contributor

jreback commented Aug 16, 2013

closed by #3482

@jreback jreback closed this as completed Aug 16, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Internals Related to non-user accessible pandas implementation
Projects
None yet
Development

No branches or pull requests

6 participants