Skip to content Skip to sidebar Skip to footer

Attributeerror: Can Only Use .dt Accessor With Datetimelike Values In 0yrs 0mon Format

I am trying converting date string format to numeric, but I get some error, my date column like this : train['AVERAGE_ACCT_AGE'].head(6) 0 0yrs 0mon 1 1yrs 11mon 2 0yr

Solution 1:

The column is not of Datetime format.

Here is a quick way to get it to numeric. I am using more lines than needed.

# doing this so we can have it in string format
train['AVERAGE_ACCT_AGE'] = train['AVERAGE_ACCT_AGE'].astype(str)

#Now remove the trailing or any such spaces
train['AVERAGE_ACCT_AGE'] = train['AVERAGE_ACCT_AGE'].map(lambda x: x.strip())

#Next we split and expand the column into 2 columns:
train[['yrs','months']] = train['AVERAGE_ACCT_AGE'].str.split(' ',n=1,expand=True)

#remove characters from new columns, #I am assuming the characters remain the same

train['yrs'] = train['yrs'].str.replace('yrs','')
train['months'] = train['months'].str.replace('mon','')
# Convert yrs to float
train['yrs'] = train['yrs'].astype('float')
# Convert months to float
train['months'] = train['yrs'].astype('float')

Hope it helps.

Post a Comment for "Attributeerror: Can Only Use .dt Accessor With Datetimelike Values In 0yrs 0mon Format"