Home > Enterprise >  datetime conversion of a column results in pandas warning
datetime conversion of a column results in pandas warning

Time:01-25

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?

CodePudding user response:

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.

  • Related