How to calculate age based on PESEL number in Python Pandas?

Viewed 168

In my country we have PESEL number which consists of 11 numbers.

  • First two numbers means year of birth for example: 00 (means 2000), 95 (means 1995) and so on...
  • Next two numbers means month.
  • Next two numbers means day.

It looks like that: 95050611475, so in this case:

  • year is: 1995
  • month is: 05
  • day is: 06

I have column with PESEL numbers like below:

enter image description here

And how can I calculate age based on PESEL number and current date ?

**Be aware that if someone has 00 at the beginning of PESEL it means that he born at 2000 year.

1 Answers

You can extract the substring from PESEL and convert it to datetime. Then, you can subtract it from the current timestamp. This will give you the time delta in days.

If you want to convert this information to year you can divide the result with np.timedelta64(1, 'Y').

df['age'] = (pd.Timestamp.now() - pd.to_datetime(df.PESEL.str[:6], format = '%y%m%d')) / np.timedelta64(1, 'Y')
         PESEL        age
0  95050611475  26.093457
1  00112575862  20.535506
Related