ValueError: non convertible value 2021-02-09 20:22 with the unit ‘s‘

错误复现

import pandas as pd
pd.to_datetime('2021-02-09 20:22', unit='s')

报错及原因

ValueError: could not convert string to float: ‘2021-02-09 20:22’
ValueError: non convertible value 2021-02-09 20:22 with the unit ‘s’

仔细看官方文档发现,使用unit这个参数的时候,第一个输入参数必须是数字格式的,不能是字符串。官方原话如下:

The unit of the arg (D,s,ms,us,ns) denote the unit, which is an integer or float number.

解决方案

把第一个输入参数更改成数字格式的即可。

import pandas as pd
pd.to_datetime(1, unit='s')

猜你喜欢

转载自blog.csdn.net/shiyuzuxiaqianli/article/details/116614761