Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
5.0k views
in Technique[技术] by (71.8m points)

python - AttributeError: 'str' object has no attribute 'year'

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)

~anaconda3libsite-packagespandascoreseries.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\_libslib.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'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...