0

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.

Nick ODell
  • 15,465
  • 3
  • 32
  • 66
Alex
  • 39
  • 5
  • As I explain in the question, I have tried what the answer marked as a duplicate of suggests and the problem does not go away. So as far as I am concerned, this is not a duplicate. – Alex Dec 20 '18 at 15:54
  • try creating a copy of the dataframe and work on that copy instead of the original dataframe: df.copy() – Jessica Dec 20 '18 at 16:08
  • You should provide a **[mcve]** from scratch. If you need help with this, see **[How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples)**. – jpp Dec 20 '18 at 23:19

0 Answers0