0

I have two dataframes that I want to combine to add col SLR from df2 to df1 based on the datetimeindex in df1 and the datetime column in df2. The code below works, but I keep getting an error that I need to fix

df1:

datetimeindex       name  val 
2014-01-01           X    0.9
2014-02-01           Y    0.91
2014-03-01           Z    0.92

df2:

index    datetime        SLR 
1        2013-10-01       1
2        2013-11-01       2
3        2013-12-01       3

I am trying to combine df2 with df1 by the datetime column in df2 and the datetimeindex in df1 with this:

df1['SLR'] = df1['date'].map(df2.set_index('date')['SLR'])

This works, but I keep getting this error:

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  dryhobfr['SLR'] = dryhobfr['date'].map(SLR.set_index('date')['SLR'])
<ipython-input-140-9fa4b21486e9>:17: SettingWithCopyWarning: 
...
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
  wethobfr['SLR'] = wethobfr['date'].map(SLR.set_index('date')['SLR'])
user11958450
  • 109
  • 1
  • 7

1 Answers1

0

here is another way to do it. Your two df don't have a common date, so I can't really put the result here

df.merge(df2, how='left',
        left_on='datetimeindex',
        right_on='datetime').drop(columns=['index','datetime'])

I modified the first row of the df2 to match the df1 and here is the resultset

    datetimeindex   name    val     SLR
0   2014-01-01        X     0.90    1.0
1   2014-02-01        Y     0.91    NaN
2   2014-03-01        Z     0.92    NaN
Naveed
  • 11,495
  • 2
  • 14
  • 21