简体   繁体   English

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

[英]timestamp string (Unix time) to datetime or pandas.Timestamp

From a source I retrieve some data in JSON format. 从源代码我以JSON格式检索一些数据。 I want to save this data (measurements in time) as a text file. 我想将这些数据(及时测量)保存为文本文件。 Repeatedly I want to go the same source and see if new measurements are available, if so I want to add it to the other measurements. 我反复想要使用相同的信号源并查看是否有新的测量值,如果是,我想将其添加到其他测量值。

The data I get looks like this: 我得到的数据如下:

{"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.]}

I load this data into a pandas DataFrame to be able to work with it more easily. 我将这些数据加载到pandas DataFrame中,以便能够更轻松地使用它。 When I load this into a dataframe however, all columns are treated as strings. 但是,当我将其加载到数据框中时,所有列都被视为字符串。 How can I make sure that the unixtime column is treated as a timestamp (such that I can convert to a datetime)? 如何确保将unixtime列视为时间戳(以便我可以转换为日期时间)?

use to_datetime and pass unit='s' to treat the value as epoch time after converting the dtype to int using astype : 使用to_datetime并传递unit='s'变换后以治疗该值作为信号出现时间dtypeint使用astype

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

Example: 例:

In [162]:
pd.to_datetime(1458255600, unit='s')

Out[162]:
Timestamp('2016-03-17 23:00:00')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM