data ['TS']。dt.second有效,但不适用于Apply函数。
data['TS'] # dtype: datetime64[ns]
0 2019-06-27 10:13:25
1 2019-06-27 10:13:08
2 2019-06-27 10:11:53
data['TS'].dt.second
0 25
1 8
2 53
from datetime import timedelta
def myTimeFun(tsRow):
addSec=0 # or some Value
thisSec=tsRow.dt.second
return tsRow + timedelta(seconds=addSec)
data['TS'].apply(myTimeFun)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-130-9a000b768604> in <module>
----> 1 data['TS_Apx30Sec'] = data['TS'].apply(myTimeFun)
2 data['TS_Apx30Sec']
~\AppData\Local\conda\conda\envs\py370\lib\site-packages\pandas\core\series.py in apply(self, func, convert_dtype, args, **kwds)
4036 else:
4037 values = self.astype(object).values
-> 4038 mapped = lib.map_infer(values, f, convert=convert_dtype)
4039
4040 if len(mapped) and isinstance(mapped[0], Series):
pandas\_libs\lib.pyx in pandas._libs.lib.map_infer()
<ipython-input-125-dc9c61ebdaa8> in timeApprx30Sec(tsRow)
3 def myTimeFun(tsRow):
4 addSec=0
----> 5 thisSec=tsRow.dt.second
AttributeError: 'Timestamp' object has no attribute 'dt'
答案 0 :(得分:0)
thisSec=tsRow.second #Works & Solved
def myTimeFun(tsRow):
addSec=0 # or some Value
thisSec=tsRow.second #thisSec=tsRow.dt.second
return tsRow + timedelta(seconds=addSec)
不确定为什么不thisSec=tsRow.dt.second
和data['TS'].dt.second
一样有效
如果有人对此发表评论是适用的。