Resampling with custom periods

后端 未结 2 532
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-05 18:02

Is there a \'cookbook\' way of resampling a DataFrame with (semi)irregular periods?

I have a dataset at a daily interval and want it to resample to what sometimes (in s

相关标签:
2条回答
  • 2021-02-05 18:33

    Using HYRY's data and solution up to the computation of the d variable, we can also do the following in pandas 0.11-dev or later (regardless of numpy version):

    In [18]: from datetime import timedelta
    
    In [23]: pd.Series([ timedelta(int(i)) for i in d ])
    Out[23]: 
    0             00:00:00
    1     1 days, 00:00:00
    2     2 days, 00:00:00
    3     3 days, 00:00:00
    4     4 days, 00:00:00
    5     5 days, 00:00:00
    6     6 days, 00:00:00
    7     7 days, 00:00:00
    8     8 days, 00:00:00
    9     9 days, 00:00:00
    10            00:00:00
    
    47    6 days, 00:00:00
    48    7 days, 00:00:00
    49    8 days, 00:00:00
    50    9 days, 00:00:00
    Length: 51, dtype: timedelta64[ns]
    

    The date is constructed similary to above

    date = pd.Series(df.index) - pd.Series([ timedelta(int(i)) for i in d ])
    df.groupby(date.values).mean()
    
    0 讨论(0)
  • 2021-02-05 18:38

    If you use numpy 1.7, you can use datetime64 & timedelta64 arrays to do the calculation:

    create the sample data:

    import pandas as pd
    import numpy as np
    
    begin = pd.datetime(2013,1,1)
    end = pd.datetime(2013,2,20)
    
    dtrange = pd.date_range(begin, end)
    
    p1 = np.random.rand(len(dtrange)) + 5
    p2 = np.random.rand(len(dtrange)) + 10
    
    df = pd.DataFrame({'p1': p1, 'p2': p2}, index=dtrange)
    

    calculate the dekad's date:

    d = df.index.day - np.clip((df.index.day-1) // 10, 0, 2)*10 - 1
    date = df.index.values - np.array(d, dtype="timedelta64[D]")
    df.groupby(date).mean()
    

    The output is:

                     p1         p2
    2013-01-01  5.413795  10.445640
    2013-01-11  5.516063  10.491339
    2013-01-21  5.539676  10.528745
    2013-02-01  5.783467  10.478001
    2013-02-11  5.358787  10.579149
    
    0 讨论(0)
提交回复
热议问题