Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如果时间在给定的日期范围内通过某个检查点,则计算天数,例如在酒店的停留次数_Python_Pandas - Fatal编程技术网

Python 如果时间在给定的日期范围内通过某个检查点,则计算天数,例如在酒店的停留次数

Python 如果时间在给定的日期范围内通过某个检查点,则计算天数,例如在酒店的停留次数,python,pandas,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

如果时间在给定的日期范围内通过某个检查点,我想计算天数。例如,检查点是每午夜一次。我的入住和退房时间戳如下:

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

你能给我一些建议吗?提前谢谢。

您可以像这样减去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)

您可以像这样减去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)

你就快到了,它将是df['answer']=df['chkout'].dt.day-df['chkin'].dt.dayomg。。。非常感谢。你的最后一个例子是错误的。下一个da的上午11点到凌晨4点是1,如果你不包括小时分和秒,你的答案将更准确。你就快到了,它将是df['answer]=df['chkout'].dt.day-df['chkin'].dt.dayomg。。。非常感谢。你的最后一个例子是错误的。下一次da的上午11点到凌晨4点是1,如果不包括时分和秒,您的答案将更准确。