AttributeError: can only use .dt accessor with datetimelike values

Hi, I am using pandas to convert a column to a month. When I read my data, they are objects:

Date object dtype: object 

Therefore, I first make them on a date, and then try to make them for months:

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

Also if this helps:

 In [10]: df['Date'].dtype Out[10]: dtype('O') 

So, the error I get looks like this:

 /Library/Frameworks/Python.framework/Versions/2.7/bin/User/lib/python2.7/site-packages/pandas/core/series.pyc in _make_dt_accessor(self) 2526 return maybe_to_datetimelike(self) 2527 except Exception: -> 2528 raise AttributeError("Can only use .dt accessor with datetimelike " 2529 "values") 2530 AttributeError: Can only use .dt accessor with datetimelike values 

EDITED:

The date columns are as follows:

 0 2014-01-01 1 2014-01-01 2 2014-01-01 3 2014-01-01 4 2014-01-03 5 2014-01-03 6 2014-01-03 7 2014-01-07 8 2014-01-08 9 2014-01-09 

Do you have any ideas? Thank you very much!

+18
source share
2 answers

Your problem here is that to_datetime silently failed, so dtype remained as str/object , if you set param errors='coerce' , then if the conversion fails for any particular line, then these lines will be set to NaT .

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

So you need to find out what is wrong with these specific string values.

See documents

+46
source

Your problem here is that the dtype "Date" is left 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 value. If True -> try parsing the index.
  • list of int or names. for example, If [1, 2, 3] ->, try to analyze columns 1, 2, 3, each as a separate date column.
  • list of lists. for example, If [[1, 3]] → combine columns 1 and 3 and parse as a single date column.
  • dictate, for example {'Foo: [1, 3]} → parse columns 1, 3 as the date and result of calling' foo

If a column or index cannot be represented as an array of date and time, say, due to an illegible value or a combination of time zones, the column or index will be returned unchanged as the data type of the object. For custom date and time analysis, use pd.to_datetime after pd.read_csv . To analyze an index or a column with a mixture of time zones, specify that date_parser is partially applied by pandas.to_datetime() with utc=True . See the topic CSV Analysis with Mixed Time Zones for more information.

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

The relevant case for this question is the "list of integers or names".

col is the column index for Date, which is parsed as a separate date column.

+2
source

Source: https://habr.com/ru/post/1234593/


All Articles