Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas warning with pd.to_datetime

Tags:

python

pandas

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: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
Name: TS, dtype: object

And I want change each items in tt into datetime type, and get the hour. The code is

for i in tt.index:
   tt[i]=pd.to_datetime(tt[i])

and waring is

__main__:2: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy

Why the warning occurs and how can I deal with it?

If I change one item each time, it works, the code is

>>> tt[1]=pd.to_datetime(tt[1])
>>> tt[1].hour
0
like image 883
Alice W Avatar asked Oct 23 '25 12:10

Alice W


1 Answers

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)
like image 104
EdChum Avatar answered Oct 26 '25 03:10

EdChum



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!