I have a Pandas DataFrame 'Date' column that I'm trying to convert to datetime. It's converting, and raising "SettingWithCopyWarning"
I tried to follow other excellent explanations like: How to deal with SettingWithCopyWarning in Pandas?, and others, but couldn't figure it out. Thank you all!
my original code:
import numpy as np
import pandas as pd
data = pd.DataFrame(pd.read_excel('Restaurant Shifts Data.xlsx', na_values='-')) # (-) value in cash
data.fillna(0)
columns = ['Waiter','Start','Finish','Cash','Credit','Total Hours','Total Shift','Date','Shift','Shift manager']
restaurant_data = data[columns]
restaurant_data[['Cash', 'Credit','Total Shift']] = restaurant_data[['Cash', 'Credit','Total Shift']].apply(pd.to_numeric)
restaurant_data['Date'] = pd.to_datetime(restaurant_data['Date'])
restaurant_data['Day'] = restaurant_data['Date'].dt.day_name()
Tried different combinations with .loc: restaurant_data.loc[:,'Date'] = pd.to_datetime(restaurant_data['Date'])
Date (data example)
0 2020-01-01
1 2020-01-01
2 2020-01-01
Full error message
...\python\python38-32\lib\site-packages\pandas\core\frame.py:2963: SettingWithCopyWarning:
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
self[k1] = value[k2]
<ipython-input-51-0b817be48c52>:10: SettingWithCopyWarning:
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
restaurant_data['Date'] = pd.to_datetime(restaurant_data['Date'])