datetime conversion of a column results in pandas warning

Question:

I am trying to convert a column in a pandas dataframe to datetime format as follows:

df["date"] = pd.to_datetime(df["date"])

Although this works as expected, pandas gives the following warning:

A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  if sys.path[0] == '':

Is there a better way to to a datetime conversion of a pandas column that does not produce this warning?

Asked By: veg2020

||

Answers:

This should get rid of the warning:

df.loc["date"] = pd.to_datetime(df["date"])

Pandas discourages it if you set a slice of a dataset. Generally, using .loc is the best way to go when accessing your data.

Answered By: Morgan Lewis
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.