3

I have one dataframe which looks like below:

                   Date_1                Date_2
0                5 Dec 2017                5 Dec 2017
1               14 Dec 2017               14 Dec 2017
2               15 Dec 2017               15 Dec 2017
3   18 Dec 2017 21 Dec 2017   18 Dec 2017 21 Dec 2017
4              22 Dec 2017               22 Dec 2017

Conditions to be checked:

  1. Want to check if any row contains two dates or not like 3rd row. If present split them into two separate rows.
  2. Apply the datetime on both columns.

I am trying to do the same operation like below:

df['Date_1'] = pd.to_datetime(df['Date_1'], format='%d %b %Y')

But getting below error:

ValueError: unconverted data remains:

Expected Output:

                   Date_1                Date_2
0                5 Dec 2017               5 Dec 2017
1               14 Dec 2017               14 Dec 2017
2               15 Dec 2017               15 Dec 2017
3               18 Dec 2017               18 Dec 2017 
4               21 Dec 2017               21 Dec 2017
5               22 Dec 2017                22 Dec 2017
ketan
  • 2,732
  • 11
  • 34
  • 80

1 Answers1

4

After using regex with findall get the you date , your problem become a unnesting problem

s=df.apply(lambda x : x.str.findall(r'((?:\d{,2}\s)?(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[a-z]*(?:-|\.|\s|,)\s?\d{,2}[a-z]*(?:-|,|\s)?\s?\d{,4})'))

unnesting(s,['Date_1','Date_2']).apply(pd.to_datetime)
Out[82]: 
      Date_1     Date_2
0 2017-12-05 2017-12-05
1 2017-12-14 2017-12-14
2 2017-12-15 2017-12-15
3 2017-12-18 2017-12-18
3 2017-12-21 2017-12-21
4 2017-12-22 2017-12-22
BENY
  • 317,841
  • 20
  • 164
  • 234