0

I have data table that looks like this:

Date            Name
12/1/2020       Golikeri, Dave
12/2/2020       Mewman, Tim 
12/6/2020       Nang, Bob
12/9/2020       Book, Tom

For each record in in the data table, I want to create duplicate but the duplicate DATE would equal the day after the original date. So for example, the new table would look like this:

Date            Name
12/1/2020       Golikeri, Dave
12/2/2020       Golikeri, Dave
12/2/2020       Mewman, Tim 
12/3/2020       Mewman, Tim 
12/6/2020       Nang, Bob
12/7/2020       Nang, Bob
12/9/2020       Book, Tom
12/10/2020      Book, Tom

Not quite sure where to even begin with this question but hoping someone would generously lead me on the right path - thanks!

Raven
  • 849
  • 6
  • 17
  • 1
    what code have you tried so far please? You have been on SO so you should already know that we will want to see your work effort. Thanks for sharing your work effort. – Joe Ferndz Feb 04 '21 at 20:56
  • @JoeFerndz I honestly hadn't any code yet, I came across this post but was only digging myself into a deeper hole of confusion https://stackoverflow.com/questions/24029659/python-pandas-replicate-rows-in-dataframe – Raven Feb 04 '21 at 21:04
  • 1
    Yes, replicate the row, then for the second value, you can always timedelta. Alternate, is what Quang did. – Joe Ferndz Feb 04 '21 at 21:05

1 Answers1

2

You can use pd.concat:

df['Date'] = pd.to_datetime(df['Date'])

out = (pd.concat([df, df.assign(Date=df['Date']+pd.Timedelta('1D')) ] )
         .sort_index()
      )

Output:

        Date            Name
0 2020-12-01  Golikeri, Dave
0 2020-12-02  Golikeri, Dave
1 2020-12-02     Mewman, Tim
1 2020-12-03     Mewman, Tim
2 2020-12-06       Nang, Bob
2 2020-12-07       Nang, Bob
3 2020-12-09       Book, Tom
3 2020-12-10       Book, Tom
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74
  • 1
    pd.Timedelta('1D') is the way to go. We could also do a repeat of the df, then replace the duplicated row with Timedelta. ? – Joe Ferndz Feb 04 '21 at 21:02
  • 1
    @JoeFerndz certainly, that could work too. – Quang Hoang Feb 04 '21 at 21:05
  • thanks - works great! for my own learning purposes, can you explain how the concat and assign commands function in this code? – Raven Feb 04 '21 at 21:05
  • 1
    assign is sort-hand for `df1 = df.copy(); df1['Date'] = df['Date'] + ...` and `concat` is just stacking the frames on top of each other. – Quang Hoang Feb 04 '21 at 21:07