0

DATA

Division    Name      start_date  
A           apple     2001-01-05
A           banana    2001-03-06
A           apple     2001-06-08
A           orange    2001-07-09
B           peach     2001-01-10
B           melon     2001-06-02
B           berry     2001-08-19

I need to create an end_date, which is the start_date of the next person in the same division. For the last person observed, there is no end_date, so I would just put today's date 2019-04-06.

GOAL

Division    Name      start_date    end_date
A           apple     2001-01-05    2001-03-06
A           banana    2001-03-06    2001-06-08
A           apple     2001-06-08    2001-07-09
A           orange    2001-07-09    2019-04-06
B           peach     2001-01-10    2001-06-02
B           melon     2001-06-02    2001-08-19
B           berry     2001-08-19    2019-04-06

I tried

data['end_date'] = data.groupby('Division')['start_date'].index+1

But got an error message:

AttributeError: Cannot access attribute 'index' of 'SeriesGroupBy' objects, try using the 'apply' method

Does anyone know how to fix this?

Many thanks!

1 Answer 1

1

You can groupby division and shift one row up.

df['end_date'] = df.groupby('Division').start_date.shift(-1)

Then just fillna() with today's date

df = df.fillna(datetime.date.today())

    Division    Name    start_date  end_date
0   A           apple   2001-01-05  2001-03-06
1   A           banana  2001-03-06  2001-06-08
2   A           apple   2001-06-08  2001-07-09
3   A           orange  2001-07-09  2019-04-06
4   B           peach   2001-01-10  2001-06-02
5   B           melon   2001-06-02  2001-08-19
6   B           berry   2001-08-19  2019-04-06
Sign up to request clarification or add additional context in comments.

1 Comment

This works perfectly!! Thank you so much @RafaelC!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.