Python 3.x 是否为两列之间的所有日期添加行?

Python 3.x 是否为两列之间的所有日期添加行?,python-3.x,pandas,pandas-groupby,Python 3.x,Pandas,Pandas Groupby,是否为两列之间的所有日期添加行 使用: 你好请花点时间阅读这篇文章,以及如何提供答案,并相应地修改你的问题。这些关于的提示可能也很有用。您想要什么?问题不清楚**类型错误:无法根据规则“安全”***data=data.loc[data.index.repeat((data['End\u Date']-data['Start\u Date']].dt.days+1)将数组数据从dtype('float64')强制转换为dtype('int64') ID Initiation_Date S

是否为两列之间的所有日期添加行

使用:


你好请花点时间阅读这篇文章,以及如何提供答案,并相应地修改你的问题。这些关于的提示可能也很有用。您想要什么?问题不清楚**类型错误:无法根据规则“安全”***
data=data.loc[data.index.repeat((data['End\u Date']-data['Start\u Date']].dt.days+1)将数组数据从dtype('float64')强制转换为dtype('int64')
ID     Initiation_Date  Step    Start_Date   End_Date    Days

P-03    29-11-2018        3      2018-11-29  2018-12-10  11.0
P-04    29-11-2018        4      2018-12-03  2018-12-07   4.0
P-05    29-11-2018        5      2018-12-07  2018-12-07   0.0
mydata = [{'ID' : '10', 'Entry Date': '10/10/2016', 'Exit Date': '15/10/2016'},
          {'ID' : '20', 'Entry Date': '10/10/2016', 'Exit Date': '18/10/2016'}]

df = pd.DataFrame(mydata)

#convert columns to datetimes
df[['Entry Date','Exit Date']] = df[['Entry Date','Exit Date']].apply(pd.to_datetime)

#repeat index by difference of dates
df = df.loc[df.index.repeat((df['Exit Date'] - df['Entry Date']).dt.days + 1)]
#add counter duplicated rows to day timedeltas to new column
df['Date'] = df['Entry Date'] + pd.to_timedelta(df.groupby(level=0).cumcount(), unit='d')
#default RangeIndex
df = df.reset_index(drop=True)
print (df)
   Entry Date  Exit Date  ID       Date
0  2016-10-10 2016-10-15  10 2016-10-10
1  2016-10-10 2016-10-15  10 2016-10-11
2  2016-10-10 2016-10-15  10 2016-10-12
3  2016-10-10 2016-10-15  10 2016-10-13
4  2016-10-10 2016-10-15  10 2016-10-14
5  2016-10-10 2016-10-15  10 2016-10-15
6  2016-10-10 2016-10-18  20 2016-10-10
7  2016-10-10 2016-10-18  20 2016-10-11
8  2016-10-10 2016-10-18  20 2016-10-12
9  2016-10-10 2016-10-18  20 2016-10-13
10 2016-10-10 2016-10-18  20 2016-10-14
11 2016-10-10 2016-10-18  20 2016-10-15
12 2016-10-10 2016-10-18  20 2016-10-16
13 2016-10-10 2016-10-18  20 2016-10-17
14 2016-10-10 2016-10-18  20 2016-10-18