gpt4 book ai didi

python - 将 Pandas DatetimeIndex 转换为数字格式

转载 作者:太空狗 更新时间:2023-10-30 01:47:45 25 4
gpt4 key购买 nike

我想将我的 DataFrame 中的 DatetimeIndex 转换为浮点格式,这可以在我的模型中进行分析。有人能告诉我该怎么做吗?我需要使用 date2num() 函数吗?非常感谢!

最佳答案

转换为 Timedelta 并从 dt.total_seconds 中提取总秒数:

df

date
0 2013-01-01
1 2013-01-02
2 2013-01-03
3 2013-01-04
4 2013-01-05
5 2013-01-06
6 2013-01-07
7 2013-01-08
8 2013-01-09
9 2013-01-10

pd.to_timedelta(df.date).dt.total_seconds()

0 1.356998e+09
1 1.357085e+09
2 1.357171e+09
3 1.357258e+09
4 1.357344e+09
5 1.357430e+09
6 1.357517e+09
7 1.357603e+09
8 1.357690e+09
9 1.357776e+09
Name: date, dtype: float64

或者,也许,将数据呈现为 int 类型会更有用:

pd.to_timedelta(df.date).dt.total_seconds().astype(int)

0 1356998400
1 1357084800
2 1357171200
3 1357257600
4 1357344000
5 1357430400
6 1357516800
7 1357603200
8 1357689600
9 1357776000
Name: date, dtype: int64

关于python - 将 Pandas DatetimeIndex 转换为数字格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46501626/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com