转换 pandas 时间戳列表

Convert a pandas Timestamp list

在我的变量'Datelist3'中有一个pandas时间戳列表,格式如下:

[Timestamp('2019-12-04 09:00:00+0100', tz='Europe/Rome'), Timestamp('2019-12-04 09:30:00+0100', tz='Europe/Rome'), ....]

我无法将此列表转换为日期时间字符串列表,格式如下:

['2019-12-04 09:00:00', '2019-12-04 09:30:00', .....]

我做了这些测试:

Datelist3.to_datetime # -> error: 'list' object has no attribute 'to_datetime'
Datelist3.dt.to_datetime # -> error: 'list' object has no attribute 'dt'
Datelist3.to_pydatetime() # -> error: 'list' object has no attribute 'to_pydatetime()'
Datelist3.dt.to_pydatetime() # -> error: 'list' object has no attribute 'dt'

我使用以下语句访问了变量 'Datelist3':

Datelist3 = quoteIntraPlot.index.tolist()

如果我将此指令更改为:

Datelist3 = quoteIntraPlot.index.strftime("%Y-%m-%d %H:%M:%S").tolist()

这正是我想要实现的。 问题是在 10 次中,6-7 次是可以的,3-4 次它给我一个错误:“'Index' object has no 'strftime'”。这很奇怪。我该如何解决这个问题?

如果您的数据格式正确,这将有效:

time_list = [Timestamp('2019-12-04 09:00:00+0100', tz='Europe/Rome'), Timestamp('2019-12-04 09:30:00+0100', tz='Europe/Rome'), ....]
str_list = [t.strftime("%Y-%m-%d %H:%M:%S") for t in time_list]

但是,如果您遇到与以前相同的错误,则意味着并非所有索引都是时间戳。在这种情况下,您需要先清理数据。