In [63]:
%matplotlib inline
import metapack as mp
import pandas as pd
import seaborn as sns
from matplotlib import pyplot

mt_pkg = mp.jupyter.open_package()
mt_pkg


Out[63]:

Police Calls for Service

sandiego.gov-police_calls-2015e-1

Calls dispatched by the San Diego Police Department’s communications dispatch

metapack+file:/Volumes/Storage/proj/virt/data-projects/sdrdl-data-projects/sandiego.gov/sandiego.gov-police_calls-2015e/_packages/sandiego.gov-police_calls-2015e-1/metadata.csv

Contacts

Wrangler: Eric Busboom Civic Knowledge

Resources

  1. call_type - file:/Volumes/Storage/proj/virt/data-projects/sdrdl-data-projects/sandiego.gov/sandiego.gov-police_calls-2015e/_packages/sandiego.gov-police_calls-2015e-1/data/call_type.csv Type of call

  2. disposition - file:/Volumes/Storage/proj/virt/data-projects/sdrdl-data-projects/sandiego.gov/sandiego.gov-police_calls-2015e/_packages/sandiego.gov-police_calls-2015e-1/data/disposition.csv Classification

  3. beat - file:/Volumes/Storage/proj/virt/data-projects/sdrdl-data-projects/sandiego.gov/sandiego.gov-police_calls-2015e/_packages/sandiego.gov-police_calls-2015e-1/data/beat.csv San Diego PD Beat

  4. pd_calls - file:/Volumes/Storage/proj/virt/data-projects/sdrdl-data-projects/sandiego.gov/sandiego.gov-police_calls-2015e/_packages/sandiego.gov-police_calls-2015e-1/data/pd_calls.csv Combined pd_calls for 2015, 2016 and YTD 2017

References

  1. pd_calls_2015 - http://seshat.datasd.org/pd/pd_calls_for_service_2015_datasd.csv Service calls for 2015

  2. pd_calls_2016 - http://seshat.datasd.org/pd/pd_calls_for_service_2016_datasd.csv Service calls for 2016

  3. pd_calls_2017_ytd - http://seshat.datasd.org/pd/pd_calls_for_service_2017_datasd.csv Service calls for 2017, Year To Date


In [2]:
df = mt_pkg.resource('pd_calls').read_csv(low_memory=False)
len(df)


/Volumes/Storage/proj/virt/metapack/lib/python3.6/site-packages/IPython/core/interactiveshell.py:2850: DtypeWarning: Columns (7,13) have mixed types. Specify dtype option on import or set low_memory=False.
  if self.run_code(code, result):
Out[2]:
1579262

In [4]:
df.iloc[:5].T


Out[4]:
0 1 2 3 4
incident_num P15010000001 P15010000002 P15010000003 P15010000004 P15010000005
date_time 2015-01-01 00:00:14 2015-01-01 00:00:30 2015-01-01 00:00:48 2015-01-01 00:00:57 2015-01-01 00:01:05
day 5 5 5 5 5
stno 3800 0 3600 0 3300
stdir1 NaN NaN NaN NaN NaN
street DELTA 60TH LOGAN 33RD 43RD
streettype ST ST AV ST ST
stdir2 NaN NaN NaN NaN NaN
stname2 NaN FEDERAL NaN IMPERIAL NaN
sttype2 NaN BL NaN AV NaN
call_type AU1 AU1 AU1 AU1 AU1
disposition K DUP DUP K O
beat 443 433 441 512 834
priority 1 1 1 1 1

In [30]:
ct = mt_pkg.resource('call_type').dataframe()
ct


