Attributeerror: can only use .dt accessor with datetimelike values

Attributeerror_ can only use .dt accessor with datetimelike values

attributeerror: can only use .dt accessor with datetimelike values error occurs while converting string to datetime format in the specific situation. These specific situations are if are converting multiple string values into datetime format values (complete pandas dataframe column) and some of the values are having errors in conversion. Here we need to provide the error handling mechanism too with the syntax. Apart from this, In some scenario, the interpreter throws the same error when we do not pass the format for conversion. We face this error.

Attributeerror: Can only use .dt accessor with datetimelike values ( Solution ) –

As I already explained the root cause for the error. In this section we will see the best way to fix can only use .dt accessor with datetime like values error.

Approach 1: Error handler type –

While converting any string object to datatime object in python we all use the to_datetime() function. But we do add any error handler type and if it fails, The interpreter thows the same error. Actually In to_datetime() we can provide an error handler type while calling the same function. Here is an example.

df['converted_col'] = pd.to_datetime(df['col'], errors='coerce')

Suppose you have multiple values in the dataframe column “col” in string format and you want to convert them into datetime format. Then you can use the same syntax. If there will be any error in any row, It will convert the same into NaN and rest will be converted.

Attributeerror can only use .dt accessor with datetimelike values
Attributeerror can only use .dt accessor with datetimelike values


There are two more possible arguments with errors –
1. Ignore – It will throw the same input
2. raise – It will throw the exception and halt the process there only.

Approach 2 :

In some scenarios, if we add the format of the date conversion in the to_datetime() function. We can get rid of the same error. Here is an example of this –

df['converted_col'] = pd.to_datetime(df.col, format='%Y-%m-%d %H:%M:%S')

Here “col” is the column where we are Appling the same function.

Note :

Here is the generic solution for all types of AttributeError. Please go throw it for in-depth knowledge on the same line.

What is AttributeError in Python ? Complete Overview



Data Science Learner Team

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Abhishek ( Chief Editor) , a data scientist with major expertise in NLP and Text Analytics. He has worked on various projects involving text data and have been able to achieve great results. He is currently manages, where he and his team share knowledge and help others learn more about data science.
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner