Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
Ctrl+K

pandas.DataFrame.asfreq#

DataFrame.asfreq(freq,method=None,how=None,normalize=False,fill_value=None)[source]#

Convert time series to specified frequency.

Returns the original data conformed to a new index with the specifiedfrequency.

If the index of this Series/DataFrame is aPeriodIndex, the new indexis the result of transforming the original index withPeriodIndex.asfreq (so the original indexwill map one-to-one to the new index).

Otherwise, the new index will be equivalent topd.date_range(start,end,freq=freq) wherestart andend are, respectively, the first andlast entries in the original index (seepandas.date_range()). Thevalues corresponding to any timesteps in the new index which were not presentin the original index will be null (NaN), unless a method for fillingsuch unknowns is provided (see themethod parameter below).

Theresample() method is more appropriate if an operation on each group oftimesteps (such as an aggregate) is necessary to represent the data at the newfrequency.

Parameters:
freqDateOffset or str

Frequency DateOffset or string.

method{‘backfill’/’bfill’, ‘pad’/’ffill’}, default None

Method to use for filling holes in reindexed Series (note thisdoes not fill NaNs that already were present):

  • ‘pad’ / ‘ffill’: propagate last valid observation forward to nextvalid

  • ‘backfill’ / ‘bfill’: use NEXT valid observation to fill.

how{‘start’, ‘end’}, default end

For PeriodIndex only (see PeriodIndex.asfreq).

normalizebool, default False

Whether to reset output index to midnight.

fill_valuescalar, optional

Value to use for missing values, applied during upsampling (notethis does not fill NaNs that already were present).

Returns:
Series/DataFrame

Series/DataFrame object reindexed to the specified frequency.

See also

reindex

Conform DataFrame to new index with optional filling logic.

Notes

To learn more about the frequency strings, please seethis link.

Examples

Start by creating a series with 4 one minute timestamps.

>>>index=pd.date_range('1/1/2000',periods=4,freq='min')>>>series=pd.Series([0.0,None,2.0,3.0],index=index)>>>df=pd.DataFrame({'s':series})>>>df                       s2000-01-01 00:00:00    0.02000-01-01 00:01:00    NaN2000-01-01 00:02:00    2.02000-01-01 00:03:00    3.0

Upsample the series into 30 second bins.

>>>df.asfreq(freq='30s')                       s2000-01-01 00:00:00    0.02000-01-01 00:00:30    NaN2000-01-01 00:01:00    NaN2000-01-01 00:01:30    NaN2000-01-01 00:02:00    2.02000-01-01 00:02:30    NaN2000-01-01 00:03:00    3.0

Upsample again, providing afillvalue.

>>>df.asfreq(freq='30s',fill_value=9.0)                       s2000-01-01 00:00:00    0.02000-01-01 00:00:30    9.02000-01-01 00:01:00    NaN2000-01-01 00:01:30    9.02000-01-01 00:02:00    2.02000-01-01 00:02:30    9.02000-01-01 00:03:00    3.0

Upsample again, providing amethod.

>>>df.asfreq(freq='30s',method='bfill')                       s2000-01-01 00:00:00    0.02000-01-01 00:00:30    NaN2000-01-01 00:01:00    NaN2000-01-01 00:01:30    2.02000-01-01 00:02:00    2.02000-01-01 00:02:30    3.02000-01-01 00:03:00    3.0

[8]ページ先頭

©2009-2025 Movatter.jp