Out[30]:
call_type description
0 10 OUT OF SERV, SUBJTOCALL
1 10-97 ARRIVE ON SCENE
2 1016 PRISONER IN CUSTODY
3 1016PT PTU (PRISONER TRANSPORT)
4 1016QC SHOPLIFTER/QUICK CITE
5 1016T BORDERPRISONERTRANSPORT
6 1019 RETURN TO STATION
7 1021 PHONE YOUR STATION
8 1021H PHONE YOUR HOME
9 1042 SECURE, END OF SHIFT
10 10851 AUTO THEFT
11 10851E EVAL-POSS UNREPORTED CAR THEFT
12 10851R CAR THEFT REPORT
13 10851RR CAR THEFT RECOVERY REPORT
14 10852 TAMPERING W/VEH IN PROGRESS
15 10852A VEHICLE ALARM/AUDIBLE
16 10852C VEHICLE CASER
17 10852R TAMPERING W/VEH REPORT
18 1087 MEET OFFICER 10-10
19 11-6 DISCHARGING FIREARMS
20 11-7 PROWLER
21 11-8 PERSON DOWN
22 1110M REPORT OF CRIME OCCD MEXICO
23 1111 CHECK THE AREA
24 1115 BALL GAME IN STREET(83 03MC)
25 1130 INCOMPLETE PHONE CALL
26 1131 CALLING FOR HELP
27 1131A CALLING FOR HELP-PANIC ALARM
28 1140OD AMBULANCE CALL, OVERDOSE
29 1141 AMBULANCE NEEDED
... ... ...
275 RESCUE RESCUE CITIZENS
276 RJ RUNAWAY JUVENILE, REPORT
277 ROUTE ROUTE SLIP (FLD INVESTIGATION)
278 ROUTINE OUT OF SERVICE ACTIVITY
279 RRM ROCK N ROLL MARATHON
280 RRM-1185 ROCK N ROLL 1185
281 RRM-PSI ROCK N ROLL PSI
282 RRM-TOWS VEHICLES TOWED DUE TO RRM
283 SCALP TICKET SCALPER
284 SCHOOL SCHOOL DETAIL
285 SELENF SELECTIVE ENFORCEMENT
286 SHOPS INFORMATION FOR SHOPS
287 SIG SIGALERT
288 SKATE SKATEBOARDERS
289 SLEEPER SLEEPER
290 SP SUBJECT PURSUIT (AKASPURS)
291 SPAN BILINGUAL TALLY INFO
292 SPURS FOOT PURSUIT
293 STOP TRAFFIC STOP,FOR TMPOUT
294 STREET STREET INFORMATION
295 T TRAFFIC STOP
296 TAGGER TAGGER VANDALISM, IN PROGRESS
297 TARASOFF TARASOFF REPORT
298 TLO TERRORISM LIAISON OFFICER
299 TP TRAFFIC PURSUIT (AKA TPURS)
300 TPURS TRAFFIC PURSUIT
301 TPURSCR TRAFFIC PURSUIT-COST REC
302 TRO TRO VIOLATION, REPORT
303 VARDA VARDA ALARM
304 WARRANT WARRANT

305 rows × 2 columns


In [8]:
df['date_time'] = pd.to_datetime(df.date_time)

In [12]:
df.date_time.dt.year.value_counts()


Out[12]:
2015    627472
2016    589451
2017    362339
Name: date_time, dtype: int64

In [103]:
df['hour'] = df.date_time.dt.hour
df['month'] = df.date_time.dt.month
df['year'] = df.date_time.dt.year
df['dayofweek'] = df.date_time.dt.dayofweek
df['weekofyear'] = df.date_time.dt.weekofyear

df['weekofdata'] = (df.year-df.year.min())*52+df.date_time.dt.weekofyear
df['monthofdata'] = (df.year-df.year.min())*12+df.date_time.dt.month

In [104]:
df.year.describe()


Out[104]:
count    1.579262e+06
mean     2.015832e+03
std      7.736735e-01
min      2.015000e+03
25%      2.015000e+03
50%      2.016000e+03
75%      2.016000e+03
max      2.017000e+03
Name: year, dtype: float64

When are the raging parties?


In [69]:
ht = pd.pivot_table(data=df[df.call_type=='PARTY'], 
                    values='incident_num', index=['hour'],columns=['dayofweek'],
                    aggfunc='count')

fig, ax = pyplot.subplots(figsize=(10,10))

sns.heatmap(ht, ax=ax)


Out[69]:
<matplotlib.axes._subplots.AxesSubplot at 0x11374cd30>

In [51]:
ht = pd.pivot_table(data=df[df.call_type=='1150'], 
                    values='incident_num', index=['hour'],columns=['dayofweek'],
                    aggfunc='count')
sns.heatmap(ht)


Out[51]:
<matplotlib.axes._subplots.AxesSubplot at 0x117f70710>

In [50]:
ht = pd.pivot_table(data=df, 
                    values='incident_num', index=['hour'],columns=['month'],
                    aggfunc='count')
sns.heatmap(ht)


Out[50]:
<matplotlib.axes._subplots.AxesSubplot at 0x111b328d0>

In [108]:
fig, ax = pyplot.subplots(figsize=(15,8))

top_ct = df.call_type.value_counts().iloc[:10]
top_ct

df_tct = df[df.call_type.isin(list(top_ct.index.values))]


ht = pd.pivot_table(data=df_tct, 
                    values='incident_num', index=['call_type'],columns=['weekofdata'],
                    aggfunc='count')

ht.T.plot(ax=ax)

#sns.heatmap(ht, robust = True, square=False, ax=ax)


Out[108]:
<matplotlib.axes._subplots.AxesSubplot at 0x11d8f2e48>

In [ ]: