MCM
MCM

Reputation: 1511

SettingWithCopyWarning message when transforming Datetime Date into String Python Dataframe

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

Upvotes: 2

Views: 3530

Answers (1)

Chuck
Chuck

Reputation: 3852

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

Upvotes: 2

Related Questions