Pandas 如何将时间戳列转换为日期时间?

Pandas 如何将时间戳列转换为日期时间?,pandas,datetime,timestamp,Pandas,Datetime,Timestamp,拥有数据帧:需要将时间戳列转换为日期时间 readable = datetime.utcfromtimestamp(1551348672).strftime('%d-%m-%Y %H:%M:%S') print(readable) 28-02-2019 10:11:12 对于一个值来说,这是可以的,但是对于一个名为time\u q的数据帧列,我需要得到这个结果 data_pre["time_q"] = data_pre["time_q"].map(lambda x: datetime

拥有数据帧:需要将时间戳列转换为日期时间

 readable = datetime.utcfromtimestamp(1551348672).strftime('%d-%m-%Y 
 %H:%M:%S')
 print(readable) 28-02-2019 10:11:12
对于一个值来说,这是可以的,但是对于一个名为time\u q的数据帧列,我需要得到这个结果

data_pre["time_q"] = data_pre["time_q"].map(lambda x: 
datetime.utcfromtimestamp(str(x)).strftime('%d-%m-%Y %H:%M:%S'))

Traceback (most recent call last)
     <ipython-input-65-7f8191ae6c70> in <module>
----> 1 data_pre["time_q"] = data_pre["time_q"].map(lambda x: 
     datetime.utcfromtimestamp(str(x)).strftime('%d-%m-%Y %H:%M:%S'))

~\Anaconda3\lib\site-packages\pandas\core\series.py in map(self, arg, 
na_action)
   2996         """
   2997         new_values = super(Series, self)._map_values(
-> 2998             arg, na_action=na_action)
  2999         return self._constructor(new_values,
  3000                                  
index=self.index).__finalize__(self)

~\Anaconda3\lib\site-packages\pandas\core\base.py in _map_values(self, 
mapper, na_action)
   1002 
   1003         # mapper is a function
-> 1004         new_values = map_f(values, mapper)
   1005 
      1006         return new_values

pandas/_libs/src\inference.pyx in pandas._libs.lib.map_infer()

<ipython-input-65-7f8191ae6c70> in <lambda>(x)
----> 1 data_pre["time_q"] = data_pre["time_q"].map(lambda x: 
datetime.utcfromtimestamp(str(x)).strftime('%d-%m-%Y %H:%M:%S'))

TypeError: an integer is required (got type str)
data_pre[“time_q”]=data_pre[“time_q”].map(λx:
datetime.utcfromtimestamp(str(x)).strftime(“%d-%m-%Y%H:%m:%S”))
回溯(最近一次呼叫最后一次)
在里面
---->1 data_pre[“time_q”]=data_pre[“time_q”].映射(λx:
datetime.utcfromtimestamp(str(x)).strftime(“%d-%m-%Y%H:%m:%S”))
地图中的~\Anaconda3\lib\site packages\pandas\core\series.py(self,arg,
纳乌行动)
2996         """
2997新值=超级(系列、自).\u映射\u值(
->2998 arg,na_动作=na_动作)
2999返回self.\u构造函数(新的\u值,
3000
index=self.index.。\uuuuu最终确定\uuuuuu(self)
~\Anaconda3\lib\site packages\pandas\core\base.py in\u map\u值(self,
制图员(纳乌行动)
1002
1003#映射器是一个函数
->1004新值=映射(值,映射器)
1005
1006返回新的_值
pandas/_-libs/src\inference.pyx在pandas._-libs.lib.map\u-infere()中
in(x)
---->1 data_pre[“time_q”]=data_pre[“time_q”].映射(λx:
datetime.utcfromtimestamp(str(x)).strftime(“%d-%m-%Y%H:%m:%S”))
TypeError:需要一个整数(获取类型str)
我希望Column time_q的所有值都是例如28-02-2019 10:11:12和其他使用此格式的值;相反,我有一条错误消息

# if the time_q is not int then convert first to int
data_pre['time_q'] = data_pre['time_q'].astype(int)
data_pre["time_q"].apply(lambda x: datetime.utcfromtimestamp(x).strftime('%d-%m-%Y %H:%M:%S'))

多谢各位,现在对我来说结果还可以Great@cobolleccobolec upvote或approve,如果您觉得有帮助的话