我有一个存储睡眠数据(常规睡眠和小睡)的数据框。以下是数据框的一些列:
我正在尝试一种方法来总结每天的睡眠时间。查看“开始时间”并总结同一日期的时间会很容易,但是在有人午间小睡但凌晨1点睡觉的情况下,这将非常棘手(因为考虑第二天)。
如果可以的话(例如,在9/25的5:00-7:00pm午睡,从2:00-7的睡眠时间),我理想地考虑将第二天的凌晨在同一天睡觉。 9 / 26,00am。那么9/25的总睡眠时间是7个小时。
有关如何使其正常工作的任何提示/建议?
这是我的主意(我仍然不精通Python,所以我只是在这里和那里放置伪代码):
# Create a datetime
next_day = datetime.time(4, 0, 0)
# Create an iterator
bedtimes = iter(df['Start time'])
for i in bedtimes:
# Get current and next sleep datetime
cur = df['Start time'][i]
next = df['Start time'][i+1]
# If the next datetime in the iterator is a day after AND before
# 4:00am, add the times of 'cur' and 'next' together
# What if there is a scenario where the user takes 2 one hour naps during
# the day? What is a good way to catch that scenario as well? Use a while
# loop?
if (cur.date() < next.date()) & (next.time() < next_day):
total = cur.time() + next.time()
# Advance to the next iteration
next(bedtimes, None)
# Somehow in here, I would want the loop to skip an iteration if we
# summed 'cur' and 'next' together (since they are considered sleep
# from the same day. I am interested in calculating the amount of
# sleep PER day)
如果错误,请更正我的语法!以上只是一个想法。如果有更好的方法,我很想听听如何做到。
谢谢!