基於pandas的時間序列處理方法

2021-08-21 09:33:39 字數 3104 閱讀 7466

importpandasaspd

importnumpyasnp

#生成時間序列

rng = pd.date_range('2016/1/1', periods=20, freq='d')

time = pd.series(np.random.rand(20), index=rng)

#print(time)

#過濾資料

time.truncate(before='2016-1-10')

#print(time)

#資料重取樣

'''1.時間資料由乙個頻率轉換至另乙個頻率2.降取樣3.公升取樣'''rng = pd.date_range('1/1/2011', periods=90, freq='d')

ts = pd.series(np.random.randn(len(rng)), index=rng)

#print(ts.head())

#轉成成以月為單位

#ts.resample('m').sum()

day3ts = ts.resample('3d').sum()

#print(ts.resample('3d').mean())

#插值填充

#print(day3ts.resample('d').asfreq())

#三種插值方法

'''1.ffill 空值取前面的值2.bfill 控制取後面的值3.interporate 線性取值'''#print(day3ts.resample('d').ffill(1))#對多少個nan填充

'''2011-01-01 -1.3762952011-01-02 -1.3762952011-01-03 nan'''#print(day3ts.resample('d').bfill(1))

'''2011-01-01 0.9481382011-01-02 nan2011-01-03 0.5602292011-01-04 0.560229'''#print(day3ts.resample('d').interpolate('linear'))

'''2011-01-01 0.4536592011-01-02 0.9934092011-01-03 1.5331582011-01-04 2.072907'''# 滑動視窗

'''**時求一段時間的平均值'''df = pd.series(np.random.randn(600), index=pd.date_range('7/1/2016',freq='d', periods=600))

#print(df.head())

#r = df.rolling(window=10)#視窗大小為10

pandas時間序列

import pandas as pd import numpy as np import datetimedf pd.dataframe df 將時間序列轉化為標準的年月日的形式 df date pd.to datetime df date df 生成定頻日期與時間段序列 dt pd.date r...

Pandas時間序列資料處理

時間序列資料是以時間為自變數,描述物件在時間過程中的發展 變化。比如,超市每天的銷售額,景區每天的旅客流量等。因此,處理時間序列資料是pandas資料分析中重要的一類方法。一 時間索引轉換 一般來說,我們獲得的資料不是以時間作為索引的。即使原始資料的第一列是時間,當我們匯入python後仍然會重新建...

pandas 時間處理

year month day 是 datetime 標準形式 可以用 datetime.dt.day 取到 day 如果是 datetime 相減,得到兩個時間差的天數,型別變為 timedelta 要用 dt.days 獲取天數 如果是一串數字表達的時間 沒有分隔符 可以用to datetime ...