Python pandas dataframe - any way to set frequency programmatically?

前端 未结 1 349
庸人自扰
庸人自扰 2021-01-11 12:57

I\'m trying to process CSV files like this:

df = pd.read_csv(\"raw_hl.csv\", index_col=\'time\', parse_dates = True))
df.head(2)
                    high             


        
相关标签:
1条回答
  • 2021-01-11 13:03

    If you have a regular frequency it will be reported when you look at df.index.freq

    In [20]: df = DataFrame({'A' : np.arange(5)},index=pd.date_range('20130101 09:00:00',freq='3T',periods=5))
    
    In [21]: df
    Out[21]: 
                         A
    2013-01-01 09:00:00  0
    2013-01-01 09:03:00  1
    2013-01-01 09:06:00  2
    2013-01-01 09:09:00  3
    2013-01-01 09:12:00  4
    
    In [22]: df.index.freq
    Out[22]: <3 * Minutes>
    

    Have an irregular frequency will return None

    In [23]: df.index = df.index[0:2].tolist() + [Timestamp('20130101 09:05:00')] + df.index[-2:].tolist()
    
    In [24]: df
    Out[24]: 
                         A
    2013-01-01 09:00:00  0
    2013-01-01 09:03:00  1
    2013-01-01 09:05:00  2
    2013-01-01 09:09:00  3
    2013-01-01 09:12:00  4
    
    In [25]: df.index.freq
    

    You can recover a regular frequency by doing this. Downsampling to a lower freq (where you don't have overlapping values), forward filling, then reindexing to the desired frequency and end-points).

    In [31]: df.resample('T').ffill().reindex(pd.date_range(df.index[0],df.index[-1],freq='3T'))
    Out[31]: 
                         A
    2013-01-01 09:00:00  0
    2013-01-01 09:03:00  1
    2013-01-01 09:06:00  2
    2013-01-01 09:09:00  3
    2013-01-01 09:12:00  4
    
    0 讨论(0)
提交回复
热议问题