python - Convert pandas DateOffset to microsecond -


i retrieve sampling frequency of dataframe integer in microseconds, or float in seconds.

i found following work

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

but somehow think there might less cumbersome way of doing it…

you might want use pd.timedelta.

import pandas pd import numpy np  # dataframe 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() # ================================= # freq in ms (df.index[1] - df.index[0])/pd.timedelta('1ms')  out[262]: 20.0 

Comments