时间戳字符串(Unix 时间)到日期时间或 pandas.Timestamp
timestamp string (Unix time) to datetime or pandas.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 列视为时间戳(以便我可以转换为日期时间)?
使用to_datetime
and pass unit='s'
to treat the value as epoch time after converting the dtype
to int
using astype
:
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')
我从来源检索了一些 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 列视为时间戳(以便我可以转换为日期时间)?
使用to_datetime
and pass unit='s'
to treat the value as epoch time after converting the dtype
to int
using astype
:
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')