Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
311 views
in Technique[技术] by (71.8m points)

python - Adding rows for each month in a dataframe based on column date

I am dealing with financial data which i need to extrapolate for different months. Here is my dataframe:

invoice_id,date_from,date_to
30492,2019-02-04,2019-09-18

I want to break this up for different months between date_from and date_to. Hence i need to add rows for each month with month starting date to ending date. Final output should look like:

invoice_id,date_from,date_to
30492,2019-02-04,2019-02-28
30492,2019-03-01,2019-03-31
30492,2019-04-01,2019-04-30
30492,2019-05-01,2019-05-31
30492,2019-06-01,2019-06-30
30492,2019-07-01,2019-07-31
30492,2019-08-01,2019-08-30
30492,2019-09-01,2019-09-18

Need to take care of leap year scenario as well. Is there any native method already available in pandas datetime package which i can use to achieve the desired output ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use:

print (df)
   invoice_id  date_from    date_to
0       30492 2019-02-04 2019-09-18
1       30493 2019-01-20 2019-03-10

#added months between date_from and date_to
df1 = pd.concat([pd.Series(r.invoice_id,pd.date_range(r.date_from, r.date_to, freq='MS')) 
                 for r in df.itertuples()]).reset_index()
df1.columns = ['date_from','invoice_id']

#added starts of months - sorting for correct positions
df2 = (pd.concat([df[['invoice_id','date_from']], df1], sort=False, ignore_index=True)
         .sort_values(['invoice_id','date_from'])
         .reset_index(drop=True))

#added MonthEnd and date_to  to last rows
mask = df2['invoice_id'].duplicated(keep='last')
s = df2['invoice_id'].map(df.set_index('invoice_id')['date_to'])
df2['date_to'] = np.where(mask, df2['date_from'] + pd.offsets.MonthEnd(), s)

print (df2)
    invoice_id  date_from    date_to
0        30492 2019-02-04 2019-02-28
1        30492 2019-03-01 2019-03-31
2        30492 2019-04-01 2019-04-30
3        30492 2019-05-01 2019-05-31
4        30492 2019-06-01 2019-06-30
5        30492 2019-07-01 2019-07-31
6        30492 2019-08-01 2019-08-31
7        30492 2019-09-01 2019-09-18
8        30493 2019-01-20 2019-01-31
9        30493 2019-02-01 2019-02-28
10       30493 2019-03-01 2019-03-10

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...