1

I have a pandas data frame:

Name     Date
Bob      2020-05-17
Alice    2020-04-01

Below is the expected result: for each Name group, I'd like to keep the original row with 2 more rows of past 2 days' value in Date

Name     Date
Bob      2020-05-17
Bob      2020-05-16
Bob      2020-05-15
Alice    2020-04-01
Alice    2020-03-31
Alice    2020-03-30

Thanks in advance!

MAMS
  • 419
  • 1
  • 6
  • 17

1 Answers1

1

Use pd.date_range in a list comprehension to assign the dates inline, then DataFrame.explode:

df = (df.assign(Date=[pd.date_range(end=e, periods=3, freq='D')
                for e in df['Date']])
      .explode('Date'))

[out]

    Name         Date
0    Bob   2020-05-15
0    Bob   2020-05-16
0    Bob   2020-05-17
1  Alice   2020-03-30
1  Alice   2020-03-31
1  Alice   2020-04-01

If the date ordering is important, you may need to chain on an additional sort_values method, followed by sort_index:

(df.assign(Date=[pd.date_range(end=e, periods=3, freq='D')
                for e in df['Date']])
      .explode('Date')
      .sort_values(['Date'], ascending=False)
      .sort_index())
Chris Adams
  • 18,389
  • 4
  • 22
  • 39
  • Thank you! This should work. My only question is that explode() is for pandas version 0.25.0 and after. My env has pandas 0.24.2 and I'd prefer not to update it. Are you aware of any alternative of explode() to make it work? – MAMS Aug 03 '20 at 15:46
  • Hey @MAMS, check out [this question](https://stackoverflow.com/questions/53218931/how-to-unnest-explode-a-column-in-a-pandas-dataframe) for many more ways to explode the column. Hope it helps. – Chris Adams Aug 03 '20 at 15:57
  • 1
    It helps. Thank you very much! – MAMS Aug 03 '20 at 16:03