Reputation:
My Data frame is below
I am trying to find the age
customer_Id DOB
0 268408 1970-02-01
1 268408 1970-02-01
2 268408 1970-02-01
3 268408 1970-02-01
4 268408 1970-02-01
shape is of 207518
while converting the data i got ValueError: unconverted data remains: 5
code is below to convert in to age
def cal_age(dob=str):
x = dt.datetime.strptime(dob, "%Y-%d-%m")
y = dt.date.today()
age = y.year - x.year - ((y.month, x.day) < (y.month, x.day))
return age
df_n_4['DOB'] = pd.to_datetime(df_n_4['DOB'],errors='coerce')
df_n_4['DOB'] = df_n_4['DOB'].astype('str')
df_n_4['Age'] = df_n_4.DOB.apply(lambda z: cal_age(z))
Error is below
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-113-90ef9d4c7002> in <module>
----> 1 df_n_4['Age'] = df_n_4.DOB.apply(lambda z: cal_age(z))
~\Anaconda3\lib\site-packages\pandas\core\series.py in apply(self, func, convert_dtype, args, **kwds)
4040 else:
4041 values = self.astype(object).values
-> 4042 mapped = lib.map_infer(values, f, convert=convert_dtype)
4043
4044 if len(mapped) and isinstance(mapped[0], Series):
pandas\_libs\lib.pyx in pandas._libs.lib.map_infer()
<ipython-input-113-90ef9d4c7002> in <lambda>(z)
----> 1 df_n_4['Age'] = df_n_4.DOB.apply(lambda z: cal_age(z))
<ipython-input-111-98546f386b50> in cal_age(dob)
1 def cal_age(dob=str):
----> 2 x = dt.datetime.strptime(dob, "%Y-%d-%m")
3 y = dt.date.today()
4 age = y.year - x.year - ((y.month, x.day) < (y.month, x.day))
5 return age
~\Anaconda3\lib\_strptime.py in _strptime_datetime(cls, data_string, format)
575 """Return a class cls instance based on the input string and the
576 format string."""
--> 577 tt, fraction, gmtoff_fraction = _strptime(data_string, format)
578 tzname, gmtoff = tt[-2:]
579 args = tt[:6] + (fraction,)
~\Anaconda3\lib\_strptime.py in _strptime(data_string, format)
360 if len(data_string) != found.end():
361 raise ValueError("unconverted data remains: %s" %
--> 362 data_string[found.end():])
363
364 iso_year = year = None
ValueError: unconverted data remains: 5
Upvotes: 0
Views: 53
Reputation: 863761
You can change your function for working with datetimes, also convert to strings is not necessary:
def cal_age(x):
y = dt.date.today()
age = y.year - x.year - ((y.month, x.day) < (y.month, x.day))
return age
df_n_4['DOB'] = pd.to_datetime(df_n_4['DOB'],errors='coerce')
df_n_4['Age'] = df_n_4.DOB.apply(cal_age)
Upvotes: 1
Reputation: 2189
try this code,
df['current_date']=pd.datetime.now()
df['age']=(df.current_date-pd.to_datetime(df.DOB)).dt.days/365
Upvotes: 0