-2

I have this table which contains column DateOfBirth like this :

     Gender  FullName           DateOfBirth
0    Male      Stan Smith       1980-02-10
1    Male      Nikola Griffin   1999-12-20
2    Female    Ruby Moore       1986-03-03

I want to find out the age of each fullname, based on this answer https://stackoverflow.com/a/26789573/12582712, I am doing this

import datetime as DT

import io

import numpy as np

import pandas as pd
name = {'Gender': ['Male','Male','Female'],
        'FullName': ['Stan Smith','Nikola Griffin','Ruby Moore'],
        'DateOfBirth' : ['1980-02-10', '1999-12-20', '1986-03-03']
        }

df = pd.DataFrame(name, columns = ['Gender', 'FullName', 'DateOfBirth'])

now = pd.Timestamp('now')

df['DateOfBirth'] = pd.to_datetime(df['DateOfBirth'], format='%y%m%d'

but when I am doing the last code, it says ValueError: time data '1980-02-10' does not match format '%y%m%d' (match)

18Man
  • 572
  • 5
  • 17

1 Answers1

5

You're setting the date format to %y%m%d which is equivalent to YearMonthDay However the data has hyphens separating the year, month and day. Therefore you need to reflect this in the last line of your code

Also from the docs %y is a two digit year (80, 99, 86). You need %Y instead as this is a four digit year (1980, 1999, 1986).

Therefore your final line should be

df['DateOfBirth'] = pd.to_datetime(df['DateOfBirth'], format='%Y-%m-%d')
Shaido
  • 27,497
  • 23
  • 70
  • 73
peteredhead
  • 2,394
  • 1
  • 15
  • 21