如何在python中将两个给定的时间戳(开始,结束)拆分为几天

我试图在两个给定的时间戳之间获得一天的间隔。我有三列:

id,start_date和end_date,我正在尝试实现以下目标。

In [1]: id = 12
        start_dt = '4/3/2020  10:19:55 AM'
        end_dt = '4/10/2020  1:23:26 PM'


        df = pd.DataFrame([[id,start_dt,end_dt]], columns = ['id', 'start_dt', 'end_dt'])
        df['start_dt'] = pd.to_datetime(df['start_dt'])
        df['end_dt'] = pd.to_datetime(df['end_dt'])
        df

Out [1]:
id      start_dt                  end_dt
12  4/3/2020  10:19:55 AM  4/10/2020  1:23:26 PM


In [2]: pd.date_range(start=df['start_dt'], end=df['end_dt'], freq='D')

Out [2]:

          DatetimeIndex(['2020-04-04 10:19:55', '2020-04-05 10:19:55',
           '2020-04-06 10:19:55', '2020-04-07 10:19:55',
           '2020-04-08 10:19:55', '2020-04-09 10:19:55',
           '2020-04-10 10:19:55'],
          dtype='datetime64[ns]', freq='D')

date_range将我的数据划分为几天,但不会在一天中的最晚(11:59:59 PM)进行划分。

所需输出:

id       start_dt                  end_dt
12  4/3/2020  10:19:55 AM   4/3/2020  11:59:59 PM
12  4/4/2020  12:00:00 AM   4/4/2020  11:59:59 PM
12  4/5/2020  12:00:00 AM   4/5/2020  11:59:59 PM
12  4/6/2020  12:00:00 AM   4/6/2020  11:59:59 PM
12  4/7/2020  12:00:00 AM   4/7/2020  11:59:59 PM
12  4/8/2020  12:00:00 AM   4/8/2020  11:59:59 PM
12  4/9/2020  12:00:00 AM   4/9/2020  11:59:59 PM
12  4/10/2020  12:00:00 AM  4/10/2020  1:23:26 PM
评论