I am aware of the implications of chaining get/set and the fact that it may mean I am working on a copy. If I use loc()
, I still get a warning (without the loc part, but still a warning).
I have a DF with a column 'A' which is a date but with a type string.
I am converting the string object to date (at all rows):
X['A'] = pd.to_datetime(X['A'])
I then turn it back to string formatted in a particular way. I know I am taking a detour here but I want to experiment:
X['A'] = X['user_created_date'].apply(lambda x: x.strftime("%H %w %d %m %y") if not pd.isnull(x) else '')
I then split the column to multiple columns accordingly:
X[['hour','day', 'dateofm', 'month', 'year']] = X['A'].str.split(' ', expand=True)
All this seems to be working, but I get the "A value is trying to be set on a copy of a slice from a DataFrame. Try to use loc..." warning three times.
I have also tried to create new columns on the LH of the assignments, so as not to try get and set the same columns at the same time. The warning persists.
What is the right way to do this? I want to set the original dataframe.