'Pandas datetime format without strftime

I have a CSV file that I open using pandas.

The format is DATE | AMOUNT1 | AMOUNT2

I use df[df.columns[0]]=pd.to_datetime(df[df.columns[0]], dayfirst=True).dt.strftime('%d-%B-%Y') to convert the date to my preferd format, however using dt.strftime changes the type of the data from datetime to strings.

Now, if I need to find the difference between two dates, for example, from the first datapoint to the last datapoint using print(df.iloc[-1][0]-df.iloc[0][0]) it will output an error

TypeError: unsupported operand type(s) for -: 'str' and 'str'

My question is: is there a way to have the same date format, without losing the datetime type?

I tried df[df.columns[0]]=pd.to_datetime(df[df.columns[0]], dayfirst=True, format='%d-%m-%Y') but it doesn't really work, the format doesn't change to be the disired.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source