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

Categories

0 votes
5.6k 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...