date range for six monthly in pandas

﹥>﹥吖頭↗ 提交于 2019-12-04 15:21:14
Ben.T

It's not exactely what you want, but a work around giving good enough results. I think you can do by calculating the time difference between column DiagnosisDate and Answerdate, and divide by pd.np.timedelta64(6, 'M') (to change the frequency to 6 months). Then you need the ceil function to get the integer above, such as:

data['6month'] = (pd.np.ceil((data['DiagnosisDate']-pd.Timedelta(days=1)-data['Answerdate'])
                                             /pd.np.timedelta64(6, 'M')).astype(int))

for ignoring negative columns:

data = data[(data['6month'] >= 0)]

With your sample, it gives:

    PatientNumber            QT Answer Answerdate DiagnosisDate  6month
1               1  transferring     No 2017-03-03    2018-03-05       3
2               1     preparing     No 2017-03-03    2018-03-05       3
3               1   medications    Yes 2017-03-03    2018-03-05       3
4               2  transferring     No 2011-10-05    2012-04-05       1
5               2     preparing     No 2011-10-05    2012-04-05       1
6               2   medications     No 2011-10-05    2012-04-05       1
7               2  transferring    Yes 2011-03-15    2012-04-05       3
8               2     preparing    Yes 2011-03-15    2012-04-05       3
9               2   medications     No 2011-03-15    2012-04-05       3
10              2  transferring    Yes 2010-12-15    2012-04-05       3
11              2     preparing     No 2010-12-15    2012-04-05       3
12              2   medications     No 2010-12-15    2012-04-05       3
13              2  transferring    Yes 2009-10-10    2012-04-05       5
14              2     preparing     No 2009-10-10    2012-04-05       5
15              2   medications     No 2009-10-10    2012-04-05       5
16              3   medications     No 2008-10-10    2010-04-07       3

Also, I would not use pd.date_range as it seems not to act like you want, but I might be wrong.

EDIT: to remove the case where DiagnosisDate is before Answerdate, once you have created your column 6months, just do data = data[data['6months'] > 0] as the value would be negative or zero in this case

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!