ben*_*oss 2 python numpy pandas
如果我使用以下方法构建一个pandas.DataFrame,我得到一个输出(我认为)是特殊的:
import pandas, numpy
df = pandas.DataFrame(
numpy.random.rand(100,2), index = numpy.arange(100), columns = ['s1','s2'])
smoothed = pandas.DataFrame(
pandas.ewma(df, span = 21), index = df.index, columns = ['smooth1','smooth2'])
Run Code Online (Sandbox Code Playgroud)
当我去看平滑的值时,我得到:
>>> smoothed.tail()
smooth1 smooth2
95 NaN NaN
96 NaN NaN
97 NaN NaN
98 NaN NaN
99 NaN NaN
Run Code Online (Sandbox Code Playgroud)
这似乎是以下碎片调用的聚合,产生不同的结果:
smoothed2 = pandas.DataFrame(pandas.ewma(df, span = 21))
smoothed2.index = df.index
smoothed2.columns = ['smooth1','smooth2']
Run Code Online (Sandbox Code Playgroud)
再次使用DataFrame.tail()我得到的调用:
>>> smoothed2.tail()
smooth1 smooth2
95 0.496021 0.501153
96 0.506118 0.507541
97 0.516655 0.544621
98 0.520212 0.543751
99 0.518170 0.572429
Run Code Online (Sandbox Code Playgroud)
任何人都可以提供关于为什么这些DataFrame构造方法应该有所不同的理由吗?
结果ewma(df, span=21)已经是DataFrame,因此当您将其与列表列表一起传递给DataFrame构造函数时,它会"选择"您传递的列.在这种特殊情况下,很难打破标签和数据之间的联系.如果你这样做了:
In [23]: smoothed = DataFrame(ewma(df, span = 21).values, index=df.index, columns = ['smooth1','smooth2'])
In [24]: smoothed.head()
Out[24]:
smooth1 smooth2
0 0.218350 0.877693
1 0.400214 0.813499
2 0.308564 0.739426
3 0.433341 0.641891
4 0.525260 0.620541
Run Code Online (Sandbox Code Playgroud)
那没问题.当然
smoothed = ewma(df, span=21)
smoothed.columns = ['smooth1', 'smooth2']
Run Code Online (Sandbox Code Playgroud)
也很好
| 归档时间: |
|
| 查看次数: |
1235 次 |
| 最近记录: |