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!
source share