By the end of August, 2016, it appears that the number of homicides for 2016 will have surpassed the number of homicides for all of 2015.
This is a comparison of the homicides, broken down by age of victim, geography and some temporal aspects, through August 31, comparing 2015 to 2016.
In [34]:
# Load some data
import os
import requests
def get_table_url(table_name, base_url=os.environ['NEWSROOMDB_URL']):
return '{}table/json/{}'.format(os.environ['NEWSROOMDB_URL'], table_name)
def get_table_data(table_name):
url = get_table_url(table_name)
try:
r = requests.get(url)
return r.json()
except:
print("Request failed. Probably because the response is huge. We should fix this.")
return get_table_data(table_name)
homicides_raw = get_table_data('homicides')
In [35]:
# Pull homicides into a dataframe
import pandas as pd
homicides = pd.DataFrame(homicides_raw)
In [36]:
# Add a column that contains date and time together and is a Python datetime
from datetime import datetime
def get_datetime(row):
if not row['Occ Date']:
return None
dt_str = row['Occ Date']
fmt_str = "%Y-%m-%d"
if row['Occ Time']:
dt_str = dt_str + " " + row['Occ Time']
fmt_str += " %H:%M"
return datetime.strptime(dt_str, fmt_str)
def get_month(row):
return row['datetime'].month
homicides['datetime'] = homicides.apply(get_datetime, axis=1)
homicides['year'] = homicides.apply(lambda row: row['datetime'].year, axis=1)
homicides['month'] = homicides.apply(get_month, axis=1)
In [37]:
# Filter to homicides on or before this day
from datetime import datetime
# If we wanted to do this to the exact day of the year, which accomodates the fact that 2016 was a leap year,
# we would do it this way, however, I think it will be easier to talk to editors about it if we
# do a straight-up comparison through 8/31.
# day_of_year = datetime.now().timetuple().tm_yday
#
#def get_day_of_year(row):
# try:
# return row['datetime'].timetuple().tm_yday
# except ValueError:
# return None
#
#homicides['day_of_year'] = homicides.apply(get_day_of_year, axis=1)
#
#homicides_with_day_of_year = homicides[homicides['day_of_year'] != None]
end_date = datetime(2016, 9, 1)
def is_before_date(row):
if row['datetime'].year == 2016:
return row['datetime'] < end_date
elif row['datetime'].year == 2015:
return row['datetime'] < end_date.replace(year=2015)
else:
return False
homicides_to_day = homicides[homicides.apply(is_before_date, axis=1)]
In [38]:
# Normalize district numbers
def normalize_district_number(d):
try:
return int(d)
except ValueError:
return d
homicides_to_day['District Number'] = homicides_to_day['District Number'].apply(normalize_district_number)
/Users/ghing/venvs/public-notebooks/lib/python3.4/site-packages/ipykernel/__main__.py:9: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
In [39]:
# Get only 2015, 2016 homicides
homicides_to_day_2015 = homicides_to_day[homicides_to_day['year'] == 2015]
homicides_to_day_2016 = homicides_to_day[homicides_to_day['year'] == 2016]
In [40]:
from IPython.display import display
with pd.option_context('display.max_rows', 2000):
display(homicides_to_day_2015[['datetime', 'Age', 'District Number']].sort_values('datetime'))
datetime
Age
District Number
648
2015-01-03 01:30:00
39
15
651
2015-01-04 20:50:00
20
10
647
2015-01-05 21:44:00
56
8
646
2015-01-05 23:40:00
24
6
649
2015-01-06 15:30:00
19
4
650
2015-01-06 15:30:00
18
4
667
2015-01-06 22:20:00
1
7
652
2015-01-08 20:45:00
51
10
1090
2015-01-09 22:40:00
35
11
653
2015-01-13 17:00:00
21
9
657
2015-01-13 21:50:00
16
2
674
2015-01-14 17:20:00
24
8
654
2015-01-16 19:20:00
17
8
655
2015-01-17 12:45:00
24
10
1231
2015-01-17 18:48:00
18
22
658
2015-01-18 11:02:00
21
12
661
2015-01-20 10:15:00
26
4
659
2015-01-21 00:15:00
25
9
660
2015-01-21 09:00:00
19
8
1091
2015-01-21 18:40:00
40
9
666
2015-01-23 17:25:00
35
10
827
2015-01-23 18:15:00
18
8
662
2015-01-23 18:23:00
19
6
663
2015-01-24 02:00:00
24
11
1232
2015-01-25 12:50:00
39
10
668
2015-01-27 13:35:00
23
2
669
2015-01-29 02:00:00
24
5
1096
2015-01-29 02:00:00
38
5
1444
2015-01-29 03:00:00
20
9
664
2015-01-30 17:40:00
34
7
665
2015-01-31 11:16:00
22
4
673
2015-01-31 14:35:00
27
5
670
2015-02-01 20:00:00
23
1
671
2015-02-02 08:45:00
50
2
672
2015-02-04 15:10:00
20
8
1233
2015-02-08 12:09:00
48
22
682
2015-02-08 22:00:00
13
25
676
2015-02-09 11:10:00
33
15
678
2015-02-09 21:00:00
60
12
677
2015-02-09 22:00:00
37
25
1297
2015-02-10 22:10:00
16
25
1097
2015-02-13 23:58:00
31
6
1095
2015-02-15 03:20:00
21
7
1274
2015-02-16 16:45:00
33
7
828
2015-02-19 23:40:00
30
14
772
2015-02-20 17:45:00
25
7
1101
2015-02-21 17:05:00
28
19
771
2015-02-22 14:34:00
26
7
1099
2015-02-25 04:10:00
23
24
773
2015-02-25 15:10:00
20
11
1280
2015-02-25 16:30:00
25
22
1098
2015-02-25 19:20:00
60
5
1102
2015-03-02 19:15:00
17
4
1108
2015-03-03 13:20:00
28
4
124
2015-03-07 01:35:00
18
12
774
2015-03-07 01:35:00
22
12
1103
2015-03-07 16:09:00
40
9
1111
2015-03-08 03:05:00
79
11
775
2015-03-08 18:33:00
25
3
1106
2015-03-09 09:36:00
7 mth
10
776
2015-03-09 15:00:00
20
19
1104
2015-03-09 18:30:00
77
5
1114
2015-03-09 19:35:00
25
22
779
2015-03-15 04:00:00
19
17
1110
2015-03-15 12:52:00
16
16
777
2015-03-15 16:30:00
23
25
778
2015-03-15 16:30:00
28
25
787
2015-03-16 03:10:00
34
14
1387
2015-03-16 03:10:00
41
14
1109
2015-03-19 00:30:00
43
6
780
2015-03-19 02:08:00
30
11
1129
2015-03-19 11:10:00
24
8
781
2015-03-19 20:00:00
24
12
782
2015-03-20 16:25:00
21
4
783
2015-03-21 12:30:00
30
22
784
2015-03-21 19:10:00
23
4
1237
2015-03-22 18:35:00
21
20
1112
2015-03-24 06:30:00
27
12
1123
2015-03-25 17:35:00
25
25
785
2015-03-26 17:45:00
21
15
1113
2015-03-27 02:00:00
24
4
786
2015-03-28 01:20:00
36
1
1124
2015-03-28 09:15:00
27
6
788
2015-03-29 02:00:00
25
5
1234
2015-03-30 13:55:00
24
7
1115
2015-03-31 10:10:00
16
7
1238
2015-03-31 12:10:00
52
14
1120
2015-04-01 18:25:00
16
8
1117
2015-04-01 23:55:00
28
3
1116
2015-04-02 02:05:00
36
7
1260
2015-04-02 20:30:00
37
3
1121
2015-04-03 11:30:00
22
2
789
2015-04-03 14:00:00
18
7
790
2015-04-04 12:15:00
16
7
791
2015-04-04 16:14:00
42
6
1248
2015-04-07 14:45:00
28
19
792
2015-04-08 02:55:00
24
14
1118
2015-04-08 11:15:00
21
4
1119
2015-04-08 14:25:00
56
25
1126
2015-04-10 22:35:00
39
22
1122
2015-04-11 20:40:00
22
3
793
2015-04-15 01:50:00
24
19
1855
2015-04-15 17:45:00
58
14
1328
2015-04-17 22:50:00
36
11
1125
2015-04-18 01:05:00
50
12
794
2015-04-19 05:20:00
43
10
1236
2015-04-19 06:35:00
34
25
1235
2015-04-19 06:35:00
30
25
1127
2015-04-20 14:43:00
18
3
1128
2015-04-20 22:15:00
21
22
1133
2015-04-21 02:20:00
26
2
795
2015-04-21 13:00:00
21
10
1134
2015-04-21 21:50:00
28
3
1130
2015-04-23 18:00:00
18
3
1131
2015-04-24 10:00:00
26
8
796
2015-04-24 23:00:00
19
6
797
2015-04-25 01:05:00
18
9
1239
2015-04-26 12:40:00
20
11
1240
2015-04-28 15:00:00
30
5
1388
2015-04-29 12:05:00
25
12
1247
2015-04-29 14:00:00
24
9
1241
2015-04-30 03:44:00
27
15
1242
2015-05-01 00:45:00
27
6
831
2015-05-01 17:00:00
30
11
832
2015-05-01 17:00:00
29
11
829
2015-05-01 17:20:00
24
4
830
2015-05-01 22:10:00
26
6
833
2015-05-03 00:40:00
26
8
1243
2015-05-05 01:19:00
26
4
1244
2015-05-05 17:00:00
53
24
1246
2015-05-07 12:15:00
5
4
1577
2015-05-07 19:45:00
28
7
834
2015-05-09 13:00:00
22
7
1245
2015-05-09 23:58:00
23
3
1249
2015-05-10 10:40:00
38
15
1251
2015-05-13 20:05:00
20
5
1252
2015-05-13 20:05:00
19
5
1308
2015-05-13 20:05:00
19
5
1258
2015-05-13 23:10:00
19
14
1253
2015-05-14 19:20:00
19
7
835
2015-05-15 23:58:00
24
12
1305
2015-05-18 00:30:00
28
4
836
2015-05-18 12:15:00
26
8
842
2015-05-20 06:15:00
61
17
843
2015-05-20 11:45:00
23
8
1257
2015-05-20 20:10:00
54
8
1256
2015-05-21 11:40:00
33
4
1261
2015-05-21 23:25:00
22
9
837
2015-05-23 01:45:00
34
14
1272
2015-05-23 02:15:00
20
12
838
2015-05-23 12:45:00
24
15
1337
2015-05-23 23:45:00
27
17
1273
2015-05-24 00:27:00
32
4
1262
2015-05-24 00:48:00
29
7
839
2015-05-24 15:30:00
38
11
1352
2015-05-24 16:00:00
28
14
1286
2015-05-24 20:15:00
15
2
1264
2015-05-25 14:30:00
29
17
1263
2015-05-25 17:30:00
19
2
844
2015-05-25 19:50:00
17
15
1271
2015-05-28 14:50:00
49
8
1270
2015-05-28 14:50:00
22
8
1266
2015-05-29 01:36:00
19
20
1268
2015-05-29 23:58:00
24
10
841
2015-05-30 00:45:00
43
12
840
2015-05-30 01:25:00
19
3
1269
2015-05-30 02:00:00
25
19
1363
2015-05-31 05:25:00
18
11
1281
2015-05-31 12:15:00
25
6
847
2015-05-31 19:10:00
53
15
1275
2015-05-31 19:13:00
27
15
1295
2015-06-01 13:10:00
22
24
855
2015-06-01 14:01:00
28
11
1306
2015-06-01 14:30:00
23
11
1276
2015-06-03 16:50:00
45
24
1282
2015-06-04 16:59:00
28
6
845
2015-06-04 21:45:00
17
4
1277
2015-06-05 18:01:00
34
5
1278
2015-06-05 21:55:00
27
10
846
2015-06-06 02:13:00
17
9
1607
2015-06-07 03:50:00
19
25
1608
2015-06-07 03:50:00
19
25
1307
2015-06-08 19:00:00
35
22
848
2015-06-10 00:45:00
20
20
1598
2015-06-10 12:55:00
22
8
849
2015-06-10 20:35:00
17
9
1283
2015-06-12 00:45:00
23
16
1284
2015-06-12 01:00:00
38
2
1285
2015-06-12 11:02:00
55
15
850
2015-06-13 02:50:00
23
7
1288
2015-06-13 21:55:00
55
9
1291
2015-06-14 00:30:00
9
1359
2015-06-14 18:20:00
31
15
1289
2015-06-14 21:45:00
49
19
1292
2015-06-16 01:05:00
49
6
1301
2015-06-16 17:40:00
15
10
1290
2015-06-16 22:01:00
29
11
1298
2015-06-17 22:02:00
15
10
1293
2015-06-17 23:50:00
19
19
1294
2015-06-18 15:10:00
30
6
852
2015-06-18 21:50:00
43
3
851
2015-06-18 21:55:00
23
6
1296
2015-06-20 12:02:00
21
4
1303
2015-06-20 23:20:00
18
25
1409
2015-06-21 00:00:00
28
1318
2015-06-21 10:00:00
27
15
1299
2015-06-21 23:15:00
23
7
853
2015-06-23 14:06:00
21
6
1304
2015-06-23 20:20:00
52
2
1300
2015-06-23 23:35:00
34
7
854
2015-06-25 00:05:00
21
10
1302
2015-06-25 16:45:00
21
20
1310
2015-06-27 02:25:00
21
9
1309
2015-06-27 03:40:00
32
9
1319
2015-06-27 19:00:00
21
3
856
2015-06-28 14:10:00
21
5
1311
2015-06-29 03:03:00
26
14
1314
2015-06-29 15:30:00
20
19
1313
2015-06-30 02:30:00
49
11
857
2015-06-30 22:45:00
22
22
858
2015-07-01 21:50:00
50
10
1315
2015-07-02 11:10:00
22
5
859
2015-07-02 18:25:00
20
10
1316
2015-07-03 00:05:00
26
9
860
2015-07-03 01:20:00
47
22
1317
2015-07-03 16:45:00
17
2
1348
2015-07-04 20:25:00
28
6
1350
2015-07-04 23:55:00
7
11
861
2015-07-05 00:55:00
28
4
1414
2015-07-05 02:45:00
23
17
1326
2015-07-05 05:00:00
27
11
862
2015-07-05 06:10:00
25
22
863
2015-07-05 06:10:00
31
22
1320
2015-07-05 16:00:00
48
4
871
2015-07-06 17:30:00
16
4
1338
2015-07-07 09:40:00
21
4
864
2015-07-08 02:10:00
25
11
1325
2015-07-08 13:15:00
36
6
1375
2015-07-09 20:45:00
22
9
1321
2015-07-09 22:05:00
17
3
1401
2015-07-10 17:00:00
40
12
1339
2015-07-10 23:10:00
25
6
1322
2015-07-11 02:15:00
23
7
1340
2015-07-11 03:15:00
26
15
1324
2015-07-11 13:40:00
22
4
1342
2015-07-11 14:00:00
1
3
865
2015-07-12 17:30:00
19
4
1345
2015-07-13 04:30:00
21
10
1371
2015-07-15 17:45:00
16
25
867
2015-07-15 19:50:00
21
15
866
2015-07-15 23:58:00
23
25
868
2015-07-17 04:25:00
34
17
1331
2015-07-17 13:40:00
22
24
1329
2015-07-17 17:23:00
29
17
1330
2015-07-17 17:23:00
29
17
1334
2015-07-18 01:00:00
25
11
869
2015-07-18 04:20:00
20
1
875
2015-07-18 04:30:00
29
7
1335
2015-07-18 21:55:00
25
11
870
2015-07-20 23:00:00
36
24
1341
2015-07-21 21:39:00
26
2
1616
2015-07-23 16:45:00
59
16
872
2015-07-24 18:45:00
17
10
1346
2015-07-25 00:50:00
32
15
1343
2015-07-25 22:15:00
21
3
1344
2015-07-26 02:35:00
32
11
873
2015-07-26 02:35:00
35
11
1362
2015-07-26 09:00:00
18
4
874
2015-07-26 19:45:00
16
10
1347
2015-07-27 19:20:00
21
8
1349
2015-07-28 14:20:00
40
12
876
2015-07-30 03:40:00
21
5
1553
2015-07-30 23:40:00
35
4
1354
2015-07-31 04:50:00
23
9
1355
2015-08-01 02:45:00
21
12
877
2015-08-02 01:03:00
22
25
1353
2015-08-02 03:15:00
21
2
882
2015-08-02 05:00:00
20
5
879
2015-08-05 06:00:00
21
6
1358
2015-08-05 23:20:00
27
15
1361
2015-08-06 01:20:00
51
22
1369
2015-08-06 19:00:00
22
22
1366
2015-08-06 19:00:00
20
22
881
2015-08-07 02:45:00
31
5
1360
2015-08-07 23:10:00
17
7
1367
2015-08-09 00:15:00
18
10
880
2015-08-09 23:05:00
27
10
883
2015-08-11 22:55:00
20
5
1364
2015-08-12 14:20:00
14
11
1373
2015-08-12 21:40:00
None
25
887
2015-08-13 03:10:00
30
6
1368
2015-08-13 14:55:00
18
9
1365
2015-08-14 14:30:00
44
1
884
2015-08-14 18:30:00
19
11
1382
2015-08-15 02:00:00
27
11
889
2015-08-15 03:05:00
50
7
1380
2015-08-16 13:00:00
21
7
1434
2015-08-16 21:45:00
16
10
1433
2015-08-16 21:45:00
19
10
1370
2015-08-17 21:00:00
32
11
885
2015-08-17 21:30:00
37
11
920
2015-08-19 01:35:00
31
24
1374
2015-08-19 13:25:00
20
7
1372
2015-08-20 01:10:00
23
25
1377
2015-08-21 01:30:00
48
18
1379
2015-08-21 02:08:00
20
15
1437
2015-08-21 18:15:00
18
11
1403
2015-08-21 22:09:00
29
22
886
2015-08-22 04:53:00
34
8
1376
2015-08-22 21:30:00
35
9
888
2015-08-23 06:00:00
28
25
1381
2015-08-23 20:35:00
42
15
1378
2015-08-23 20:40:00
30
8
1392
2015-08-24 18:00:00
24
15
1384
2015-08-24 19:35:00
66
6
1416
2015-08-24 20:20:00
24
25
890
2015-08-25 18:00:00
25
2
1386
2015-08-25 22:30:00
28
5
1385
2015-08-26 10:35:00
23
11
1698
2015-08-26 10:35:00
22
11
1465
2015-08-26 19:30:00
23
9
1393
2015-08-27 21:00:00
25
3
1390
2015-08-29 04:10:00
34
11
891
2015-08-29 23:05:00
44
7
1389
2015-08-30 01:00:00
16
6
1402
2015-08-30 04:05:00
28
11
1522
2015-08-30 14:00:00
64
11
1523
2015-08-31 11:00:00
51
17
1412
2015-08-31 18:57:00
30
5
1391
2015-08-31 20:35:00
35
2
892
2015-08-31 21:35:00
25
15
In [41]:
from IPython.display import display
with pd.option_context('display.max_rows', 2000):
display(homicides_to_day_2016[['datetime', 'Age', 'District Number']].sort_values('datetime'))
datetime
Age
District Number
926
2016-01-01 02:00:00
24
2
1506
2016-01-01 06:40:00
36
9
1599
2016-01-01 21:40:00
32
7
1503
2016-01-02 16:45:00
16
3
1536
2016-01-04 08:20:00
21
15
1504
2016-01-04 19:35:00
33
12
1531
2016-01-05 15:35:00
16
4
1530
2016-01-05 15:35:00
17
4
927
2016-01-05 20:20:00
25
9
1505
2016-01-05 20:20:00
23
9
1507
2016-01-06 23:30:00
31
6
1508
2016-01-07 22:25:00
17
9
1510
2016-01-08 19:30:00
25
19
941
2016-01-08 20:45:00
29
10
931
2016-01-08 21:00:00
21
8
928
2016-01-08 21:45:00
37
25
929
2016-01-09 14:00:00
19
9
1511
2016-01-09 20:00:00
15
22
1512
2016-01-09 20:30:00
17
22
930
2016-01-11 21:20:00
21
15
1521
2016-01-13 09:30:00
18
11
1514
2016-01-13 13:30:00
25
4
1515
2016-01-13 14:50:00
25
6
1557
2016-01-14 17:25:00
17
11
933
2016-01-14 18:20:00
19
10
1516
2016-01-16 10:25:00
32
22
935
2016-01-16 11:50:00
27
8
934
2016-01-16 20:15:00
29
17
1517
2016-01-17 18:00:00
39
25
1518
2016-01-18 23:45:00
21
24
1528
2016-01-20 19:16:00
24
7
1526
2016-01-21 19:20:00
23
11
1558
2016-01-21 22:10:00
20
25
1519
2016-01-21 22:10:00
19
9
936
2016-01-23 02:20:00
25
1
937
2016-01-23 07:10:00
22
11
143
2016-01-23 22:45:00
26
5
1527
2016-01-23 23:45:00
61
24
1525
2016-01-24 09:20:00
26
11
939
2016-01-25 09:18:00
20
7
1520
2016-01-25 11:25:00
19
7
1524
2016-01-25 16:40:00
39
5
938
2016-01-25 17:00:00
19
10
1559
2016-01-25 20:25:00
25
22
940
2016-01-26 12:01:00
76
5
1560
2016-01-26 16:00:00
33
15
1561
2016-01-26 18:00:00
62
12
942
2016-01-27 14:00:00
71
17
1562
2016-01-28 05:00:00
36
1
943
2016-01-29 18:20:00
20
25
1563
2016-01-30 01:05:00
19
7
1534
2016-01-30 02:55:00
26
11
1535
2016-01-30 22:45:00
34
11
1564
2016-01-31 04:40:00
29
7
1621
2016-01-31 05:15:00
21
17
1532
2016-01-31 05:15:00
30
3
1529
2016-01-31 05:15:00
16
17
1533
2016-02-01 00:30:00
42
11
1565
2016-02-01 10:15:00
31
22
1542
2016-02-02 17:30:00
10
8
1543
2016-02-02 17:30:00
38
8
1544
2016-02-02 17:30:00
32
8
1540
2016-02-02 17:30:00
62
8
1539
2016-02-02 17:30:00
58
8
1541
2016-02-02 17:30:00
13
8
944
2016-02-02 19:40:00
34
11
1537
2016-02-02 22:30:00
21
4
1538
2016-02-03 08:05:00
16
15
945
2016-02-04 08:32:00
34
10
1546
2016-02-04 11:04:00
55
19
948
2016-02-04 17:15:00
18
25
947
2016-02-04 19:40:00
18
25
1566
2016-02-05 19:25:00
25
12
1600
2016-02-05 23:30:00
21
3
1547
2016-02-07 01:15:00
22
6
946
2016-02-07 12:45:00
21
11
1545
2016-02-08 09:50:00
35
7
146
2016-02-11 17:20:00
37
11
950
2016-02-13 02:07:00
23
8
1548
2016-02-13 03:50:00
24
9
951
2016-02-13 05:25:00
27
15
1864
2016-02-13 23:15:00
39
25
952
2016-02-14 01:00:00
22
22
953
2016-02-14 10:50:00
26
2
145
2016-02-14 10:50:00
25
2
954
2016-02-16 09:00:00
21
25
147
2016-02-16 18:50:00
28
12
955
2016-02-17 20:00:00
20
6
1549
2016-02-18 08:40:00
28
15
1550
2016-02-19 23:20:00
20
6
1685
2016-02-20 15:00:00
18
2
1552
2016-02-20 18:00:00
35
9
1620
2016-02-20 22:05:00
33
6
956
2016-02-21 02:30:00
25
6
1554
2016-02-22 07:55:00
58
19
957
2016-02-22 09:45:00
28
7
961
2016-02-22 13:30:00
50
12
1555
2016-02-24 07:35:00
39
10
958
2016-02-24 18:12:00
16
15
960
2016-02-24 18:24:00
23
11
1556
2016-02-26 21:45:00
30
9
959
2016-02-27 15:45:00
26
6
1609
2016-03-01 17:47:00
25
11
962
2016-03-01 18:15:00
20
9
964
2016-03-02 13:55:00
22
15
965
2016-03-02 16:30:00
31
11
1567
2016-03-02 19:05:00
45
12
1569
2016-03-04 01:15:00
22
25
1568
2016-03-04 21:30:00
44
2
963
2016-03-07 11:05:00
23
3
968
2016-03-08 18:40:00
42
22
1585
2016-03-08 19:50:00
31
8
1605
2016-03-09 00:10:00
21
12
1570
2016-03-09 18:35:00
19
14
1571
2016-03-09 20:40:00
27
1
149
2016-03-10 04:50:00
37
11
1579
2016-03-10 16:53:00
27
4
966
2016-03-11 09:20:00
26
7
1580
2016-03-11 14:40:00
23
8
1573
2016-03-11 17:45:00
20
15
150
2016-03-11 20:10:00
24
11
1572
2016-03-12 03:29:00
22
17
973
2016-03-13 06:40:00
18
11
967
2016-03-13 19:50:00
16
5
1586
2016-03-14 21:45:00
29
11
1587
2016-03-15 19:30:00
23
7
1575
2016-03-15 22:40:00
25
8
1697
2016-03-15 23:05:00
23
3
1576
2016-03-15 23:30:00
31
7
1581
2016-03-17 19:10:00
16
11
974
2016-03-18 00:00:00
23
5
1588
2016-03-18 13:50:00
42
11
1578
2016-03-18 17:51:00
24
25
1660
2016-03-20 05:00:00
27
9
1582
2016-03-20 14:45:00
23
17
153
2016-03-21 20:50:00
39
7
151
2016-03-22 06:15:00
34
9
976
2016-03-22 11:00:00
24
11
969
2016-03-23 00:25:00
19
9
1589
2016-03-23 00:30:00
35
11
154
2016-03-23 16:25:00
18
24
971
2016-03-25 01:40:00
29
6
152
2016-03-26 14:10:00
27
7
970
2016-03-29 09:26:00
19
25
972
2016-03-30 01:45:00
31
3
1584
2016-03-30 17:09:00
22
9
975
2016-03-30 18:00:00
25
14
1583
2016-03-31 01:45:00
32
14
1751
2016-03-31 16:04:00
43
11
1750
2016-03-31 16:04:00
23
11
1696
2016-03-31 22:30:00
28
20
1613
2016-04-01 17:00:00
56
17
1590
2016-04-02 23:45:00
51
17
1591
2016-04-04 13:15:00
17
16
155
2016-04-05 19:35:00
35
4
977
2016-04-06 01:45:00
32
15
160
2016-04-06 11:30:00
39
11
1617
2016-04-07 02:10:00
30
6
1594
2016-04-07 15:30:00
18
11
1593
2016-04-07 15:30:00
23
11
1592
2016-04-07 23:50:00
23
11
1597
2016-04-08 01:05:00
65
25
1614
2016-04-08 19:03:00
23
8
156
2016-04-10 04:50:00
28
5
1595
2016-04-10 11:20:00
18
8
978
2016-04-11 19:30:00
37
10
1618
2016-04-11 19:30:00
19
8
1596
2016-04-11 19:40:00
16
11
1601
2016-04-15 00:10:00
25
8
1604
2016-04-15 23:20:00
32
7
1603
2016-04-15 23:20:00
34
7
1602
2016-04-17 01:25:00
33
10
1611
2016-04-19 02:20:00
23
6
157
2016-04-19 02:35:00
37
6
1610
2016-04-19 23:10:00
18
9
1612
2016-04-21 20:15:00
53
25
1628
2016-04-21 22:45:00
24
4
1645
2016-04-22 01:30:00
42
9
979
2016-04-23 01:14:00
22
10
158
2016-04-23 21:00:00
23
5
980
2016-04-24 00:25:00
24
10
1615
2016-04-24 06:23:00
25
25
1619
2016-04-24 23:10:00
27
7
1637
2016-04-24 23:10:00
38
7
159
2016-04-25 17:00:00
16
7
981
2016-04-26 06:00:00
27
22
1635
2016-04-26 16:35:00
24
9
1634
2016-04-29 20:00:00
15
7
161
2016-04-30 02:05:00
30
10
1624
2016-04-30 04:35:00
19
10
1622
2016-04-30 13:20:00
36
2
1623
2016-04-30 22:45:00
42
8
1632
2016-05-02 00:55:00
21
20
1737
2016-05-02 18:45:00
13
9
168
2016-05-02 21:35:00
16
7
1633
2016-05-02 22:30:00
28
25
1625
2016-05-03 13:50:00
29
15
1626
2016-05-03 20:30:00
25
2
162
2016-05-03 21:10:00
21
8
1627
2016-05-03 23:40:00
43
12
163
2016-05-05 19:50:00
46
6
1636
2016-05-06 00:20:00
25
6
1630
2016-05-06 10:30:00
16
11
1629
2016-05-06 20:10:00
29
6
169
2016-05-07 01:41:00
30
7
1631
2016-05-07 20:08:00
58
8
185
2016-05-07 22:50:00
27
15
164
2016-05-08 05:10:00
35
19
167
2016-05-08 08:45:00
21
6
166
2016-05-08 12:55:00
20
11
165
2016-05-08 15:20:00
16
11
1695
2016-05-09 00:30:00
51
2
1684
2016-05-09 10:30:00
26
8
1672
2016-05-09 11:50:00
26
11
1639
2016-05-09 11:50:00
35
11
1638
2016-05-09 18:45:00
18
6
54
2016-05-10 17:50:00
20
6
1694
2016-05-11 00:10:00
56
3
172
2016-05-11 23:20:00
50
7
171
2016-05-11 23:20:00
26
7
170
2016-05-11 23:20:00
26
7
173
2016-05-12 00:40:00
22
5
55
2016-05-13 11:55:00
47
2
1671
2016-05-13 23:45:00
21
12
176
2016-05-14 15:42:00
20
15
175
2016-05-14 15:45:00
32
15
1683
2016-05-14 19:30:00
15
3
1661
2016-05-15 00:00:00
36
14
174
2016-05-15 04:55:00
23
10
1693
2016-05-15 07:15:00
35
15
45
2016-05-15 14:33:00
35
15
46
2016-05-15 21:00:00
17
10
177
2016-05-16 20:35:00
37
22
1643
2016-05-16 22:50:00
28
17
1670
2016-05-16 23:10:00
24
12
1644
2016-05-17 21:30:00
38
25
178
2016-05-18 17:30:00
22
15
180
2016-05-18 21:35:00
28
15
47
2016-05-19 01:12:00
16
3
1640
2016-05-20 05:30:00
24
3
1703
2016-05-20 15:58:00
49
2
1641
2016-05-21 00:20:00
30
2
179
2016-05-21 03:00:00
35
6
1747
2016-05-21 16:36:00
21
10
1669
2016-05-21 19:30:00
26
7
1642
2016-05-22 16:00:00
20
3
1668
2016-05-24 20:10:00
18
2
1646
2016-05-26 15:10:00
38
7
51
2016-05-27 00:15:00
22
7
48
2016-05-27 07:30:00
22
2
56
2016-05-27 14:30:00
39
7
1648
2016-05-27 23:20:00
25
8
1649
2016-05-28 01:30:00
15
19
1654
2016-05-28 05:15:00
25
31
1647
2016-05-28 17:20:00
27
9
1651
2016-05-29 01:30:00
32
18
49
2016-05-30 23:00:00
39
24
50
2016-05-30 23:00:00
44
2
1748
2016-05-31 03:55:00
84
22
1656
2016-05-31 22:15:00
15
4
1650
2016-06-01 08:45:00
16
8
1653
2016-06-01 17:05:00
17
3
1657
2016-06-01 21:00:00
19
24
59
2016-06-02 22:50:00
35
11
1652
2016-06-03 11:35:00
16
9
52
2016-06-03 18:45:00
20
14
1686
2016-06-04 00:00:00
2 mo.
8
8
2016-06-04 00:00:00
32
7
58
2016-06-04 02:50:00
35
15
53
2016-06-04 03:15:00
38
22
1655
2016-06-04 18:02:00
21
6
57
2016-06-05 00:50:00
21
6
1689
2016-06-05 06:05:00
27
17
1658
2016-06-05 23:30:00
43
6
1659
2016-06-06 04:00:00
27
1
1821
2016-06-06 07:55:00
52
5
9
2016-06-08 13:40:00
34
11
60
2016-06-08 20:08:00
18
7
183
2016-06-09 17:30:00
62
8
1690
2016-06-09 19:45:00
36
9
11
2016-06-10 20:45:00
26
2
1664
2016-06-10 21:30:00
27
4
10
2016-06-11 01:30:00
18
6
1662
2016-06-11 22:20:00
17
9
1692
2016-06-12 01:00:00
33
22
184
2016-06-12 01:00:00
42
22
1663
2016-06-12 08:35:00
32
7
1665
2016-06-13 11:45:00
18
7
1666
2016-06-13 21:30:00
29
15
1682
2016-06-13 22:45:00
21
7
1667
2016-06-14 03:40:00
35
15
182
2016-06-14 16:00:00
17
22
181
2016-06-14 19:00:00
22
9
1673
2016-06-15 20:45:00
28
10
1674
2016-06-16 12:30:00
25
15
1675
2016-06-16 16:05:00
20
4
1691
2016-06-16 21:15:00
31
7
1687
2016-06-16 22:15:00
45
9
1679
2016-06-17 21:06:00
16
7
1678
2016-06-17 22:30:00
22
10
1677
2016-06-18 01:30:00
19
11
1701
2016-06-18 03:40:00
24
12
1676
2016-06-18 06:30:00
23
15
982
2016-06-18 09:49:00
65
2
1700
2016-06-18 18:05:00
23
3
61
2016-06-18 20:20:00
33
6
1680
2016-06-19 13:10:00
21
9
1681
2016-06-19 18:50:00
21
7
1713
2016-06-19 22:40:00
21
1
1699
2016-06-20 00:48:00
34
7
1714
2016-06-20 01:30:00
22
3
1688
2016-06-22 00:30:00
35
22
1709
2016-06-23 12:38:00
25
9
1702
2016-06-23 22:00:00
16
11
1715
2016-06-23 22:30:00
29
7
1711
2016-06-24 16:30:00
25
9
984
2016-06-24 21:00:00
23
11
1710
2016-06-24 22:00:00
26
6
1708
2016-06-25 18:50:00
67
7
1712
2016-06-25 19:15:00
20
8
1704
2016-06-26 01:30:00
55
11
1706
2016-06-26 04:27:00
30
3
1705
2016-06-26 05:10:00
41
15
983
2016-06-27 14:05:00
20
3
1755
2016-06-27 14:40:00
60
5
1717
2016-06-27 21:25:00
24
10
1707
2016-06-27 23:25:00
44
7
1716
2016-06-28 00:55:00
30
9
1718
2016-06-29 02:05:00
18
8
985
2016-06-29 12:20:00
40
6
1725
2016-06-29 20:09:00
27
18
1723
2016-06-30 21:45:00
37
22
1722
2016-07-01 00:03:00
27
6
1719
2016-07-01 06:20:00
34
7
1721
2016-07-02 03:20:00
32
12
1720
2016-07-02 05:00:00
33
22
1724
2016-07-03 22:50:00
24
10
1790
2016-07-04 01:50:00
30
10
1789
2016-07-04 02:10:00
23
8
1727
2016-07-04 21:20:00
40
3
1728
2016-07-05 01:35:00
34
11
1726
2016-07-05 13:05:00
25
4
1736
2016-07-05 17:00:00
30
6
1729
2016-07-06 03:44:00
18
10
1730
2016-07-06 22:35:00
24
5
1731
2016-07-08 21:30:00
22
5
1732
2016-07-08 23:40:00
59
15
1733
2016-07-09 22:45:00
21
7
1734
2016-07-10 22:15:00
33
5
1735
2016-07-12 06:10:00
34
2
1753
2016-07-12 11:40:00
17
22
1739
2016-07-12 22:50:00
28
7
986
2016-07-12 22:50:00
34
7
1744
2016-07-13 15:40:00
21
8
1741
2016-07-13 20:30:00
32
5
1740
2016-07-14 00:40:00
45
3
988
2016-07-14 01:59:00
25
11
1759
2016-07-15 15:00:00
19
15
1758
2016-07-16 09:27:00
18
11
1754
2016-07-16 20:45:00
19
15
1752
2016-07-16 23:00:00
23
2
1756
2016-07-17 00:50:00
15
2
1757
2016-07-17 04:45:00
38
11
1743
2016-07-17 11:25:00
18
7
1742
2016-07-17 19:40:00
20
15
1749
2016-07-17 23:50:00
18
15
1766
2016-07-18 20:55:00
20
9
1746
2016-07-18 21:25:00
17
11
1791
2016-07-18 23:45:00
24
7
1774
2016-07-18 23:48:00
24
7
1827
2016-07-19 18:40:00
38
3
1792
2016-07-20 00:24:00
26
24
1771
2016-07-20 00:45:00
37
1793
2016-07-21 19:54:00
2
1760
2016-07-21 20:15:00
29
1
1768
2016-07-21 21:15:00
29
3
1795
2016-07-22 00:00:00
26
11
1764
2016-07-22 16:30:00
18
7
1761
2016-07-22 18:15:00
20
5
1762
2016-07-23 13:30:00
25
5
1763
2016-07-24 02:10:00
20
8
1794
2016-07-24 09:09:00
2 mo.
15
1765
2016-07-24 15:30:00
19
9
1776
2016-07-25 13:30:00
26
11
1773
2016-07-25 17:25:00
43
3
1767
2016-07-26 11:10:00
18
15
1769
2016-07-27 00:05:00
23
4
1772
2016-07-27 13:30:00
32
7
1775
2016-07-27 14:30:00
19
11
1788
2016-07-28 00:18:00
34
9
1770
2016-07-28 00:18:00
25
9
1777
2016-07-28 19:30:00
18
3
1781
2016-07-28 22:15:00
23
6
987
2016-07-28 22:40:00
17
4
1784
2016-07-29 06:00:00
41
7
1785
2016-07-29 14:45:00
49
25
1778
2016-07-29 17:59:00
17
6
1779
2016-07-29 19:18:00
21
8
1780
2016-07-30 03:20:00
27
15
1782
2016-07-30 19:10:00
21
3
1783
2016-07-31 01:00:00
27
11
1787
2016-07-31 14:05:00
40
15
1786
2016-07-31 17:30:00
20
11
1862
2016-08-01 14:10:00
28
8
1796
2016-08-02 00:30:00
29
15
1798
2016-08-02 15:50:00
28
15
1809
2016-08-03 05:00:00
51
8
1797
2016-08-03 14:20:00
54
4
1799
2016-08-03 15:25:00
57
19
1800
2016-08-04 02:35:00
21
5
1845
2016-08-04 21:30:00
23
11
1807
2016-08-05 12:15:00
15
14
1801
2016-08-05 12:40:00
16
3
1802
2016-08-06 00:30:00
18
24
1803
2016-08-06 03:10:00
32
18
1808
2016-08-06 04:25:00
29
4
1804
2016-08-06 23:00:00
26
12
1806
2016-08-07 02:19:00
17
5
1805
2016-08-07 02:20:00
43
12
1815
2016-08-08 02:25:00
43
11
1814
2016-08-08 02:27:00
25
12
1812
2016-08-08 13:20:00
61
6
1838
2016-08-08 19:25:00
22
11
1811
2016-08-08 19:50:00
24
8
1810
2016-08-08 20:45:00
28
11
1816
2016-08-08 21:25:00
28
11
1813
2016-08-08 22:18:00
27
7
1817
2016-08-08 23:17:00
44
6
1818
2016-08-09 14:20:00
18
7
1820
2016-08-09 20:10:00
32
5
1819
2016-08-09 20:30:00
25
24
1826
2016-08-09 22:55:00
30
15
1822
2016-08-10 22:00:00
22
12
1823
2016-08-10 22:20:00
22
5
186
2016-08-11 01:55:00
50
6
1825
2016-08-11 14:27:00
22
3
1824
2016-08-11 23:55:00
37
7
1835
2016-08-12 13:09:00
28
15
1828
2016-08-12 22:15:00
17
7
1837
2016-08-12 23:35:00
30
14
1833
2016-08-13 01:40:00
23
8
1832
2016-08-13 13:00:00
27
11
1831
2016-08-14 00:04:00
19
8
1830
2016-08-14 02:18:00
31
15
1829
2016-08-14 03:03:00
31
12
1839
2016-08-14 05:11:00
28
15
1840
2016-08-14 09:00:00
21
3
1843
2016-08-14 10:45:00
2 days
10
1834
2016-08-15 04:00:00
33
11
1844
2016-08-16 03:30:00
19
25
1836
2016-08-16 03:55:00
21
11
1842
2016-08-16 17:15:00
26
12
1841
2016-08-16 22:00:00
40
3
989
2016-08-16 22:45:00
37
10
1861
2016-08-17 23:50:00
28
5
1856
2016-08-19 08:40:00
24
4
1846
2016-08-19 17:35:00
26
6
1848
2016-08-20 20:10:00
20
25
1860
2016-08-20 22:30:00
66
14
1847
2016-08-21 00:05:00
24
11
1857
2016-08-21 02:50:00
25
8
1849
2016-08-21 04:00:00
14
25
1868
2016-08-21 06:45:00
26
11
1851
2016-08-21 16:15:00
22
11
1850
2016-08-21 16:30:00
33
8
1863
2016-08-22 10:05:00
68
1
1879
2016-08-23 01:39:00
4
1883
2016-08-23 01:39:00
56
4
1853
2016-08-23 01:39:00
3 mos.
4
1852
2016-08-23 01:39:00
4
1859
2016-08-23 15:20:00
42
4
1854
2016-08-23 18:00:00
20
10
1878
2016-08-23 18:55:00
22
8
1877
2016-08-23 21:40:00
37
10
1876
2016-08-24 04:10:00
24
9
1875
2016-08-24 10:54:00
28
7
1867
2016-08-26 15:30:00
32
3
12
2016-08-26 18:12:00
35
25
990
2016-08-26 18:15:00
55
19
1866
2016-08-27 03:35:00
21
5
1869
2016-08-27 12:15:00
19
3
1870
2016-08-28 00:15:00
30
11
1880
2016-08-28 00:35:00
38
11
62
2016-08-28 10:05:00
45
1
1872
2016-08-28 16:20:00
20
15
1874
2016-08-28 19:05:00
19
8
1873
2016-08-29 00:30:00
35
5
1884
2016-08-29 17:15:00
34
10
1881
2016-08-29 20:55:00
22
6
1882
2016-08-29 22:10:00
16
15
1885
2016-08-31 00:45:00
20
12
1886
2016-08-31 13:15:00
33
8
187
2016-08-31 21:16:00
21
2
991
2016-08-31 21:55:00
24
8
In [42]:
from IPython.display import display
import numpy as np
import pandas as pd
median_age_2015 = homicides_to_day_2015['Age'][homicides_to_day_2015['Age'].apply(np.isreal)].median()
median_age_2016 = homicides_to_day_2016['Age'][homicides_to_day_2016['Age'].apply(np.isreal)].median()
median_age = pd.DataFrame({
2015: pd.Series([median_age_2015], index=['Median age']),
2016: pd.Series([median_age_2016], index=['Median age']),
})
display(median_age)
2015
2016
Median age
25.0
25.5
In [43]:
from IPython.display import display
import numpy as np
import pandas as pd
youngest_2015 = homicides_to_day_2015['Age'][homicides_to_day_2015['Age'].apply(np.isreal)].min()
youngest_2016 = homicides_to_day_2016['Age'][homicides_to_day_2016['Age'].apply(np.isreal)].min()
oldest_2015 = homicides_to_day_2015['Age'][homicides_to_day_2015['Age'].apply(np.isreal)].max()
oldest_2016 = homicides_to_day_2016['Age'][homicides_to_day_2016['Age'].apply(np.isreal)].max()
oldest_youngest = pd.DataFrame({
2015: pd.Series([youngest_2015, oldest_2015], index=['Youngest victim age', 'Oldest victim age']),
2016: pd.Series([youngest_2016, oldest_2016], index=['Youngest victim age', 'Oldest victim age']),
})
display(oldest_youngest)
2015
2016
Youngest victim age
1
2
Oldest victim age
79
84
In [44]:
from IPython.display import display
by_police_district = pd.DataFrame({
2015: homicides_to_day_2015.groupby('District Number').size(),
2016: homicides_to_day_2016.groupby('District Number').size(),
})
by_police_district['change'] = by_police_district[2016] - by_police_district[2015]
by_police_district['percent change'] = (((by_police_district[2016] - by_police_district[2015]) / by_police_district[2015]) * 100)
display(by_police_district)
2015
2016
change
percent change
1
4.0
8
4.0
100.000000
2
14.0
20
6.0
42.857143
3
15.0
29
14.0
93.333333
4
26.0
21
-5.0
-19.230769
5
19.0
23
4.0
21.052632
6
22.0
35
13.0
59.090909
7
25.0
55
30.0
120.000000
8
19.0
41
22.0
115.789474
9
20.0
33
13.0
65.000000
10
22.0
25
3.0
13.636364
11
33.0
63
30.0
90.909091
12
14.0
19
5.0
35.714286
14
10.0
8
-2.0
-20.000000
15
20.0
41
21.0
105.000000
16
3.0
1
-2.0
-66.666667
17
9.0
10
1.0
11.111111
18
1.0
3
2.0
200.000000
19
8.0
7
-1.0
-12.500000
20
4.0
2
-2.0
-50.000000
22
16.0
18
2.0
12.500000
24
7.0
8
1.0
14.285714
25
19.0
21
2.0
10.526316
1.0
2
1.0
100.000000
31
NaN
1
NaN
NaN
In [45]:
from IPython.display import display
by_month = pd.DataFrame({
2015: homicides_to_day_2015.groupby('month').size(),
2016: homicides_to_day_2016.groupby('month').size(),
})
by_month['change'] = by_month[2016] - by_month[2015]
by_month['percent change'] = (((by_month[2016] - by_month[2015]) / by_month[2015]) * 100)
display(by_month)
2015
2016
change
percent change
month
1.0
32
57
25
78.125000
2.0
20
45
25
125.000000
3.0
35
49
14
40.000000
4.0
35
41
6
17.142857
5.0
49
68
19
38.775510
6.0
49
72
23
46.938776
7.0
54
71
17
31.481481
8.0
57
91
34
59.649123
In [50]:
from datetime import datetime, timedelta
from dateutil import rrule
from dateutil import parser
from dateutil import relativedelta
from IPython.display import display
today = datetime.now().date()
fridays_2015 = rrule.rrule(rrule.WEEKLY, byweekday=relativedelta.FR(1), dtstart=datetime(2015,1,1), count=52)
fridays_2016 = rrule.rrule(rrule.WEEKLY, byweekday=relativedelta.FR(1), dtstart=datetime(2016,1,1), count=52)
end_date_2016 = datetime(2016, 9, 1)
end_date_2015 = end_date.replace(year=2015)
fridays_2015_to_date = [d.date() for d in fridays_2015 if d < end_date_2015]
fridays_2016_to_date = [d.date() for d in fridays_2016 if d < end_date_2016]
def get_weekends(fridays):
weekends = []
for friday in fridays:
friday_dt = datetime(year=friday.year, month=friday.month, day=friday.day, hour=15, minute=0)
monday_dt = (friday_dt + timedelta(days=3)).replace(hour=6, minute=0)
weekends.append((friday_dt, monday_dt))
return weekends
weekends_2015_to_date = get_weekends(fridays_2015_to_date)
weekends_2016_to_date = get_weekends(fridays_2016_to_date)
1 2015-01-02 15:00:00 2015-01-05 06:00:00
2 2015-01-09 15:00:00 2015-01-12 06:00:00
3 2015-01-16 15:00:00 2015-01-19 06:00:00
4 2015-01-23 15:00:00 2015-01-26 06:00:00
5 2015-01-30 15:00:00 2015-02-02 06:00:00
6 2015-02-06 15:00:00 2015-02-09 06:00:00
7 2015-02-13 15:00:00 2015-02-16 06:00:00
8 2015-02-20 15:00:00 2015-02-23 06:00:00
9 2015-02-27 15:00:00 2015-03-02 06:00:00
10 2015-03-06 15:00:00 2015-03-09 06:00:00
11 2015-03-13 15:00:00 2015-03-16 06:00:00
12 2015-03-20 15:00:00 2015-03-23 06:00:00
13 2015-03-27 15:00:00 2015-03-30 06:00:00
14 2015-04-03 15:00:00 2015-04-06 06:00:00
15 2015-04-10 15:00:00 2015-04-13 06:00:00
16 2015-04-17 15:00:00 2015-04-20 06:00:00
17 2015-04-24 15:00:00 2015-04-27 06:00:00
18 2015-05-01 15:00:00 2015-05-04 06:00:00
19 2015-05-08 15:00:00 2015-05-11 06:00:00
20 2015-05-15 15:00:00 2015-05-18 06:00:00
21 2015-05-22 15:00:00 2015-05-25 06:00:00
22 2015-05-29 15:00:00 2015-06-01 06:00:00
23 2015-06-05 15:00:00 2015-06-08 06:00:00
24 2015-06-12 15:00:00 2015-06-15 06:00:00
25 2015-06-19 15:00:00 2015-06-22 06:00:00
26 2015-06-26 15:00:00 2015-06-29 06:00:00
27 2015-07-03 15:00:00 2015-07-06 06:00:00
28 2015-07-10 15:00:00 2015-07-13 06:00:00
29 2015-07-17 15:00:00 2015-07-20 06:00:00
30 2015-07-24 15:00:00 2015-07-27 06:00:00
31 2015-07-31 15:00:00 2015-08-03 06:00:00
32 2015-08-07 15:00:00 2015-08-10 06:00:00
33 2015-08-14 15:00:00 2015-08-17 06:00:00
34 2015-08-21 15:00:00 2015-08-24 06:00:00
35 2015-08-28 15:00:00 2015-08-31 06:00:00
1 2016-01-01 15:00:00 2016-01-04 06:00:00
2 2016-01-08 15:00:00 2016-01-11 06:00:00
3 2016-01-15 15:00:00 2016-01-18 06:00:00
4 2016-01-22 15:00:00 2016-01-25 06:00:00
5 2016-01-29 15:00:00 2016-02-01 06:00:00
6 2016-02-05 15:00:00 2016-02-08 06:00:00
7 2016-02-12 15:00:00 2016-02-15 06:00:00
8 2016-02-19 15:00:00 2016-02-22 06:00:00
9 2016-02-26 15:00:00 2016-02-29 06:00:00
10 2016-03-04 15:00:00 2016-03-07 06:00:00
11 2016-03-11 15:00:00 2016-03-14 06:00:00
12 2016-03-18 15:00:00 2016-03-21 06:00:00
13 2016-03-25 15:00:00 2016-03-28 06:00:00
14 2016-04-01 15:00:00 2016-04-04 06:00:00
15 2016-04-08 15:00:00 2016-04-11 06:00:00
16 2016-04-15 15:00:00 2016-04-18 06:00:00
17 2016-04-22 15:00:00 2016-04-25 06:00:00
18 2016-04-29 15:00:00 2016-05-02 06:00:00
19 2016-05-06 15:00:00 2016-05-09 06:00:00
20 2016-05-13 15:00:00 2016-05-16 06:00:00
21 2016-05-20 15:00:00 2016-05-23 06:00:00
22 2016-05-27 15:00:00 2016-05-30 06:00:00
23 2016-06-03 15:00:00 2016-06-06 06:00:00
24 2016-06-10 15:00:00 2016-06-13 06:00:00
25 2016-06-17 15:00:00 2016-06-20 06:00:00
26 2016-06-24 15:00:00 2016-06-27 06:00:00
27 2016-07-01 15:00:00 2016-07-04 06:00:00
28 2016-07-08 15:00:00 2016-07-11 06:00:00
29 2016-07-15 15:00:00 2016-07-18 06:00:00
30 2016-07-22 15:00:00 2016-07-25 06:00:00
31 2016-07-29 15:00:00 2016-08-01 06:00:00
32 2016-08-05 15:00:00 2016-08-08 06:00:00
33 2016-08-12 15:00:00 2016-08-15 06:00:00
34 2016-08-19 15:00:00 2016-08-22 06:00:00
35 2016-08-26 15:00:00 2016-08-29 06:00:00
In [51]:
for i, weekend in enumerate(weekends_2015_to_date):
start, end = weekend
print(i+1, start, end)
1 2015-01-02 15:00:00 2015-01-05 06:00:00
2 2015-01-09 15:00:00 2015-01-12 06:00:00
3 2015-01-16 15:00:00 2015-01-19 06:00:00
4 2015-01-23 15:00:00 2015-01-26 06:00:00
5 2015-01-30 15:00:00 2015-02-02 06:00:00
6 2015-02-06 15:00:00 2015-02-09 06:00:00
7 2015-02-13 15:00:00 2015-02-16 06:00:00
8 2015-02-20 15:00:00 2015-02-23 06:00:00
9 2015-02-27 15:00:00 2015-03-02 06:00:00
10 2015-03-06 15:00:00 2015-03-09 06:00:00
11 2015-03-13 15:00:00 2015-03-16 06:00:00
12 2015-03-20 15:00:00 2015-03-23 06:00:00
13 2015-03-27 15:00:00 2015-03-30 06:00:00
14 2015-04-03 15:00:00 2015-04-06 06:00:00
15 2015-04-10 15:00:00 2015-04-13 06:00:00
16 2015-04-17 15:00:00 2015-04-20 06:00:00
17 2015-04-24 15:00:00 2015-04-27 06:00:00
18 2015-05-01 15:00:00 2015-05-04 06:00:00
19 2015-05-08 15:00:00 2015-05-11 06:00:00
20 2015-05-15 15:00:00 2015-05-18 06:00:00
21 2015-05-22 15:00:00 2015-05-25 06:00:00
22 2015-05-29 15:00:00 2015-06-01 06:00:00
23 2015-06-05 15:00:00 2015-06-08 06:00:00
24 2015-06-12 15:00:00 2015-06-15 06:00:00
25 2015-06-19 15:00:00 2015-06-22 06:00:00
26 2015-06-26 15:00:00 2015-06-29 06:00:00
27 2015-07-03 15:00:00 2015-07-06 06:00:00
28 2015-07-10 15:00:00 2015-07-13 06:00:00
29 2015-07-17 15:00:00 2015-07-20 06:00:00
30 2015-07-24 15:00:00 2015-07-27 06:00:00
31 2015-07-31 15:00:00 2015-08-03 06:00:00
32 2015-08-07 15:00:00 2015-08-10 06:00:00
33 2015-08-14 15:00:00 2015-08-17 06:00:00
34 2015-08-21 15:00:00 2015-08-24 06:00:00
35 2015-08-28 15:00:00 2015-08-31 06:00:00
In [52]:
for i, weekend in enumerate(weekends_2016_to_date):
start, end = weekend
print(i+1, start, end)
1 2016-01-01 15:00:00 2016-01-04 06:00:00
2 2016-01-08 15:00:00 2016-01-11 06:00:00
3 2016-01-15 15:00:00 2016-01-18 06:00:00
4 2016-01-22 15:00:00 2016-01-25 06:00:00
5 2016-01-29 15:00:00 2016-02-01 06:00:00
6 2016-02-05 15:00:00 2016-02-08 06:00:00
7 2016-02-12 15:00:00 2016-02-15 06:00:00
8 2016-02-19 15:00:00 2016-02-22 06:00:00
9 2016-02-26 15:00:00 2016-02-29 06:00:00
10 2016-03-04 15:00:00 2016-03-07 06:00:00
11 2016-03-11 15:00:00 2016-03-14 06:00:00
12 2016-03-18 15:00:00 2016-03-21 06:00:00
13 2016-03-25 15:00:00 2016-03-28 06:00:00
14 2016-04-01 15:00:00 2016-04-04 06:00:00
15 2016-04-08 15:00:00 2016-04-11 06:00:00
16 2016-04-15 15:00:00 2016-04-18 06:00:00
17 2016-04-22 15:00:00 2016-04-25 06:00:00
18 2016-04-29 15:00:00 2016-05-02 06:00:00
19 2016-05-06 15:00:00 2016-05-09 06:00:00
20 2016-05-13 15:00:00 2016-05-16 06:00:00
21 2016-05-20 15:00:00 2016-05-23 06:00:00
22 2016-05-27 15:00:00 2016-05-30 06:00:00
23 2016-06-03 15:00:00 2016-06-06 06:00:00
24 2016-06-10 15:00:00 2016-06-13 06:00:00
25 2016-06-17 15:00:00 2016-06-20 06:00:00
26 2016-06-24 15:00:00 2016-06-27 06:00:00
27 2016-07-01 15:00:00 2016-07-04 06:00:00
28 2016-07-08 15:00:00 2016-07-11 06:00:00
29 2016-07-15 15:00:00 2016-07-18 06:00:00
30 2016-07-22 15:00:00 2016-07-25 06:00:00
31 2016-07-29 15:00:00 2016-08-01 06:00:00
32 2016-08-05 15:00:00 2016-08-08 06:00:00
33 2016-08-12 15:00:00 2016-08-15 06:00:00
34 2016-08-19 15:00:00 2016-08-22 06:00:00
35 2016-08-26 15:00:00 2016-08-29 06:00:00
In [ ]:
def is_weekend(timestamp):
"""Does the timestamp fall between Friday 3 p.m. and Monday 6 a.m."""
if not timestamp:
return False
day_of_week = timestamp.weekday()
if day_of_week > 0 and day_of_week < 4:
return False
if day_of_week == 4:
# Friday
# Same day, 3 p.m.
start = datetime(timestamp.year, timestamp.month, timestamp.day, 15)
return timestamp >= start
if day_of_week == 0:
# Monday
# Same day, 6 a.m.
end = datetime(timestamp.year, timestamp.month, timestamp.day, 6)
return timestamp < end
return True
homicides_to_day_2015_weekend = homicides_to_day_2015[homicides_to_day_2015.apply(lambda r: is_weekend(r['datetime']), axis=1)]
homicides_to_day_2016_weekend = homicides_to_day_2016[homicides_to_day_2016.apply(lambda r: is_weekend(r['datetime']), axis=1)]
def get_weekend_2015(row):
for i, weekend in enumerate(weekends_2015_to_date):
weekend_start, weekend_end = weekend
if row['datetime'] >= weekend_start and row['datetime'] <= weekend_end:
return i + 1
def get_weekend_2016(row):
for i, weekend in enumerate(weekends_2016_to_date):
weekend_start, weekend_end = weekend
if row['datetime'] >= weekend_start and row['datetime'] <= weekend_end:
return i + 1
homicides_to_day_2015_weekend['weekend'] = homicides_to_day_2015_weekend.apply(get_weekend_2015, axis=1)
homicides_to_day_2016_weekend['weekend'] = homicides_to_day_2016_weekend.apply(get_weekend_2016, axis=1)
by_weekend = pd.DataFrame({
2015: homicides_to_day_2015_weekend.groupby('weekend').size(),
2016: homicides_to_day_2016_weekend.groupby('weekend').size(),
})
by_weekend['change'] = by_weekend[2016] - by_weekend[2015]
by_weekend['percent change'] = (((by_weekend[2016] - by_weekend[2015]) / by_weekend[2015]) * 100)
display(by_weekend)
In [ ]:
Content source: newsapps/public-notebooks
Similar notebooks: