2

I am receiving the following warning message when performing the following transformation of a date into a string:

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

I am doing the following:

Data['Date'] = Data['Date'].dt.strftime('%Y-%m-%d')
8
  • It actually suggests you to Try using .loc[row_indexer,col_indexer] = value instead Commented Jan 24, 2017 at 19:53
  • @VitaliiStrimbanu, thanks but I tried Data.loc['Date'] = Data['Date'].dt.strftime('%Y-%m-%d')it is not working and I receive the following error message: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame Commented Jan 24, 2017 at 19:57
  • See this: stackoverflow.com/questions/30132282/… Commented Jan 24, 2017 at 20:02
  • You're going to have to use .apply and a lambda function out on your dataframe ala: stackoverflow.com/questions/19738169/… Commented Jan 24, 2017 at 20:04
  • 1
    Try Data.loc[:, 'Date'] = Data['Date'].dt.strftime('%Y-%m-%d'). See more info in this post Commented Jan 24, 2017 at 20:04

1 Answer 1

2

If you provide a sample of your code, I can amend this answer to be more specific to your requirements. If this answer is deemed superfluous to the answer given in the quoted question, let me know, and I'll delete it.

I should make it clear, the following is a near exact replication of the answer to this question here: datetime to string with series in python pandas.

For some data frame:

df = pandas.Series(['20010101', '20010331'])

0    20010101
1    20010331
dtype: object

# Convert to the format you need:

dates = pandas.to_datetime(A, format = '%Y-%m-%d')

0   2001-01-01
1   2001-03-31
dtype: datetime64[ns]

# Then to convert back, do:

df2 = dates.apply(lambda x: x.strftime('%Y-%m-%d'))

0    2001-01-01
1    2001-03-31
dtype: object
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. The answers above helped. You can delete it if you want but it also helps for other problems.
Ok glad it helped, I'll leave it up. But @EdChum should know: this is down to you, I make no contention.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.