一般的时间戳分为三种,分别是10位、13位、16位。
10位时间戳转时间这么写
df['time_s'] = pd.to_datetime(df['time_s'], unit='s').dt.strftime('%Y-%m-%d %H:%M:%S')
13位时间戳转时间这么写
df['time_s'] = pd.to_datetime(df['time_s']/1000, unit='s').dt.strftime('%Y-%m-%d %H:%M:%S')
16位时间戳转时间这么写
df['time_s'] = pd.to_datetime(df['time_s']/1000, unit='ms').dt.strftime('%Y-%m-%d %H:%M:%S')
不过这里有一个需要注意的地方,有时候线上线下时间转换后出现了不一致的问题,而且正好差八个小时。
这个原因是因为,线上可能指定了转换的标定日期,而且你在本地写代码的时候并没有指定,其默认指定日期是1970-01-01 00:00:00。
我们把它调后即可:
df['time_s'] = pd.to_datetime(df['time_s']/1000, unit='ms', origin='1970-01-01 08:00:00').
转载自:
https://zhuanlan.zhihu.com/p/412420860