close

[Solved] Attribute: Can only use .dt accessor with datetimelike values

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Attribute: Can only use .dt accessor with datetimelike values in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Attribute: Can only use .dt accessor with datetimelike values Error Occurs?

Today I get the following error Attribute: Can only use .dt accessor with datetimelike values in python.

How To Solve Attribute: Can only use .dt accessor with datetimelike values Error ?

  1. How To Solve Attribute: Can only use .dt accessor with datetimelike values Error ?

    To Solve Attribute: Can only use .dt accessor with datetimelike values Error Your problem here is that the dtype of 'Date' remained as str/object. You can use the parse_dates parameter when using read_csv

  2. Attribute: Can only use .dt accessor with datetimelike values

    To Solve Attribute: Can only use .dt accessor with datetimelike values Error Your problem here is that the dtype of 'Date' remained as str/object. You can use the parse_dates parameter when using read_csv

Solution 1

Your problem here is that to_datetime silently failed so the dtype remained as str/object, if you set param errors='coerce' then if the conversion fails for any particular string then those rows are set to NaT.

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

So you need to find out what is wrong with those specific row values.

Solution 2

Your problem here is that the dtype of ‘Date’ remained as str/object. You can use the parse_dates parameter when using read_csv

import pandas as pd
file = '/pathtocsv.csv'
df = pd.read_csv(file, sep = ',', parse_dates= [col],encoding='utf-8-sig', usecols= ['Date', 'ids'],)    
df['Month'] = df['Date'].dt.month

From the documentation for the parse_dates parameter

parse_dates : bool or list of int or names or list of lists or dict, default False

The behavior is as follows:

  • boolean. If True -> try parsing the index.
  • list of int or names. e.g. If [1, 2, 3] -> try parsing columns 1, 2, 3 each as a separate date column.
  • list of lists. e.g. If [[1, 3]] -> combine columns 1 and 3 and parse as a single date column.
  • dict, e.g. {‘foo’ : [1, 3]} -> parse columns 1, 3 as date and call result ‘foo’

If a column or index cannot be represented as an array of datetimes, say because of an unparseable value or a mixture of timezones, the column or index will be returned unaltered as an object data type. For non-standard datetime parsing, use pd.to_datetime after pd.read_csv. To parse an index or column with a mixture of timezones, specify date_parser to be a partially-applied pandas.to_datetime() with utc=True. See Parsing a CSV with mixed timezones for more.

Note: A fast-path exists for iso8601-formatted dates.

The relevant case for this question is the “list of int or names” one.

col is the columns index of ‘Date’ which parses as a separate date column.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read