how to make interpret date as 3 12 not 31 2 in ddmmyyyy format

Viewed 68

my date format come in format 11122020 (ddmmyyyy) in a pandas column.

I use

datapdf["wholetime"]=pd.to_datetime(datapdf["wholetime"],format='%d%m%Y)  

to convert to time and do processing on the time.

recently my code failed for date 3122020 as

ValueError: day is out of range for month

python is interpreting as 31 2 2020 instead of 3 12 2020 which is causing the error. Any one have solution for this?

2 Answers

One way would be to use str.zfill to ensure that date is in 8 digits:

s = pd.Series(["11122020", "3122020"])
pd.to_datetime(s.str.zfill(8), format="%d%m%Y")

Output:

0   2020-12-11
1   2020-12-03
dtype: datetime64[ns]

Note that this answer only concerns about missing 0 in the day. It won't be able to parse more ambiguous items such as 332020, where the month part also requires heading 0.

Little bit newbie approach using apply I created custom parser for dates, if you have some other formats in it then you can tweak the function w.r.t your date formats,

import pandas as pd
data = {
    #assuming your dates are mix of ddmmyyyy,dmmyyyy,dmyyyy
    'date': ['11122020','3122020','572020','','222019','3112019']
}

df = pd.DataFrame(data)

def parser(elem):
  res = ''
  if len(elem) > 7:
    res = elem
  elif len(elem) > 6:
    d = '0' + elem[0]
    m = elem[1:3]
    y = elem[3:]
    res = d+m+y
  elif len(elem) > 5:
    d = '0' + elem[0]
    m = '0' + elem[1]
    y = elem[2:]
    res = d+m+y
  else:
    res = ''


  return pd.to_datetime(res, format='%d%m%Y',errors='coerce')

df['date'] = df['date'].apply(parser)
df

output:

    date
0   2020-12-11
1   2020-12-03
2   2020-07-05
3   NaT
4   2019-02-02
5   2019-11-03
Related