Using pandas 0.6.2
. I want to change a dataframe to datetime
type, here is the dataframe
>>> tt.head()
0 2015-02-01 00:
Just do it on the entire Series
as to_datetime
can operate on array-like args and assign directly to the column:
In [72]:
df['date'] = pd.to_datetime(df['date'])
df.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 5 entries, 0 to 4
Data columns (total 1 columns):
date 5 non-null datetime64[ns]
dtypes: datetime64[ns](1)
memory usage: 80.0 bytes
In [73]:
df
Out[73]:
date
index
0 2015-02-01 00:46:28
1 2015-02-01 00:59:56
2 2015-02-01 00:16:27
3 2015-02-01 00:33:45
4 2015-02-01 13:48:29
If you changed your loop to this then it would work:
In [80]:
for i in df.index:
df.loc[i,'date']=pd.to_datetime(df.loc[i, 'date'])
df
Out[80]:
date
index
0 2015-02-01 00:46:28
1 2015-02-01 00:59:56
2 2015-02-01 00:16:27
3 2015-02-01 00:33:45
4 2015-02-01 13:48:29
the code moans because you're operating on potentially a copy of that row on the df and not a view, using the new indexers avoids this ambiguity
EDIT
It looks like you're using an ancient version of pandas, the following should work:
tt[1].apply(lambda x: x.hour)