Skip to content Skip to sidebar Skip to footer

Pandas Convert Yearly To Monthly

I'm working on pulling financial data, in which some is formatted in yearly and other is monthly. My model will need all of it monthly, therefore I need that same yearly value rep

Solution 1:

You want resample

First, you need to set the index so that resample will work. Then you backfill and reset the index.

df.set_index('date').resample('M').bfill().reset_index()datetickervalue01999-12-31  ECB/RA61.012000-01-31  ECB/RA64.022000-02-29  ECB/RA64.032000-03-31  ECB/RA64.042000-04-30  ECB/RA64.052000-05-31  ECB/RA64.062000-06-30  ECB/RA64.072000-07-31  ECB/RA64.082000-08-31  ECB/RA64.092000-09-30  ECB/RA64.0102000-10-31  ECB/RA64.0112000-11-30  ECB/RA64.0122000-12-31  ECB/RA64.0132001-01-31  ECB/RA62.0142001-02-28  ECB/RA62.0152001-03-31  ECB/RA62.0...

To handle this per ticker

df.set_index('date').groupby('ticker', group_keys=False) \
    .resample('M').bfill().reset_index()

Post a Comment for "Pandas Convert Yearly To Monthly"