python - 将每日数据重新采样为每小时数据帧并复制内容

标签 python python-3.x pandas

我有以下数据框:

    Date        Holiday
0   2018-01-01  New Year's Day
1   2018-01-15  Martin Luther King, Jr. Day
2   2018-02-19  Washington's Birthday
3   2018-05-08  Truman Day
4   2018-05-28  Memorial Day
...     ...     ...
58  2022-10-10  Columbus Day
59  2022-11-11  Veterans Day
60  2022-11-24  Thanksgiving
61  2022-12-25  Christmas Day
62  2022-12-26  Christmas Day (Observed)

我想重新采样此数据框,使其成为每日 df 中的每小时 df(同时将假期列中的内容复制到正确的日期)。我希望它看起来像这样[忽略表的索引,它应该比这个多很多]

    Timestamp           Holiday
0   2018-01-01 00:00:00     New Year's Day
1   2018-01-01 01:00:00     New Year's Day
2   2018-01-01 02:00:00     New Year's Day
3   2018-01-01 03:00:00     New Year's Day
4   2018-01-01 04:00:00     New Year's Day
5   2018-01-01 05:00:00     New Year's Day
...     ...     ...
62  2022-12-26 20:00:00     Christmas Day (Observed)
63  2022-12-26 21:00:00     Christmas Day (Observed)
64  2022-12-26 22:00:00     Christmas Day (Observed)
65  2022-12-26 23:00:00     Christmas Day (Observed)

最快的方法是什么?提前致谢。

最佳答案

怎么样

df.set_index("Date").resample("H").ffill().reset_index().rename(
    {"Date": "Timestamp"}, axis=1
)

关于python - 将每日数据重新采样为每小时数据帧并复制内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59522090/

相关文章:

python - 尝试拟合回归模型时出现 ValueError

string - BeautifulSoup Div 类返回空

python - 在 Python 3.x 中,将非类指定为基类是否合法?

python - 引用正在定义的数据框的列

python - 将 (x, y) 坐标的元组对设置为 dict 作为带有 id 值的键

python - 能够定位元素但不能点击它

java - 在java中加载 tensorflow 模型

python - 如何使用 GItHub Actions 测试我的 python 包?

python - Pandas 在数据帧中向后插值()

python - 类实例中的增量实例计数器