pandas 大熊猫系列上的 numpy 差异
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13689512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
numpy diff on a pandas Series
提问by Dan Allan
I want to use numpy.diff on a pandas Series. Am I right that this is a bug? Or am I doing it wrong?
我想在Pandas系列上使用 numpy.diff。我说这是一个错误是正确的吗?还是我做错了?
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
In [165]: np.diff(np.arange(10))
Out[165]: array([1, 1, 1, 1, 1, 1, 1, 1, 1])
I am using pandas 0.9.1rc1, numpy 1.6.1.
我正在使用Pandas 0.9.1rc1,numpy 1.6.1。
回答by Aman
Pandas implements difflike so:
Pandasdiff是这样实现的:
In [3]: s = pd.Series(np.arange(10))
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
Using np.diffdirectly:
np.diff直接使用:
In [7]: np.diff(s.values)
Out[7]: array([1, 1, 1, 1, 1, 1, 1, 1, 1])
In [8]: np.diff(np.array(s))
Out[8]: array([1, 1, 1, 1, 1, 1, 1, 1, 1])
So why doesn't np.diff(s)work? Because np is taking np.asanyarray()of the series before finding the diff. Like so:
那么为什么不起作用np.diff(s)呢?因为 npnp.asanyarray()在找到diff. 像这样:
In [25]: a = np.asanyarray(s)
In [26]: a
Out[26]:
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
In [27]: np.diff(a)
Out[27]:
0 NaN
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 NaN

