pandas Python数据帧在微秒内重新采样

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32313741/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 23:50:19  来源:igfitidea点击:

Python Data Frame resample on micro second

python-2.7pandasdataframeresampling

提问by alphiii

I am working with resampling data frame and it works on hours, days mins, but doesn't resample less then sec. Program just hangs even on short time span. So am I missing something?

我正在使用重采样数据框,它可以在几小时、几天、几分钟内工作,但不会在不到 sec 的时间内重新采样。即使在很短的时间内,程序也会挂起。所以我错过了什么吗?

I tried 0.000001S, U etc... Nothing worked so far.

我尝试了 0.000001S、U 等......到目前为止没有任何效果。

my time format: 2015-08-29 19:30:47.015506

我的时间格式:2015-08-29 19:30:47.015506

you can see varable sf represent resampling freq.

你可以看到变量 sf 代表重采样频率。

grph = df.set_index('Date and Time').resample(sf, len).astype(int)

How can I resample data frame on micro seconds granularity?

如何以微秒粒度重新采样数据帧?

回答by YOBA

sf = "1U" #for one microsecond

For milliseconds/microseconds/seconds use:

对于毫秒/微秒/秒,请使用:

L       milliseonds
U       microseconds
S       seconds

Full doc:

完整文档

B       business day frequency
C       custom business day frequency (experimental)
D       calendar day frequency
W       weekly frequency
M       month end frequency
BM      business month end frequency
CBM     custom business month end frequency
MS      month start frequency
BMS     business month start frequency
CBMS    custom business month start frequency
Q       quarter end frequency
BQ      business quarter endfrequency
QS      quarter start frequency
BQS     business quarter start frequency
A       year end frequency
BA      business year end frequency
AS      year start frequency
BAS     business year start frequency
BH      business hour frequency
H       hourly frequency
T       minutely frequency
S       secondly frequency
L       milliseonds
U       microseconds
N       nanoseconds

回答by Pilik

If I understand your problem correctly you can't resample microseconds to another frequency that is less than a second, right? I made a toy example and there doesn't seem to be a problem though:

如果我正确理解您的问题,您不能将微秒重新采样到另一个小于一秒的频率,对吗?我做了一个玩具示例,但似乎没有问题:

import pandas as pd
import numpy as np

np.random.seed(0)
index=pd.date_range('22/10/2010', periods=100000, freq='U')
example=pd.Series(index=index,data=np.random.randn(100000))
example.resample('ms',how='sum')

This gives the expected output.

这给出了预期的输出。

(I think your problem is that you are trying to resample data that are in a microsecond format to microseconds itself, which doesn't make any sense. You want to either upsample or downsample (as in my example).)

(我认为您的问题是您试图将微秒格式的数据重新采样为微秒本身,这没有任何意义。您想要上采样或下采样(如我的示例)。