1

This is the code for calculating age of a customer dataset.

from datetime import date
  
def calculate_age(birthdate): 
    today = date.today()
    age = today.year - birthdate.year - ((today.month, today.day) <  (birthdate.month, birthdate.day))
    return Age

Age = asbank['Cust_dob '].apply(calculate_age)

But I am getting the below error. All the help is appreciated. Thanks.

AttributeError                            Traceback (most recent call last)
<ipython-input-20-a5191ae217de> in <module>
      6     return Age
      7 
----> 8 Age = asbank['Cust_dob '].apply(calculate_age)

~\anaconda3\lib\site-packages\pandas\core\series.py in apply(self, func, convert_dtype, args, **kwds)
   3846             else:
   3847                 values = self.astype(object).values
-> 3848                 mapped = lib.map_infer(values, f, convert=convert_dtype)
   3849 
   3850         if len(mapped) and isinstance(mapped[0], Series):

pandas\_libs\lib.pyx in pandas._libs.lib.map_infer()

<ipython-input-20-a5191ae217de> in calculate_age(birthdate)
      3 def calculate_age(birthdate):
      4     today = date.today()
----> 5     age = today.year - birthdate.year - ((today.month, today.day) <  (birthdate.month, birthdate.day))
      6     return Age
      7 

AttributeError: 'str' object has no attribute 'year'
  • You're trying to call `year()` against a `string`. You likely need to convert your `dataframe` column to `datetime` https://stackoverflow.com/a/17134750/7942856 – PacketLoss Jan 16 '21 at 11:23

0 Answers0