Skip to main content
added 99 characters in body
Source Link
Alexander
  • 110.5k
  • 32
  • 212
  • 208

You can do an outer merge, set the new index to Dat, sort it, forward fill, and then reindex based on the dates in pub.

pubdates = ['2015-01-01', '2015-01-04', '2015-01-06']
pub = pd.DataFrame([dt.datetime.strptime(ts, '%Y-%m-%d').date() for ts in dates], 
                   columns=['Dat'])

>>> (ref
     .merge(pd.DataFrame(pub, columns=['Dat']), on='Dat', how='outer')
     .ffillset_index('Dat')
     .set_indexsort_index('Dat')
     .ffill()
     .reindex(pub.Dat))
            col1  col2
Dat                   
2015-01-01     5     4
2015-01-04     86     97
2015-01-06     8     9

You can do an outer merge, forward fill, and then reindex based on the dates in pub.

pub = ['2015-01-01', '2015-01-04', '2015-01-06']
>>> (ref
     .merge(pd.DataFrame(pub, columns=['Dat']), on='Dat', how='outer')
     .ffill()
     .set_index('Dat')
     .reindex(pub))
            col1  col2
Dat                   
2015-01-01     5     4
2015-01-04     8     9
2015-01-06     8     9

You can do an outer merge, set the new index to Dat, sort it, forward fill, and then reindex based on the dates in pub.

dates = ['2015-01-01', '2015-01-04', '2015-01-06']
pub = pd.DataFrame([dt.datetime.strptime(ts, '%Y-%m-%d').date() for ts in dates], 
                   columns=['Dat'])

>>> (ref
     .merge(pub, on='Dat', how='outer')
     .set_index('Dat')
     .sort_index()
     .ffill()
     .reindex(pub.Dat))
            col1  col2
Dat                   
2015-01-01     5     4
2015-01-04     6     7
2015-01-06     8     9
Source Link
Alexander
  • 110.5k
  • 32
  • 212
  • 208

You can do an outer merge, forward fill, and then reindex based on the dates in pub.

pub = ['2015-01-01', '2015-01-04', '2015-01-06']
>>> (ref
     .merge(pd.DataFrame(pub, columns=['Dat']), on='Dat', how='outer')
     .ffill()
     .set_index('Dat')
     .reindex(pub))
            col1  col2
Dat                   
2015-01-01     5     4
2015-01-04     8     9
2015-01-06     8     9