Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

pandas.to_datetime() returning different dtypes

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]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You subtract first dataframe from second. If you want to get
difference between date in dataframes, you need:

df2['end'] - df1['start']
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading