将微秒(?)转换为日期和时间

问题描述 投票:0回答:1

我有:

index| raw_data
150 1535932800000000
151 1506902400000000
152 1506902400000000
153 1506902400000000
154 1506902400000000
155 1506902400000000

我执行了此代码:

#seconds
df_csv['document_date'] = df_csv['document_date'].floordiv(1000000)
#date and time
df_csv['document_date'] = df_csv['document_date'].apply(lambda x: datetime.utcfromtimestamp(x).strftime('%Y-%m-%d %H:%M:%S'))

结果:

index| transformed_data
150 2018-09-03 00:00:00
151 2017-10-02 00:00:00
152 2017-10-02 00:00:00
153 2017-10-02 00:00:00
154 2017-10-02 00:00:00
155 2017-10-02 00:00:00

我用过,来源:

https://www.timestampconvert.com/?go2=true&offset=0&timestamp=1535932800&Submit=++++++Convert+to+Date++++++

并且至少第150和151行没有正确返回小时,有人可以理解为什么/为什么吗?如果缺少一些信息,请询问。谢谢:)

python pandas datetime
1个回答
0
投票

所以,遵循@MrFuppes的建议:

   df_csv['document_date']=  pd.to_datetime(df_csv['document_date'], utc=True, unit='us')

并且得到:

    150 2018-09-03 00:00:00+00:00
    151 2017-10-02 00:00:00+00:00
    152 2017-10-02 00:00:00+00:00
    153 2017-10-02 00:00:00+00:00
    154 2017-10-02 00:00:00+00:00
    155 2017-10-02 00:00:00+00:00

希望是有帮助的:)

© www.soinside.com 2019 - 2024. All rights reserved.