简体   繁体   English

按时间顺序更改频率

[英]change frequency in time series

I have a dataframe of boolean variables, idexed by timestamps. 我有一个布尔变量的数据框,由时间戳确定。 The timestamps are irregular and I wish to fill in the gaps. 时间戳是不规则的,我希望填补空白。 I know that the frequency needed is 3ms. 我知道所需的频率是3ms。

So far, I can do the following : 到目前为止,我可以执行以下操作:

df = pd.read_csv(path, sep= ';')
df['timestamp'] = pd.to_datetime(df ['timestamp'], errors='raise',infer_datetime_format = True)
df = df.sort(['timestamp'])
df = df.set_index('timestamp')
df.reindex(pd.period_range(df.index[0], df.index[-1], freq='ms'))     
df = df.fillna(method = 'ffill')

So, I am reindexing using a ms interval and filling forward missing values (which is what fits my case : all variables are boolean, so at each moment, the current state is the last appearing in my data). 因此,我使用ms间隔重新索引并填充缺失值(这正是我的情况:所有变量均为布尔值,因此在每时每刻,当前状态都是数据中的最后一个出现)。

How can I resample every 3 milliseconds? 如何每3毫秒重新采样一次?

EDIT : It seems like DataFrame.resample can also be used for upsampling. 编辑:似乎DataFrame.resample也可以用于上采样。 Any suggestions on how to use it in my case ? 关于如何在我的情况下使用它的任何建议? I do not seem to get how it works. 我似乎不明白它是如何工作的。

Use DataFrame.asfreq : 使用DataFrame.asfreq

df = pd.DataFrame({
    'timestamp': pd.to_datetime(['2015-02-01 15:14:11.30',
                                 '2015-02-01 15:14:11.36',
                                 '2015-02-01 15:14:11.39']),
    'B': [7,10,3]
})
print (df)
                timestamp   B
0 2015-02-01 15:14:11.300   7
1 2015-02-01 15:14:11.360  10
2 2015-02-01 15:14:11.390   3

df = df.set_index('timestamp').asfreq('3ms', method='ffill')

print (df)
                          B
timestamp                  
2015-02-01 15:14:11.300   7
2015-02-01 15:14:11.303   7
2015-02-01 15:14:11.306   7
2015-02-01 15:14:11.309   7
2015-02-01 15:14:11.312   7
2015-02-01 15:14:11.315   7
2015-02-01 15:14:11.318   7
2015-02-01 15:14:11.321   7
2015-02-01 15:14:11.324   7
2015-02-01 15:14:11.327   7
2015-02-01 15:14:11.330   7
2015-02-01 15:14:11.333   7
2015-02-01 15:14:11.336   7
2015-02-01 15:14:11.339   7
2015-02-01 15:14:11.342   7
2015-02-01 15:14:11.345   7
2015-02-01 15:14:11.348   7
2015-02-01 15:14:11.351   7
2015-02-01 15:14:11.354   7
2015-02-01 15:14:11.357   7
2015-02-01 15:14:11.360  10
2015-02-01 15:14:11.363  10
2015-02-01 15:14:11.366  10
2015-02-01 15:14:11.369  10
2015-02-01 15:14:11.372  10
2015-02-01 15:14:11.375  10
2015-02-01 15:14:11.378  10
2015-02-01 15:14:11.381  10
2015-02-01 15:14:11.384  10
2015-02-01 15:14:11.387  10
2015-02-01 15:14:11.390   3

if you have your timestamp in index: 如果您的时间戳记在索引中:

df = df.resample('3ms').ffill()

EDIT: 编辑:

performance benchmark 绩效基准

import time
import pandas as pd


dd = {'dt': ['2018-01-01 00:00:00', '2018-01-01 01:12:59'], 'v':[1,1]}

df = pd.DataFrame(data=dd)
df['dt'] = pd.to_datetime(df['dt'])
df = df.set_index('dt')

start = time.time()
df = df.resample('3ms').ffill()
print(time.time() - start)


df = pd.DataFrame(data=dd)
df['dt'] = pd.to_datetime(df['dt'])
df = df.set_index('dt')

start = time.time()
df = df.asfreq('3ms', method='ffill')
print(time.time() - start)

print(df.shape)

result: 结果:

0.03699994087219238
0.029999732971191406
(1459667, 1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM