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

Printing date from Year, Month and Day columns in Pandas

I am looking to add a new column – "date" to my Pandas dataframe. Below are the first 5 rows of my dataframe:
First 5 rows of the dataframe
As seen from the image, the first column is year, second month, and third day. Below is what I have tried to do:

df['Year'] = pd.to_datetime(df[['Year','Month','Day']])

But, I keep getting the error as below:

ValueError: cannot assemble the datetimes: time data '610101' does not match format 
'%Y%m%d' (match)

It would be great if I can get any help for the same.

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 :

Following up on my comment, I was able to reproduce the error and solve it by adding 1900 to the year

df = pd.DataFrame({"year": [61,99], "month": [1, 2], "day": [3, 12]})
df["year"] = df["year"] + 1900
df['full_date'] = pd.to_datetime(df[['year','month','day']])

Output:

    year    month   day full_date
0   1961    1   3   1961-01-03
1   1999    2   12  1999-02-12

There is a format parameter to the to_datetime method but for some reason I wasn’t able to make it work. doc

df['full_date'] = pd.to_datetime(df[['year','month','day']], format="%y%m%d", infer_datetime_format=False)

this still throw the same error although I am using %y which should be 2 digit year but the error message still says it does not match this format '%Y%m%d'

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