从源代码我以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列视为时间戳(以便我可以转换为日期时间)?
解决方法:
在使用astype
将dtype转换为int后,使用to_datetime
并传递unit =’s’将值视为纪元时间:
df['unixtime'] = pd.to_datetime(df['unixtime'].astype(int), unit='s')
例:
In [162]:
pd.to_datetime(1458255600, unit='s')
Out[162]:
Timestamp('2016-03-17 23:00:00')