Python是進行數據分析的一種出色語言,主要是因為以數據為中心的python軟件包具有奇妙的生態係統。 Pandas是其中的一種,使導入和分析數據更加容易。
Pandas dataframe.asfreq()
函數用於將TimeSeries轉換為指定頻率。此函數可以選擇提供填充方法來填充/回填缺失的值。它以指定的頻率返回符合新索引的原始數據。如果需要某種操作(例如匯總)來代表新頻率的數據,則重新采樣更為合適。
用法: DataFrame.asfreq(freq, method=None, how=None, normalize=False, fill_value=None)
參數:
freq:DateOffset對象或字符串
method:重新索引係列中填充孔的方法
how:僅對於PeriodIndex,請參見PeriodIndex.asfreq
normalize:是否將輸出索引重置為午夜
fill_value:用於缺失值的值,在上采樣期間應用(請注意,這不會填充已經存在的NaN)。
返回:已轉換:調用者類型
範例1:從每周頻率到每日頻率對時間序列數據進行非采樣
# importing pandas as pd
import pandas as pd
# Creating a date_time form index
index_values = (pd.date_range('1/1/2000',
periods=3,freq='W'))
# Creating a series using 'index_values'
# Notice, one of the series value is nan value
series = (pd.Series([0.0,None,2.0],
index=index_values))
# Creating dataframe using the series
df=pd.DataFrame({"Col_1":series})
# Print the Dataframe
df
現在將每周采樣的數據解采樣為每日采樣的數據。默認情況下,新創建的bin將具有nan值。因此,使用fill_value參數用提供的值填充所有新創建的容器。
# unsampling and providing a fill value = 9.0
df.asfreq(freq ='D', fill_value = 9.0)
輸出:
注意:這不會填充采樣前已經存在的NaN。
範例2:將帶有時間戳的一分鍾數據解采樣到30s容器中。首先創建一個帶有5個一分鍾時間戳的序列。
# importing pandas as pd
import pandas as pd
# Creating a date_time form index
index_values = (pd.date_range('1/1/2000',
periods=5,freq='T'))
# Creating a series using 'index_values'
# Notice, one of the series value is nan value
series = (pd.Series([0.0,1.0,None,3.0,4.0],
index=index_values))
# Creating dataframe using the series
df=pd.DataFrame({"Col_1":series})
# Print the Dataframe
df
現在取消采樣到30秒的容器中,並提供100.0的fill_value
# unsampling and providing a fill value of 100.0
df.asfreq(freq ='30S', fill_value = 100.0)
輸出:
注意:采樣前存在的Nan值將不被填充
相關用法
- Python pandas.map()用法及代碼示例
- Python Pandas Series.str.len()用法及代碼示例
- Python Pandas.factorize()用法及代碼示例
- Python Pandas TimedeltaIndex.name用法及代碼示例
- Python Pandas dataframe.ne()用法及代碼示例
- Python Pandas Series.between()用法及代碼示例
- Python Pandas DataFrame.where()用法及代碼示例
- Python Pandas Series.add()用法及代碼示例
- Python Pandas.pivot_table()用法及代碼示例
- Python Pandas Series.mod()用法及代碼示例
- Python Pandas Dataframe.at[ ]用法及代碼示例
- Python Pandas Dataframe.iat[ ]用法及代碼示例
- Python Pandas.pivot()用法及代碼示例
- Python Pandas dataframe.mul()用法及代碼示例
- Python Pandas.melt()用法及代碼示例
注:本文由純淨天空篩選整理自Shubham__Ranjan大神的英文原創作品 Python | Pandas dataframe.asfreq()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。