从时间戳到日期时间

From timestamp to datetime

我没有使用时间戳的经验。我开始分析包含一列时间戳的新数据帧,例如:time= 1599091199986769056。我想将一列时间戳转换为日期和时间。所以我在一行中尝试了以下操作:

ts = int("1599091199986769056")
print(datetime.utcfromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S'))

但是我收到以下错误:

----> 1 print(datetime.utcfromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S'))

OSError: [Errno 22] Invalid argument

谁能告诉我哪里做错了以及如何将列从时间戳转换为日期时间?

您可以使用to_datetime函数:

import pandas as pd

ts = int("1599091199986769056")

df = pd.DataFrame([ts], columns=['Timestamp'])
df['Timestamp'] = pd.to_datetime(df['Timestamp'])
print(df)

输出:

                      Timestamp
0 2020-09-02 23:59:59.986769056