时间戳字符串(Unix时间)到datetime或pandas.Timestamp

时间:2016-03-23 12:00:36

标签: python datetime pandas timestamp unix-timestamp

从源代码我以JSON格式检索一些数据。我想将这些数据(及时测量)保存为文本文件。我一再想要使用相同的信号源,看看是否有新的测量结果,如果有,我想将其添加到其他测量中。

我得到的数据如下:

{"xyz":[{"unixtime":"1458255600","time":"00:00","day":"18\/03","value":"11","paramlabel":"30-500 mHz","popupcorr":"550","iconnr":"7","paramname":"30-500 mHz"},{"unixtime":"1458256200","time":"00:10","day":"18\/03","value":"14","paramlabel":"30-500 mHz","popupcorr":"550","iconnr":"7","paramname":"30-500 mHz"},etc.]}

我将此数据加载到pandas DataFrame中,以便能够更轻松地使用它。但是,当我将其加载到数据框中时,所有列都被视为字符串。如何确保将unixtime列视为时间戳(以便我可以转换为日期时间)?

1 个答案:

答案 0 :(得分:6)

使用to_datetime并传递ConcurrentHashMap,在使用astypeunit='s'转换为dtype后将值视为纪元时间:

int

示例:

df['unixtime'] = pd.to_datetime(df['unixtime'].astype(int), unit='s')