0

I have two date time dataframes which looks like: df1:

0   2020-03-16 14:00:00
1   2020-03-16 17:00:00
2   2020-03-16 20:00:00
3   2020-03-16 20:00:00
4   2020-03-16 19:00:00
5   2020-03-16 12:00:00
6   2020-03-16 18:00:00

df2:

0   2020-03-16 09:00:00
1   2020-03-16 09:00:00
2   2020-03-16 09:00:00
3   2020-03-16 08:30:00
4   2020-03-16 08:30:00
5   2020-03-16 08:30:00
6   2020-03-16 09:00:00

I wanted to delete df2 from df1 and convert the result into minutes. For that purpose I am converting each dataframe into datetime.strptime by using following code:

datetime.strptime(df1.to_string(),'%Y-%m-%d %H:%M:%S')

However, when I run above code, it throws an error message of Value error and it says that value does not match format '%Y-%m-%d %H:%M:%S'. Could anyone point out where am I making the mistake?

Nicolas Gervais
  • 33,817
  • 13
  • 115
  • 143
user2293224
  • 2,128
  • 5
  • 28
  • 52

1 Answers1

3

This works for me:

pd.to_datetime(df1.date, format='%Y-%m-%d %H:%M:%S') # I named my column 'date'

The reason it's not working is that when you use to_string() it gives something like this:

Out[78]: '0   2020-03-15 09:00:00\n1   2020-03-15 09:00:00\n2   2020-03-15 08:30:00\n3 

Here's my full code after copying your example dataframe:

import pandas as pd

df1 = pd.read_clipboard(index_col=0, header=None)
df1['date'] = df1.iloc[:, 0] + ' ' + df1.iloc[:, 1]
pd.to_datetime(df1.date, format='%Y-%m-%d %H:%M:%S')
Nicolas Gervais
  • 33,817
  • 13
  • 115
  • 143