如果时间超过给定日期范围内的某个检查点,则计算天数,例如入住酒店的人数

时间:2017-10-03 19:22:09

标签: python pandas

如果时间超过某个特定日期范围内的某个检查点,我想计算一天的天数。例如,检查站是每个午夜。我有办理登机手续和退房时间戳如下:

chkin = ['2015-01-01 23:00:00',
           '2015-01-01 22:30:30',
           '2015-01-01 01:30:30',
           '2015-01-01 11:20:45']
chkout = ['2015-01-01 23:45:05',
           '2015-01-02 01:10:10',
           '2015-01-01 12:00:00',
           '2015-01-03 04:30:45']
df = pd.DataFrame({'chkin':chkin,'chkout':chkout})

我的数据框和预期答案应该是这样的。

chkin                  chkout                expected answer
2015-01-01 23:00:00    2015-01-01 23:45:05   0 (because time has not yet passed the checkpoint)
2015-01-01 22:30:30    2015-01-02 01:10:10   1 (because time has passed the checkpoint for 1 time)
2015-01-01 01:30:30    2015-01-01 12:00:00   0 (because time has not yet passed the checkpoint)
2015-01-01 11:20:45    2015-01-02 04:30:45   2 (because time has passed the checkpoint for 2 times)

我试图使用以下命令得到答案但尚未成功。

df['answer'] = (df['chkout'] - df['chkin']).dt.days

我可以提出你的建议吗?提前谢谢。

1 个答案:

答案 0 :(得分:0)

你可以这样减去datetime对象:

import datetime
format = '%Y-%m-%d %H:%M:%S'
difference = abs((datetime.datetime.strptime(df['chkout'], format) - datetime.datetime.strptime(df['chkin'], format)).days)
print(difference)

虽然如果你不包括小时分钟和秒钟,你会得到更准确的答案,例如:运行你的第一个例子结账并通过上面的代码检查时产生1而不是0.你可以解决这个问题。只使用日子,而不是像这样的时间:

from datetime import datetime as dt
format = '%Y-%m-%d'
difference = abs((dt.strptime(df['chkout'].split(' ')[0], format) - dt.strptime(df['chkin'].split(' ')[0], format)).days)
print(difference)