我有一个熊猫数据框,其中以天,分钟和小时为单独的数字。我正在尝试以日期和时间的格式创建一列。我可以使用timedelta函数添加日期和小时,但不能添加分钟。
我研究了各种现有的python代码-但无法弄清楚这里出了什么问题。它适用于3列中的2列,相同的算法不适用于第三列。
df_training['hour'] = df_training['timestamp'].apply(lambda x: x.split(':')[0]).astype('float64')
df_training['min'] = df_training['timestamp'].apply(lambda x: x.split(':')[1]).astype('float64')
df_training['day'] = df_training['day'].astype('float64')
df_training['date_time_2'] = df_training['day'].apply(lambda x: date_base+ timedelta(days = x))
df_training['date_time_2'] = df_training[['date_time_2','hour']].apply(lambda x: x.date_time_2 + timedelta(hours = x.hour), axis = 1)```
df_training['date_time_2'] = df_training[['date_time_2','min']].apply(lambda x: x.date_time_2.timedelta(minutes = x.min), axis = 1)
获得错误消息
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-72-0ec3568f21b6> in <module>
----> 1 df_training['date_time_2'] = df_training[['date_time_2','min']].apply(lambda x: x.date_time_2.timedelta(minutes = x.min), axis = 1)
~/anaconda3/lib/python3.7/site-packages/pandas/core/frame.py in apply(self, func, axis, broadcast, raw, reduce, result_type, args, **kwds)
6485 args=args,
6486 kwds=kwds)
-> 6487 return op.get_result()
6488
6489 def applymap(self, func):
~/anaconda3/lib/python3.7/site-packages/pandas/core/apply.py in get_result(self)
149 return self.apply_raw()
150
--> 151 return self.apply_standard()
152
153 def apply_empty_result(self):
~/anaconda3/lib/python3.7/site-packages/pandas/core/apply.py in apply_standard(self)
255
256 # compute the result using the series generator
--> 257 self.apply_series_generator()
258
259 # wrap results
~/anaconda3/lib/python3.7/site-packages/pandas/core/apply.py in apply_series_generator(self)
284 try:
285 for i, v in enumerate(series_gen):
--> 286 results[i] = self.f(v)
287 keys.append(v.name)
288 except Exception as e:
<ipython-input-72-0ec3568f21b6> in <lambda>(x)
----> 1 df_training['date_time_2'] = df_training[['date_time_2','min']].apply(lambda x: x.date_time_2.timedelta(minutes = x.min), axis = 1)
AttributeError: ("'Timestamp' object has no attribute 'timedelta'", 'occurred at index 0')