小编blo*_*nri的帖子

对具有不规则时间间隔的大型数据集进行快速 EMA 计算

我有超过 800,000 行的数据。我想取其中一列的指数移动平均线 (EMA)。时间不是均匀采样的,我想在每次更新(行)时衰减 EMA。我的代码是这样的:

window = 5            
for i in range(1, len(series)):
    dt = series['datetime'][i] - series['datetime'][i - 1]
    decay = 1 - numpy.exp(-dt / window)
    result[i] = (1 - decay) * result[i - 1] + decay * series['midpoint'].iloc[i]
return pandas.Series(result, index=series.index)
Run Code Online (Sandbox Code Playgroud)

问题是,对于 800,000 行,这非常慢。无论如何使用numpy的其他一些功能来优化它?我无法对其进行矢量化,因为results[i]它依赖于results[i-1].

示例数据在这里:

Timestamp             Midpoint
1559655000001096130    2769.125
1559655000001162260    2769.127
1559655000001171688    2769.154
1559655000001408734    2769.138
1559655000001424200    2769.123
1559655000001433128    2769.110
1559655000001541560    2769.125
1559655000001640406    2769.125
1559655000001658436    2769.127
1559655000001755924    2769.129
1559655000001793266    2769.125
1559655000001878688    2769.143
1559655000002061024    2769.125
Run Code Online (Sandbox Code Playgroud)

python numpy pandas

4
推荐指数
1
解决办法
1148
查看次数

Converting string with comma delimited data and newline character to pandas dataframe

I'm pulling 1 minute historical bars for a stock and the data comes in like this:

'2018-06-11 09:31:00,968.250,965.000,968.000,965.250,17220,1160\n2018-06-11
09:32:00,965.250,964.250,965.250,964.750,17872,611\n2018-06-11
09:33:00,965.000,963.250,965.000,963.500,18851,547\n'
Run Code Online (Sandbox Code Playgroud)

It's one string where each row is separated by the new line character and each field is separated by a comma. It looks fine when I use the print() function but I want to convert this into a pandas dataframe. I appreciate any help.

python python-3.x pandas

3
推荐指数
1
解决办法
2108
查看次数

删除pandas中DateTime索引的时间部分

当我通过他们的API查询服务的日常数据时,他们会投入一个时间部分,该时间部分等于查询的时间.所以当我在14:54:36调用函数时,我的pandas数据框看起来像这样 -

2018-05-16 14:54:36  1024.75  1008.25      ...        39221        242897
2018-05-17 14:54:36  1017.00  1002.00      ...        35361        241132
2018-05-18 14:54:36  1015.75  1002.75      ...        49090        242938
2018-05-21 14:54:36  1034.50  1020.75      ...        56950        243316
2018-05-22 14:54:36  1043.75  1028.50      ...        49724        247874
2018-05-23 14:54:36  1049.00  1036.25      ...        46256        253609
2018-05-24 14:54:36  1059.75  1047.00      ...        65352        259617
Run Code Online (Sandbox Code Playgroud)

由于这是每日数据,因此时间部分无用.当我做:

data = pd.read_csv(StringIO(data), index_col=0, header=None,names=['High','Low','Open','Close','Volume','OpenInterest'])
data.index = pd.to_datetime(data.index,format="%Y-%m-%d")
Run Code Online (Sandbox Code Playgroud)

格式似乎不起作用.DateTime索引仍包含时间.知道如何删除时间部分吗?

python pandas

3
推荐指数
2
解决办法
6901
查看次数

Groupby Pandas 中的条件计数

我有一个数据框,其中包含股票代码和各种日期的模型准确性,如下所示:

ZAYO  20170228  0.203997
ZAYO  20170531  0.180037
ZAYO  20170831  0.110623
ZBH   20170131  0.130060
ZBH   20170430  0.115384
ZBH   20170731  0.065990
AAAP  20170331  1.075765
AAAP  20170630  1.119365
AAAP  20170930  0.223602
AAL   20170131  0.338255
AAL   20170430  0.386442
AAL   20170731  0.171351
Run Code Online (Sandbox Code Playgroud)

我想创建一个汇总数据框,其中每行包含股票代码名称、平均准确度、准确度高于 20 的次数百分比、准确度高于 50% 的次数百分比。

我用

dfGrouped = df.groupby(['tickers'])
dfGrouped.Accuracy.mean()
Run Code Online (Sandbox Code Playgroud)

获得平均准确度,但不确定一种干净的方法来计算特定股票的准确度超过 20%、50% 的次数。

python dataframe pandas pandas-groupby

2
推荐指数
1
解决办法
4059
查看次数

标签 统计

pandas ×4

python ×4

dataframe ×1

numpy ×1

pandas-groupby ×1

python-3.x ×1