Python 如何转换';浮动64';在数据帧中添加时间戳

Python 如何转换';浮动64';在数据帧中添加时间戳,python,pandas,dataframe,timestamp,Python,Pandas,Dataframe,Timestamp,这是我的数据 id enter_time 1 1.481044e+12 2 1.486d74e+12 这是我的预期输出 id enter_time enter_timestamp 1 1.481044e+12 2017-07-14 08:10:03 2 1.486774e+12 2017-07-15 08:10:00 注:上述期望中“输入时间戳”的值为假,仅用于给出预期格式的想法,以下为额外信息,以备回答时需要 In : main['enter_time'].dtyp

这是我的数据

id enter_time
1  1.481044e+12
2  1.486d74e+12
这是我的预期输出

id enter_time     enter_timestamp
1  1.481044e+12   2017-07-14 08:10:03
2  1.486774e+12   2017-07-15 08:10:00
注:上述期望中“输入时间戳”的值为假,仅用于给出预期格式的想法,以下为额外信息,以备回答时需要

In : main['enter_time'].dtype
Out: dtype('float64')
我已经试过了

import datetime
main['loan_timestamp'] = datetime.datetime.fromtimestamp(main['loan_time'])
以及输出

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-27-cfe2d514debb> in <module>
      1 import datetime
----> 2 main['loan_timestamp'] = datetime.datetime.fromtimestamp(main['loan_time'])

/opt/conda/lib/python3.8/site-packages/pandas/core/series.py in wrapper(self)
    127         if len(self) == 1:
    128             return converter(self.iloc[0])
--> 129         raise TypeError(f"cannot convert the series to {converter}")
    130 
    131     wrapper.__name__ = f"__{converter.__name__}__"

TypeError: cannot convert the series to <class 'int'>

---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在里面
1导入日期时间
---->2 main['loan_timestamp']=datetime.datetime.fromtimestamp(main['loan_time']))
/包装器中的opt/conda/lib/python3.8/site-packages/pandas/core/series.py(self)
127如果len(self)==1:
128返回转换器(self.iloc[0])
-->129 raise TypeError(f“无法将序列转换为{converter}”)
130
131包装器
TypeError:无法将序列转换为

尝试使用pandas to_datetime()(我假设第二个输入浮点中的字符“d”是一个拼写错误,所以我将其替换为):


我假设输入时间的单位是毫秒,因为秒或更高会导致超出边界错误(时间戳的基本限制)。

它在我的数据集上工作,谢谢这回答了你的问题吗?是的@MrFuppes,它在[25]中给出了答案,但我不知道它来自unix时代。没问题,我只是添加了链接,以便其他人可以很容易地找到相关资源。
import pandas as pd

df = pd.DataFrame([(1, 1.481044e+12), (2, 1.48674e+12)], columns=['id', 'enter_time'])
df['enter_timestamp'] = pd.to_datetime(df['enter_time'], unit='ms')
df

    id  enter_time  enter_timestamp
0   1   1.481044e+12    2016-12-06 17:06:40
1   2   1.486740e+12    2017-02-10 15:20:00