0

I want to replace dates as strings using format yyyy-mm-dd to yyyymmddhhmmss as follows:

current value:  2020-11-06
new value:      20201106000000

Input:

         DATE
1  2020-11-06
2  2020-11-06
5  2015-09-28
6  2015-09-28
7  2015-09-28

I have the next piece of code taht works but throws a warning:

merge_filtered['DATE'] = merge_filtered['DATE'].str.replace('-', '')
merge_filtered['DATE'] = merge_filtered['DATE'] + '000000'

But I get the warnings at the output:

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
merge_filtered['DATE'] = merge_filtered['DATE'].str.replace('-', '')
            
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
merge_filtered['DATE'] = merge_filtered['DATE'] + '000000'
        
             DATE
1  20201106000000
2  20201106000000
5  20150928000000
6  20150928000000
7  20150928000000
gesgallar
  • 51
  • 5
  • 1
    At some point _before_ this provided code you have unsafely subset your DataFrame. Either `new_df = df[cols]` or `new_df = df[mask]` when it should have been `new_df = df[cols].copy()` or `new_df = df[mask].copy()` The warning is letting you know that `df[mask]['col'] = value` will may not work because `df[mask]` may produce a copy and recommends that you use `df.loc[mask, 'col'] = value` but that message is not helpful here. – Henry Ecker Oct 13 '21 at 00:33
  • 1
    Given the name of the variable, I would guess you did something like `merge_filtered = merged_df[some condition to filter rows]` where it should have been `merge_filtered = merged_df[some condition to filter rows].copy()` – Henry Ecker Oct 13 '21 at 00:36
  • 1
    @HenryEcker thanks a lot for your answer.. this solved the issue – gesgallar Oct 13 '21 at 00:50

0 Answers0