【问题标题】:Unable to integrate over discrete set of points using Python无法使用 Python 对离散的点集进行积分
【发布时间】:2015-02-20 20:58:32
【问题描述】:

我有一个具有以下值的 Pandas 系列。我们如何进行积分以找到该图下方的区域?

Hour
0.00000    1.195617
0.23990    2.408227
0.47980    1.256069
0.71970    2.227347
0.95960    1.397774
1.19949    1.896309
1.43939    1.309016
1.67929    1.827614
1.91919    1.383252
2.15909    1.630766
2.39899    1.360364
2.63889    1.541367
2.87879    1.560319
3.11869    0.743437
3.35859    1.549370
...
20.39141    2.067811
20.63131    1.938257
20.87121    1.944990
21.11111    1.853212
21.35101    1.702590
21.59091    1.746243
21.83081    2.337570
22.07071    3.773000
22.31061    1.532937
22.55051    1.178040
22.79040    1.850222
23.03030    1.092376
23.27020    1.895959
23.51010    0.966083
23.75000    1.950073
Name: Cost, Length: 100, dtype: float64

尝试了 cookbook 的集成功能,但它抛出了一个错误

TypeError: Setting <class 'pandas.core.index.Float64Index'> dtype to anything other than float64 or object is not supported

【问题讨论】:

  • 请显示指向TypeError 的完整堆栈跟踪,否则无法判断哪个代码错误地尝试设置dtype

标签: python python-2.7 pandas scipy


【解决方案1】:

食谱中的食谱要求您使用 pd.TimeSeries 而不是 pd.Series。您可以像这样将索引转换为 pd.Timestamp 以获得 pd.TimeSeries

i = [0.00000,
0.23990,
0.47980,
0.71970,
0.95960,
1.19949,
1.43939,
1.67929,
1.91919,
2.15909,
2.39899,
2.63889,
2.87879,
3.11869,
3.35859] 
c = [1.195617,
2.408227,
1.256069,
2.227347,
1.397774,
1.896309,
1.309016,
1.827614,
1.383252,
1.630766,
1.360364,
1.541367,
1.560319,
0.743437,
1.549370]

import pandas as pd
s = pd.TimeSeries (index = [pd.Timestamp(k) for k in i], data = c)
s.integrate()

这应该会产生,

Out[29]:
4.3059284999999994e-09

【讨论】:

    猜你喜欢
    • 2012-06-04
    • 2012-04-10
    • 2017-08-16
    • 2021-04-04
    • 1970-01-01
    • 2021-12-29
    • 2013-11-19
    • 2016-12-13
    • 1970-01-01
    相关资源
    最近更新 更多