2024-09-01
Python
00

目录

生成 Timestamp对象 列表
生成 str对象 列表
生成 python datetime对象 列表
转换

借助pd.date_range快速实现

生成 Timestamp对象 列表

生成一系列Timestamp对象,Timestamp对象是对python datetime再封装得到的,一般情况直接用起来就好了。

python
import pandas as pd a = list(pd.date_range(start='2021-06-21 00:00:00', end='2021-06-22 00:00:00', freq='H')) print(a)

得到:

[Timestamp('2021-06-21 00:00:00', freq='H'), Timestamp('2021-06-21 01:00:00', freq='H'), Timestamp('2021-06-21 02:00:00', freq='H'), Timestamp('2021-06-21 03:00:00', freq='H'), Timestamp('2021-06-21 04:00:00', freq='H'), Timestamp('2021-06-21 05:00:00', freq='H'), Timestamp('2021-06-21 06:00:00', freq='H'), Timestamp('2021-06-21 07:00:00', freq='H'), Timestamp('2021-06-21 08:00:00', freq='H'), Timestamp('2021-06-21 09:00:00', freq='H'), Timestamp('2021-06-21 10:00:00', freq='H'), Timestamp('2021-06-21 11:00:00', freq='H'), Timestamp('2021-06-21 12:00:00', freq='H'), Timestamp('2021-06-21 13:00:00', freq='H'), Timestamp('2021-06-21 14:00:00', freq='H'), Timestamp('2021-06-21 15:00:00', freq='H'), Timestamp('2021-06-21 16:00:00', freq='H'), Timestamp('2021-06-21 17:00:00', freq='H'), Timestamp('2021-06-21 18:00:00', freq='H'), Timestamp('2021-06-21 19:00:00', freq='H'), Timestamp('2021-06-21 20:00:00', freq='H'), Timestamp('2021-06-21 21:00:00', freq='H'), Timestamp('2021-06-21 22:00:00', freq='H'), Timestamp('2021-06-21 23:00:00', freq='H'), Timestamp('2021-06-22 00:00:00', freq='H')]

生成 str对象 列表

python
import pandas as pd a = list(pd.date_range(start='2021-06-21 00:00:00', end='2021-06-22 00:00:00', freq='H')) a=list(map(str,a)) print(a)

得到:

['2021-06-21 00:00:00', '2021-06-21 01:00:00', '2021-06-21 02:00:00', '2021-06-21 03:00:00', '2021-06-21 04:00:00', '2021-06-21 05:00:00', '2021-06-21 06:00:00', '2021-06-21 07:00:00', '2021-06-21 08:00:00', '2021-06-21 09:00:00', '2021-06-21 10:00:00', '2021-06-21 11:00:00', '2021-06-21 12:00:00', '2021-06-21 13:00:00', '2021-06-21 14:00:00', '2021-06-21 15:00:00', '2021-06-21 16:00:00', '2021-06-21 17:00:00', '2021-06-21 18:00:00', '2021-06-21 19:00:00', '2021-06-21 20:00:00', '2021-06-21 21:00:00', '2021-06-21 22:00:00', '2021-06-21 23:00:00', '2021-06-22 00:00:00']

生成 python datetime对象 列表

python
import pandas as pd a = list(pd.date_range(start='2021-06-21 00:00:00', end='2021-06-22 00:00:00', freq='H')) a=list(map(lambda x:x.to_pydatetime(),a)) print(a)

得到:

python
[datetime.datetime(2021, 6, 21, 0, 0), datetime.datetime(2021, 6, 21, 1, 0), datetime.datetime(2021, 6, 21, 2, 0), datetime.datetime(2021, 6, 21, 3, 0), datetime.datetime(2021, 6, 21, 4, 0), datetime.datetime(2021, 6, 21, 5, 0), datetime.datetime(2021, 6, 21, 6, 0), datetime.datetime(2021, 6, 21, 7, 0), datetime.datetime(2021, 6, 21, 8, 0), datetime.datetime(2021, 6, 21, 9, 0), datetime.datetime(2021, 6, 21, 10, 0), datetime.datetime(2021, 6, 21, 11, 0), datetime.datetime(2021, 6, 21, 12, 0), datetime.datetime(2021, 6, 21, 13, 0), datetime.datetime(2021, 6, 21, 14, 0), datetime.datetime(2021, 6, 21, 15, 0), datetime.datetime(2021, 6, 21, 16, 0), datetime.datetime(2021, 6, 21, 17, 0), datetime.datetime(2021, 6, 21, 18, 0), datetime.datetime(2021, 6, 21, 19, 0), datetime.datetime(2021, 6, 21, 20, 0), datetime.datetime(2021, 6, 21, 21, 0), datetime.datetime(2021, 6, 21, 22, 0), datetime.datetime(2021, 6, 21, 23, 0), datetime.datetime(2021, 6, 22, 0, 0)]

转换

pd.Timestamp 转 datetime.datetime :

python
pd.Timestamp("2021-06-06").to_pydatetime()

datetime.datetime 转 pd.Timestamp:

python
pd.to_datetime(datetime.datetime.now())
如果对你有用的话,可以打赏哦
打赏
ali pay
wechat pay

本文作者:Dong

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC。本作品采用《知识共享署名-非商业性使用 4.0 国际许可协议》进行许可。您可以在非商业用途下自由转载和修改,但必须注明出处并提供原作者链接。 许可协议。转载请注明出处!