Skip to content
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

np.diff fails when called on a Series #816

Closed
wesm opened this issue Feb 23, 2012 · 6 comments

Comments

Projects
None yet
6 participants
@wesm
Copy link
Member

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 closed this in 094aee0 Feb 23, 2012

@adamklein adamklein reopened this Feb 24, 2012

@adamklein adamklein closed this in 4a20310 Feb 24, 2012

@changhiskhan

This comment has been minimized.

Copy link
Contributor

commented Dec 3, 2012

On Dec 3, 2012, at 1:31 PM, Dan Allan notifications@github.com 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

This comment has been minimized.

Copy link
Member Author

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

This comment has been minimized.

Copy link
Contributor

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

This comment has been minimized.

Copy link
Contributor

commented Jan 8, 2013

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

This comment has been minimized.

Copy link
Contributor

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

This comment has been minimized.

Copy link
Contributor

commented Aug 16, 2013

closed by #3482

@jreback jreback closed this Aug 16, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.