Issue
Why does the code below gives rise to the following error
UFuncTypeError: ufunc 'subtract' cannot use operands with types dtype('<M8[ns]') and dtype('float64')
?
df1 = pd.DataFrame(['2022-01-01'], columns=['start'])
df1['start'] = pd.to_datetime(df1['start'])
df2 = pd.DataFrame(['2023-01-01'], columns=['end'])
df2['end'] = pd.to_datetime(df2['end'])
df2 - df1
Even though I can see that both arguments are showing datetime64[ns]
:
print(df1.loc[0])
start 2022-01-01
Name: 0, dtype: datetime64[ns]
print(df2.loc[0])
end 2023-01-01
Name: 0, dtype: datetime64[ns]
Solution
You subtract first dataframe from second. If you want to get difference between date in dataframes, you need:
df2['end'] - df1['start']
Answered By - Kirill Kondratenko
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.