datetime conversion - TypeError: only integer scalar arrays can be converted to a scalar index

datetime conversion - TypeError: only integer scalar arrays can be converted to a scalar index

我有这样一个数据框:

ds  y
22/02/18 13 1
28/07/19 14 1
09/02/21 15 2

我想获取日期时间列 ds 并使用:

df1['ds'] = pd.to_datetime(df1['ds'], format = '%d/%m/%y %H')

但是得到:

TypeError: only integer scalar arrays can be converted to a scalar index

我必须在这里做什么?问题是,完全相同的操作以前有效,但现在不可能了。

编辑:感谢您的所有回答!我在我的脚本中发现了一个可能导致错误的错误。请投票结束问题。

当数据时间为 'string' 格式时,转换工作正常。所以也许先把它转换成字符串格式?

data = {'ds':['22/02/18 13', '28/07/19 14', '09/02/21 15']}
df1 = pd.DataFrame(data=data)
df1['ds'] = pd.to_datetime(df1['ds'], format = '%d/%m/%y %H')
df1

输出:

ds
0   2018-02-22 13:00:00
1   2019-07-28 14:00:00
2   2021-02-09 15:00:00

希望这会有所帮助?