在 Python 中分隔日期

Separating Dates in Python

我正在尝试使用日期对数据进行预测。问题是我有这样的数据集

    LeaveStartDate  TotalLeaveDays
0   2020-03-14  1.0
1   2020-03-18  2.0
2   2020-03-20  1.0
3   2020-01-13  3.0
4   2020-02-15  1.0

我想像这样扩展总叶数:

  LeaveStartDate   TotalLeaveDays

    0  2020-03-14       1.0

    1   2020-03-18      1.0

    2   2020-03-19      1.0

    3   2020-01-20      1.0

    4   2020-01-13      1.0

    5   2020-01-14      1.0

    6   2020-01-15      1.0

    7   2020-02-15      1.0 

我应该怎么做才能得到这种形式的数据

使用 Index.repeat by column TotalLeaveDays, then add counter values converted to days timedeltas by GroupBy.cumcount and to_timedelta 并最后将 1 设置为 TotalLeaveDays 列:

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

df = df.loc[df.index.repeat(df['TotalLeaveDays'])]
df['LeaveStartDate'] += pd.to_timedelta(df.groupby(level=0).cumcount(), unit='D')
df['TotalLeaveDays'] = 1
df = df.reset_index(drop=True)
print (df)
  LeaveStartDate  TotalLeaveDays
0     2020-03-14               1
1     2020-03-18               1
2     2020-03-19               1
3     2020-03-19               1
4     2020-03-20               1
5     2020-03-21               1
6     2020-01-13               1
7     2020-02-17               1