In [167]:
from ahh import pre, ext, vis, sci
import pandas as pd
import datetime

In [200]:
df = pre.read_csv('monthly/sleep_summary_20170301_20171028.csv', date='SLEEP DATE', time='START TIME')
df = df.drop('sleep date', axis=1).drop('start time', axis=1).drop('minutes to fall asleep', axis=1)
df.index = df.index - datetime.timedelta(hours=4) # because I sometimes sleep past 12 AM and it count as next day
df['weekday'] = df.index.weekday

dts = df.index
tib = df['time in bed'] / 60
tas = df['minutes asleep'] / 60
df.columns


Out[200]:
Index(['time in bed', 'minutes asleep', 'awakenings count', 'minutes awake',
       'minutes after wakeup', 'efficiency', 'weekday'],
      dtype='object')

In [169]:
ax = vis.plot_line(dts, tib, label='time in bed')
_ = vis.plot_line(dts, tas, figsize='na', color='blue', label='time asleep')

ax.set_ylim(3, 12)
_ = vis.set_labels(ax, ylabel='Hours', title='March to October 2017', title_pad=1.12)
vis.set_legend(ax, ncol=3, loc='top center')


Out[169]:
(3, 12)
Out[169]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fa76d22ceb8>

In [225]:
df_gb = df.groupby([df.index.month, 'weekday']).mean()
df_gb = df_gb.reset_index()
df_piv = df_gb.pivot(index='weekday', columns='level_0', values='minutes asleep') / 60
df_piv.index = ext.MISC['weekdays_short']
df_piv.columns = ext.MISC['months_short'][2:10]
vis.plot_heatmap(df_piv, data_lim=(6, 9), ylabel='Night of', xlabel='Month', cbar_label='Hours Asleep')


Out[225]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fa7677a40b8>

In [246]:
df_gb = df.groupby([df.index.month, 'weekday']).mean()
df_gb = df_gb.reset_index()
df_piv = df_gb.pivot(index='weekday', columns='level_0', values='minutes awake')
df_piv.index = ext.MISC['weekdays_short']
df_piv.columns = ext.MISC['months_short'][2:10]
vis.plot_heatmap(df_piv, data_lim=(50, 90), interval=5, ylabel='Night of', xlabel='Month', cbar_label='Minutes Awake')


Out[246]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fa7667c4438>

In [236]:
df


Out[236]:
time in bed minutes asleep awakenings count minutes awake minutes after wakeup efficiency weekday
2017-03-01 18:36:00 583 532 26 51 0 91 2
2017-03-02 19:02:00 502 475 12 27 0 95 3
2017-03-03 19:58:00 494 438 28 55 1 89 4
2017-03-03 20:23:00 508 456 27 52 0 90 4
2017-03-04 20:03:00 547 523 12 24 0 96 5
2017-03-06 18:53:00 577 539 16 38 0 93 0
2017-03-07 19:04:00 502 477 15 25 0 95 1
2017-03-08 19:28:00 546 523 12 23 0 96 2
2017-03-09 18:44:00 522 491 15 31 0 94 3
2017-03-10 19:06:00 604 528 39 72 4 88 4
2017-03-10 20:26:00 478 451 14 27 0 94 4
2017-03-11 20:37:00 584 522 37 62 0 89 5
2017-03-12 20:37:00 584 522 37 62 0 89 6
2017-03-14 18:56:00 511 476 26 35 0 93 1
2017-03-15 19:29:00 525 470 31 55 0 90 2
2017-03-16 18:58:00 502 489 9 13 0 97 3
2017-03-16 20:42:00 480 427 32 52 1 89 3
2017-03-17 20:32:00 476 458 9 18 0 96 4
2017-03-19 19:40:00 322 287 15 34 1 89 6
2017-03-20 19:31:00 466 422 29 44 0 91 0
2017-03-21 19:46:00 373 357 10 15 1 96 1
2017-03-22 19:12:00 528 508 12 19 1 96 2
2017-03-23 19:45:00 538 470 36 68 0 87 3
2017-03-24 19:14:00 653 610 21 43 0 93 4
2017-03-24 20:27:00 485 469 9 16 0 97 4
2017-03-26 19:01:00 551 530 10 21 0 96 6
2017-03-27 19:08:00 552 466 0 86 0 84 0
2017-03-27 20:01:00 443 382 0 61 0 86 0
2017-03-29 19:35:00 530 457 0 73 0 86 2
2017-03-30 19:18:00 491 410 0 81 0 84 3
... ... ... ... ... ... ... ...
2017-09-29 19:28:00 537 472 0 65 0 88 4
2017-09-30 19:28:00 537 472 0 65 0 88 5
2017-10-01 18:42:00 634 544 0 90 0 86 6
2017-10-02 19:05:00 514 463 0 51 0 90 0
2017-10-03 19:12:00 488 411 0 77 0 84 1
2017-10-04 19:55:00 473 402 0 71 0 85 2
2017-10-05 19:05:00 586 527 0 59 0 90 3
2017-10-06 19:23:00 462 413 0 49 0 89 4
2017-10-07 19:49:00 491 407 0 84 0 83 5
2017-10-07 20:42:00 446 381 0 65 0 85 5
2017-10-09 19:47:00 490 427 0 63 0 87 0
2017-10-10 19:29:00 561 470 0 91 0 84 1
2017-10-11 19:01:00 536 458 0 78 0 85 2
2017-10-11 20:13:00 524 444 0 80 0 85 2
2017-10-12 21:02:00 443 380 0 63 0 86 3
2017-10-14 19:21:00 510 435 0 75 0 85 5
2017-10-15 19:34:00 553 500 0 53 0 90 6
2017-10-15 20:01:00 467 404 0 63 0 87 6
2017-10-17 19:41:00 557 492 0 65 0 88 1
2017-10-18 19:22:00 501 432 0 69 0 86 2
2017-10-19 19:39:00 522 452 0 70 0 87 3
2017-10-20 19:37:00 482 412 0 70 0 85 4
2017-10-20 21:58:00 399 347 0 52 0 87 4
2017-10-21 21:58:00 399 347 0 52 0 87 5
2017-10-23 19:14:00 521 459 29 60 2 88 0
2017-10-24 19:13:00 347 302 23 45 0 87 1
2017-10-25 19:24:00 488 470 12 16 2 97 2
2017-10-25 20:55:00 500 434 31 66 0 87 2
2017-10-27 19:43:00 535 461 0 74 0 86 4
2017-10-28 19:24:00 530 461 0 69 0 87 5

242 rows × 7 columns


In [ ]: