Difference between asfreq and resample

后端 未结 2 1100
萌比男神i
萌比男神i 2020-12-24 05:39

Can some please explain the difference between the asfreq and resample methods in pandas? When should one use what?

2条回答
  •  一个人的身影
    2020-12-24 06:07

    resample is more general than asfreq. For example, using resample I can pass an arbitrary function to perform binning over a Series or DataFrame object in bins of arbitrary size. asfreq is a concise way of changing the frequency of a DatetimeIndex object. It also provides padding functionality.

    As the pandas documentation says, asfreq is a thin wrapper around a call to date_range + a call to reindex. See here for an example.

    An example of resample that I use in my daily work is computing the number of spikes of a neuron in 1 second bins by resampling a large boolean array where True means "spike" and False means "no spike". I can do that as easy as large_bool.resample('S', how='sum'). Kind of neat!

    asfreq can be used when you want to change a DatetimeIndex to have a different frequency while retaining the same values at the current index.

    Here's an example where they are equivalent:

    In [6]: dr = date_range('1/1/2010', periods=3, freq=3 * datetools.bday)
    
    In [7]: raw = randn(3)
    
    In [8]: ts = Series(raw, index=dr)
    
    In [9]: ts
    Out[9]:
    2010-01-01   -1.948
    2010-01-06    0.112
    2010-01-11   -0.117
    Freq: 3B, dtype: float64
    
    In [10]: ts.asfreq(datetools.BDay())
    Out[10]:
    2010-01-01   -1.948
    2010-01-04      NaN
    2010-01-05      NaN
    2010-01-06    0.112
    2010-01-07      NaN
    2010-01-08      NaN
    2010-01-11   -0.117
    Freq: B, dtype: float64
    
    In [11]: ts.resample(datetools.BDay())
    Out[11]:
    2010-01-01   -1.948
    2010-01-04      NaN
    2010-01-05      NaN
    2010-01-06    0.112
    2010-01-07      NaN
    2010-01-08      NaN
    2010-01-11   -0.117
    Freq: B, dtype: float64
    

    As far as when to use either: it depends on the problem you have in mind...care to share?

提交回复
热议问题