Python 将时间戳(历元时间)转换为适当的日期时间列

Python 将时间戳(历元时间)转换为适当的日期时间列,python,pandas,datetime,timestamp,Python,Pandas,Datetime,Timestamp,尝试将列从时间戳转换为datetime,所需结果最后打印。 源代码是Unix历元时间戳。我可以转换单个值,但不能对整列数据进行转换。 谢谢 import pandas as pd import numpy as np from datetime import datetime thd = pd.DataFrame(data=data["candles"]) data={'candles': [{'open': 1417.5, 'high': 1417.52, 'low': 1416

尝试将列从时间戳转换为datetime,所需结果最后打印。 源代码是Unix历元时间戳。我可以转换单个值,但不能对整列数据进行转换。 谢谢

import pandas as pd 
import numpy as np
from datetime import datetime
thd = pd.DataFrame(data=data["candles"])
data={'candles': [{'open': 1417.5,
   'high': 1417.52,
   'low': 1416.95,
   'close': 1416.95,
   'volume': 508,
   'datetime': 1590758940},
  {'open': 1416.95,
   'high': 1420.18,
   'low': 1415.06,
   'close': 1416.64,
   'volume': 32934,
   'datetime': 1590759000},]
}

thd["Time"]=pd.to_datetime(thd["datetime"])
display(thd)
datetime.fromtimestamp(1590759060) #<---this is the result I want to get on dataframe
将熊猫作为pd导入
将numpy作为np导入
从日期时间导入日期时间
thd=pd.DataFrame(数据=数据[“蜡烛”])
数据={'cands':[{'open':1417.5,
“高”:1417.52,
“低”:1416.95,
“关闭”:1416.95,
“卷”:508,
“日期时间”:1590758940},
{'open':1416.95,
“高”:1420.18,
“低”:1415.06,
“关闭”:1416.64,
“卷”:32934,
“日期时间”:1590759000},]
}
thd[“Time”]=pd.to_datetime(thd[“datetime”])
显示器(thd)

datetime.fromtimestamp(1590759060)#您可以对列使用apply来为列中的每个值执行此函数

thd['datetime'].apply(datetime.fromtimestamp)
完整代码

import pandas as pd 
import numpy as np
from datetime import datetime

data={'candles': [{'open': 1417.5,
'high': 1417.52,
'low': 1416.95,
'close': 1416.95,
'volume': 508,
'datetime': 1590758940},
{'open': 1416.95,
'high': 1420.18,
'low': 1415.06,
'close': 1416.64,
'volume': 32934,
'datetime': 1590759000},]
}

thd = pd.DataFrame(data=data["candles"])

thd['datetime_converted'] = thd['datetime'].apply(datetime.fromtimestamp)

thd
结果

unit='s'一起使用

In [731]: thd['datetime'] = pd.to_datetime(thd['datetime'], unit='s') 

In [732]: thd                                                          
Out[732]: 
      open     high      low    close  volume            datetime
0 1,417.50 1,417.52 1,416.95 1,416.95     508 2020-05-29 13:29:00
1 1,416.95 1,420.18 1,415.06 1,416.64   32934 2020-05-29 13:30:00