[英]Python - Convert epoch in object format to datetime
I'm pulling data from API that gives me date values in epoch in milliseconds, but the values are in this format:我正在从 API 中提取数据,这些数据以毫秒为单位为我提供了纪元的日期值,但这些值采用以下格式:
I am trying to convert these to datetime with pd.to_datetime(df1.loc[:, 'Curve Value'], unit='ms')
, but am getting the error我正在尝试使用
pd.to_datetime(df1.loc[:, 'Curve Value'], unit='ms')
将这些转换为日期时间,但出现错误
ValueError: non convertible value 1,609,459,200,000.0000000000 with the unit 'ms'
ValueError:不可转换的值 1,609,459,200,000.0000000000,单位为“毫秒”
I then tried to format the column into float with df1["Curve Value"] = df["Curve Value"].astype(float)
, but get then get this error然后我尝试使用
df1["Curve Value"] = df["Curve Value"].astype(float)
将列格式化为浮点数,但随后得到此错误
ValueError: could not convert string to float: '1,609,459,200,000.0000000000'
ValueError:无法将字符串转换为浮点数:'1,609,459,200,000.0000000000'
I've also tried various ways to remove the commas and convert to float, but get errors trying that, as well.我还尝试了各种方法来删除逗号并转换为浮点数,但尝试也会出错。
A bit unwieldy and requires importing datetime from datetime but it works as far as I can see.有点笨拙,需要从日期时间导入日期时间,但据我所知它可以工作。
df['Real Time'] = df['Curve Value'].apply(lambda t: datetime.fromtimestamp(int(''.join(t.split(',')[:4]))))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.