我有以下代码来计算两个数据帧列的小时差异并将其放入新列中:
def transform_data(data_frame):
start = pd.to_datetime(data_frame['start'])
end = pd.to_datetime(data_frame['end'])
print(start, end)
data_frame['hours'] = (end-start)
return fetch_layers(data_frame)
如果我运行它会打印以下内容:
3402959 2010-09-06 09:00:00
3402961 2010-09-07 09:30:00
...
3403125 2011-04-15 08:30:00
3403126 2011-04-18 09:00:00
Name: start, dtype: datetime64[ns]
3402959 2010-09-06 18:00:00
3402961 2010-09-07 18:00:00
...
Name: end, dtype: datetime64[ns]
Series([], Name: start, dtype: datetime64[ns]) Series([], Name: end, dtype: datetime64[ns])
从这一点看来,两列现在都有日期时间,但是当减去它时会出现以下错误:
TypeError: ufunc subtract cannot use operands with types dtype('<M8[ns]') and dtype('O')
奇怪的是,这段代码适用于我的大多数数据,但只能在一小部分数据中失败。是什么导致了这个错误?