python - Python 将每月和分钟数据帧与 TZ 感知的日期时间索引相结合
问题描述
我在下面有两个时间序列。日期时间索引是 TZ 感知的。
df1 : 五分钟间隔
value_1
Timestamp
2009-04-01 10:50:00+09:30 50
2009-04-05 11:55:00+09:30 55
2009-04-23 16:00:00+09:30 0
2009-05-03 10:50:00+09:30 50
2009-05-07 11:55:00+09:30 55
2009-05-11 16:00:00+09:30 0
2009-07-04 02:05:00+09:30 5
2009-07-21 09:10:00+09:30 10
2009-07-30 12:15:00+09:30 15
2010-09-02 11:25:00+09:30 25
2010-09-22 15:30:00+09:30 30
2010-09-30 06:15:00+09:30 15
2010-12-06 11:25:00+09:30 25
2010-12-22 15:30:00+09:30 30
2010-12-28 06:15:00+09:30 15
df2:groupby('Month')
从不同数据集获得的每月间隔。
value_2
Timestamp
2009-04-30 00:00:00+09:30 23
2009-07-31 00:00:00+09:30 28
2010-12-31 00:00:00+09:30 23
我想按索引组合这两个数据集。如果 df1 中的任何记录与 df2 具有相同的月份,则应将其包含在最终结果中。预期结果如下。
value_1 value_2
Timestamp
2009-04-01 10:50:00+09:30 50 23
2009-04-05 11:55:00+09:30 55 23
2009-04-23 16:00:00+09:30 0 23
2009-07-04 02:05:00+09:30 5 28
2009-07-21 09:10:00+09:30 10 28
2009-07-30 12:15:00+09:30 15 28
2010-12-06 11:25:00+09:30 25 23
2010-12-22 15:30:00+09:30 30 23
2010-12-28 06:15:00+09:30 15 23
这是我的尝试。
result = pd.concat([df1, df2], axis=1)
# this combines the datasets, but not like expected, also by including join="outer". With join="inner", no data shown.
result = pd.merge(df1, df2, left_on='value_1', right_index=True)
# this return ValueError: You are trying to merge on Int64 and datetime64[ns, Australia/North] columns. If you wish to proceed you should use pd.concat
# Using @Ben.T
mt_hMF = df1.merge( df2.reset_index().set_index(df2.index.floor('M')),
how='left', left_index=True, right_index=True).set_index('Timestamp')
# This gives ValueError: <MonthEnd> is a non-fixed frequency
解决方案
试试这个,使用strftime
为两个数据框创建一个临时合并键:
df1.reset_index()\
.assign(yearmonth=df1.index.strftime('%Y%m'))\
.merge(df2.assign(yearmonth=df2.index.strftime('%Y%m')))\
.set_index('Timestamp')\
.drop('yearmonth', axis=1)
输出:
value_1 value_2
Timestamp
2009-04-01 10:50:00+09:30 50 23
2009-04-05 11:55:00+09:30 55 23
2009-04-23 16:00:00+09:30 0 23
2009-07-04 02:05:00+09:30 5 28
2009-07-21 09:10:00+09:30 10 28
2009-07-30 12:15:00+09:30 15 28
2010-12-06 11:25:00+09:30 25 23
2010-12-22 15:30:00+09:30 30 23
2010-12-28 06:15:00+09:30 15 23
推荐阅读
- html - 将 HTML 文本栏限制为仅包含一个特定字符的条目
- java - 我们应该总是释放 Java 中的 Map 或 List 吗?
- java - 设计困境——背景还是契约?(Java/科特林)
- go - Golang struct 的动态属性设置
- firebase - 在 Firebase Firestore 中查询时出错
- android - 收听 onDownloadComplete Exoplayer
- javascript - 使用 webpack-dev-server 时,chrome 扩展中未定义 chrome
- python - Xarray:使同一Dataset中的两个DataArrays使用相同的坐标系
- python - 将参数传递给嵌套函数
- ansible - 错误:运行/home/circleci/project/ansible/inventory.yml --list 出现问题([Errno 8] Exec 格式错误)