2015-07-10 166 views
0

我想檢索數據幀的採樣頻率,例如以微秒爲單位的整數,或以秒爲單位的浮點數。轉換大熊貓DateOffset爲微秒

我發現下面的工作

import pandas as pd 
(pd.datetime(1,1,1) + data_frame.index.freq - pd.datetime(1,1,1)).total_seconds() 

但不知何故,我覺得可能是這樣做的不太麻煩的方式...

回答

1

您可能需要使用pd.Timedelta

import pandas as pd 
import numpy as np 

# your dataframe with some unknown freq 
# ==================================== 
df = pd.DataFrame(np.random.randn(100), columns=['col'], index=pd.date_range('2015-01-01 00:00:00', periods=100, freq='20ms')) 

Out[263]: 
          col 
2015-01-01 00:00:00.000 0.8647 
2015-01-01 00:00:00.020 -0.2269 
2015-01-01 00:00:00.040 0.8112 
2015-01-01 00:00:00.060 0.2878 
2015-01-01 00:00:00.080 -0.5385 
2015-01-01 00:00:00.100 1.9085 
2015-01-01 00:00:00.120 -0.4758 
2015-01-01 00:00:00.140 1.4407 
2015-01-01 00:00:00.160 -1.1491 
2015-01-01 00:00:00.180 0.8057 
...       ... 
2015-01-01 00:00:01.800 -0.6615 
2015-01-01 00:00:01.820 0.7059 
2015-01-01 00:00:01.840 -0.3586 
2015-01-01 00:00:01.860 0.7320 
2015-01-01 00:00:01.880 -0.0364 
2015-01-01 00:00:01.900 0.5889 
2015-01-01 00:00:01.920 -0.7796 
2015-01-01 00:00:01.940 0.4763 
2015-01-01 00:00:01.960 0.8339 
2015-01-01 00:00:01.980 1.3138 

[100 rows x 1 columns] 


# processing using pd.Timedelta() 
# ================================= 
# get the freq in ms 
(df.index[1] - df.index[0])/pd.Timedelta('1ms') 

Out[262]: 20.0