【问题标题】:Python Data Frame resample on micro secondPython Dataframe 在微秒上重新采样
【发布时间】:2015-11-25 15:14:47
【问题描述】:

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

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

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

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

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

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

【问题讨论】:

  • 您能否发布一个可重现的示例?首先要检查的是您的 index 包含实际的日期时间,而不仅仅是字符串。
  • 可重现的例子是什么意思?我的索引 ["Date and Time"] 是类型 ,我通过 df = pd.read_csv(filename, parse_dates=[2]) 设置它
  • “可重现的示例”是指其他人可以复制和粘贴的内容,以便他们可以看到您遇到的相同问题。阅读 MCVE 上的 this

标签: python-2.7 pandas dataframe resampling


【解决方案1】:

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

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')

这给出了预期的输出。

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

【讨论】:

  • 我设法修复它(我需要设置非常短的时间跨度),但即使在毫秒上也有很多时间。有没有办法加快这个过程?
  • 要查看大部分时间的去向,可以通过运行%prun -l 10 example.resample('ms',how='sum') 进行一些分析。
  • how 属性自 pandas 0.18.0 版起已弃用,请像这样使用 example.resample('ms').sum()。参考:link
【解决方案2】:
sf = "1U" #for one microsecond

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

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-20
  • 2018-03-11
  • 1970-01-01
  • 2018-01-20
  • 2013-12-06
  • 2020-10-18
  • 2013-03-15
相关资源
最近更新 更多