将数据集中的时间戳重新格式化为datetime

2024-05-16 18:49:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我想重新格式化数据集中的时间戳,使其成为日期+时间

这是我的数据集

[1]: https://i.stack.imgur.com/kYLEA.png

我试过这个

data1 = pd.read_excel(r"C:\Users\user\Desktop\Consumption.xlsx")

data1['Timestamp']= pd.to_datetime(['Timestamp'], unit='s')

我犯了这个错误

ValueError: non convertible value Timestamp with the unit 's'

我还试图不在pd.to_datetime函数中传递“unit”,但它给出了一个错误 时间戳的类型是对象。请帮忙


Tags: to数据readdatetime错误时间unitexcel
3条回答

试试这个:

data1['Date'] = pd.DataFrame(data1['Timestamp'], format ='%d/%m/%Y')

我建议您检查函数here的文档

如果要添加日期时间,可以采用以下格式:

  • format='%d/%m/%Y %H:%M:%S'

datetimes的格式不是unix时间,因此引发错误。您可以按;分割值,按str[1]选择第二个列表,然后转换为日期时间:

data1['Timestamp']= pd.to_datetime(data1['Timestamp'].str.split(';').str[1])

相关问题 更多 >