How to avoid getting the SettingWithCopyWarning with pandas to_datetime method

I am using pandas 1.0.1 and I am creating a new column that converts the date column to a datetime column and I am getting the warning below. I tried using data.loc[:, "Datetime"] as well and I still got the same warning. Please how could this be avoided?

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: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  data["Datetime"] = pd.to_datetime(data["Date"], infer_datetime_format=True)

Answer

Most likely you created your source DataFrame as a view of another DataFrame (only some columns and / or only some rows).

Find in your code the place where your DataFrame is created and append .copy() there.

Then your DataFrame will be created as a fully independent DataFrame (with its own data buffer) and this warning should not appear any more.