python-3.x - 将年度时间序列转换为月度
问题描述
如果我的 df 索引有 '01-01-2000'、'01-01-2001'、'01-01-2002' 等,我如何将年度时间序列转换为月度。带有“M”的重采样方法有效,但每月系列停止在去年的“01-01”,但我希望去年的“12-01”在该范围内。感谢任何帮助。
解决方案
您可以尝试添加最后一个假观察(正如 Quang Hoang 建议的那样):
import pandas as pd
import numpy as np
df = pd.DataFrame({'date': pd.date_range('2001-01-01', periods = 5, freq = 'AS'), 'Value_1': np.random.randint(0, 100, 4).tolist() + [np.NaN]})
df = df.set_index('date')
print(df)
原始系列于 2004-01-01 结束。
df_out = df.resample('MS').ffill()
print(df_out)
新系列于 2004-12-01 结束。
推荐阅读
- r - 将 xtable 和 ggplot2 输出并排放置在 Beamer 中
- r - ggplot 填充 SpatialPolygonsDataFrame
- typo3 - 错字3更新后 - 文字不环绕图像,图像不显示在列中
- java - google guice 在 android 项目中不起作用
- angular - Angular2 +单选按钮不可点击
- reactjs - 分组特定子组件 React
- mysql - MySQL - SELECT 和 GROUP BY 的问题
- sql-server - 将数据从临时表导入永久表时出现动态 SQL 错误
- bazel - bazel 缺少 stddef.h 和其他 Ubuntu 18.04 的依赖项声明
- angular - Angular 7 CLI ng 生成组件错误:'无法读取未定义的属性'sourceRoot'