时间数据由一个频率转换到另一个频率
方法:调用resample
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import datetime as dt
rng = pd.date_range('1/1/2023', periods=90, freq='D')
# 数据按天
ts = pd.Series(np.random.randn(len(rng)), index=rng)
print(ts.head())
print(ts.resample('M').sum())
print(ts.resample('3D').mean())
# 数据降采样,降为月,指标是求和,也可以平均,自己指定
? ? ? ? 需要插值
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import datetime as dt
rng = pd.date_range('1/1/2023', periods=90, freq='D')
# 数据按天
ts = pd.Series(np.random.randn(len(rng)), index=rng)
day3Ts = ts.resample('3D').mean()
print(day3Ts)
print(day3Ts.resample('D').asfreq())
# 升采样,要进行插值
# 数据降采样,降为月,指标是求和,也可以平均,自己指定
这样结果插入的控空值NaN
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import datetime as dt
rng = pd.date_range('1/1/2023', periods=90, freq='D')
# 数据按天
ts = pd.Series(np.random.randn(len(rng)), index=rng)
day3Ts = ts.resample('3D').mean()
print(day3Ts.resample('D').ffill(1))
# 升采样,要进行插值
# 数据降采样,降为月,指标是求和,也可以平均,自己指定
day3Ts.resample('D').bfill(1)
print(day3Ts.resample('D').interpolate('linear'))