Eliran Nider
Eliran Nider

Reputation: 75

convert to df column to datetime - raise SettingWithCopyWarning

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'])

Upvotes: 4

Views: 2032

Answers (1)

jkr
jkr

Reputation: 19310

Use the .loc syntax for getting and setting values. This syntax has the benefit of being clearer (i.e., it is more apparent whether you are referencing rows or columns).

You write that you tried .loc, but you don't show the code that didn't work. Try the below.

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.loc[:, columns]
restaurant_data.loc[:, ['Cash', 'Credit','Total Shift']] = restaurant_data.loc[:, ['Cash', 'Credit','Total Shift']].apply(pd.to_numeric)
restaurant_data.loc[:, 'Date'] = pd.to_datetime(restaurant_data.loc[:, 'Date'])
restaurant_data.loc[:, 'Day'] = restaurant_data.loc[:, 'Date'].dt.day_name()

Upvotes: 3

Related Questions