首页 > 解决方案 > 使用python将SQL时间戳点转换为日期时间

问题描述

我正在尝试使用 python 将在 SQL 中创建的时间戳转换为 datetime 对象或 therabouts。

这是数据集

<bound method NDFrame.to_clipboard of                id                       user_id  sentiment  magnitude
created                                                              
1601820360     10  cPL1Fg7BqRXvSFKeU1mJT7KCCTq2       -0.1        0.1
1601820365     11  cPL1Fg7BqRXvSFKeU1mJT7KCCTq2       -0.8        0.8
1601900938     12  cPL1Fg7BqRXvSFKeU1mJT7KCCTq2       -0.2        0.2
1601900956     13  cPL1Fg7BqRXvSFKeU1mJT7KCCTq2       -0.2        0.2
1601900971     14  cPL1Fg7BqRXvSFKeU1mJT7KCCTq2        0.2        0.2
...           ...                           ...        ...        ...
1618553420  45024  cPL1Fg7BqRXvSFKeU1mJT7KCCTq2       -1.0        1.0
1618553422  45025  cPL1Fg7BqRXvSFKeU1mJT7KCCTq2       -1.0        1.0
1618553430  45026  cPL1Fg7BqRXvSFKeU1mJT7KCCTq2       -1.0        1.0
1618553432  45027  cPL1Fg7BqRXvSFKeU1mJT7KCCTq2       -1.0        1.0
1618883226  46022  cPL1Fg7BqRXvSFKeU1mJT7KCCTq2       -1.0        1.0

[10506 rows x 4 columns]>

created 是有问题的时间戳。

关于最佳方法的任何想法?浏览一下python文档,我有点迷失了。谢谢!

标签: pythonpandas

解决方案


您可能需要使用单位作为秒。

pd.to_datetime(df.created, unit='s')

数据集中的样本:

输入:

    created     id
0   1601820360  10
1   1601820365  11

输出

     created    id
0   2020-10-04 14:06:00 10
1   2020-10-04 14:06:05 11

推荐阅读