Analyses of crime data that researchers often need.
Each function applies one of the amos techniques of crimepy.techniques,
so the same analysis can be named in a project's settings. The functions
take an amos.Dataset or a data frame with one row for each event (an
incident, offense, victim, or arrest), such as those of crimepy.nibrs.load
and crimepy.nibrs.tables.build:
count: the number of rows in each group and period, as a new dataset.trend: counts over time, one column for each group.rates: counts per 100,000 people (or another base).crime_rates: the rate of crime in each group, using the population of the agencies that reported for the whole year.clearance_rates: the share of incidents cleared in each group.percent_change: the change from one period to the next.crosstab: counts (or shares) of two variables against each other.coverage: how many agencies reported, and how many people they serve.
Each function but count returns a table. Given a dataset, it also stores
the table in the dataset's tables, under the technique's name (such as
"clearance_rates").
Contents
clearance_rates: the share of incidents cleared in each group. count: the number of rows in each group and period. coverage: the agencies that reported and the people they serve. crime_rates: crimes per 100,000 people, by group. crosstab: counts or shares of two variables against each other. percent_change: the change from one period to the next. rates: counts per 100,000 people. trend: counts over time, one column for each group.
clearance_rates(incidents, by=None, *, cleared='cleared')
¶
Returns the share of incidents that were cleared in each group.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
incidents
|
Dataset | DataFrame
|
one row for each incident, with a column saying whether
it was cleared (such as from |
required |
by
|
Sequence[str] | str | None
|
columns to group by. Defaults to |
None
|
cleared
|
str
|
the column saying whether each incident was cleared. Defaults to "cleared". |
'cleared'
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
A table with a column for each group, "incidents", "cleared", and "clearance_rate" (from 0 to 1). |
Source code in src/crimepy/analyze.py
count(data, by=None, *, date=None, freq=None, weight=None, name='count')
¶
Returns the number of rows in each group (and period).
Applies crimepy.techniques.CountCrimes to a copy of the data, so a
dataset that is passed keeps its rows.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Dataset | DataFrame
|
one row for each event. |
required |
by
|
Sequence[str] | str | None
|
columns to group by. Defaults to |
None
|
date
|
str | None
|
column of dates that |
None
|
freq
|
str | None
|
length of each period, as a |
None
|
weight
|
str | None
|
column whose values are summed instead of counting rows.
Defaults to |
None
|
name
|
str
|
name of the column of counts. Defaults to "count". |
'count'
|
Returns:
| Type | Description |
|---|---|
Dataset
|
A dataset with a column for each group (and "period") and the counts, which are its label. Groups with no rows are left out. |
Source code in src/crimepy/analyze.py
coverage(agencies, by=('data_year', 'state'), *, months=12)
¶
Returns how many agencies reported and how many people they serve.
Agencies covered by another agency (whose crimes the other agency reports) are left out, so that people are not counted twice.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agencies
|
Dataset | DataFrame
|
the agencies, with "population" and "months_active". |
required |
by
|
Sequence[str] | str
|
columns to group by. Defaults to ("data_year", "state"). |
('data_year', 'state')
|
months
|
int
|
the months that an agency must have reported in to count as reporting for the year. Defaults to 12. |
12
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
A table with a column for each group, "agencies", "reporting" (the
agencies that reported in at least |
Source code in src/crimepy/analyze.py
crime_rates(events, agencies=None, by=('data_year', 'state'), *, months=12, per=100000, folder=None)
¶
Returns the number of events per per people in each group.
Only agencies that reported in at least months months of the year (see
"months_active" in crimepy.nibrs.munge) count, both for the events and
for the population, so that the rate is not lowered by agencies that did
not report.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
events
|
Dataset | DataFrame
|
one row for each event (such as an incident), with "data_year" and "agency_ori" (or "ori"). |
required |
agencies
|
Dataset | DataFrame | None
|
the agencies, with "data_year", "ori", "population", and
"months_active". Defaults to |
None
|
by
|
Sequence[str] | str
|
columns of the agencies to group by. Defaults to ("data_year", "state"). |
('data_year', 'state')
|
months
|
int
|
the fewest months an agency must have reported in. Defaults to 12. |
12
|
per
|
float
|
the number of people that rates are per. Defaults to 100,000. |
100000
|
folder
|
Any
|
the data folder to load agencies from. Defaults to |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
A table with a column for each group, "count", "population", "agencies", and "rate". |
Source code in src/crimepy/analyze.py
crosstab(data, rows, columns, *, normalize=False, margins=False)
¶
Returns counts (or shares) of two variables against each other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Dataset | DataFrame
|
one row for each event. |
required |
rows
|
str
|
the column whose values become rows. |
required |
columns
|
str
|
the column whose values become columns. |
required |
normalize
|
bool | Literal['all', 'index', 'columns']
|
False for counts, True for shares of all rows, "index"
for shares of each row, or "columns" for shares of each column.
Defaults to |
False
|
margins
|
bool
|
whether to add totals. Defaults to |
False
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
The table. |
Source code in src/crimepy/analyze.py
percent_change(data, periods=1)
¶
Returns the percent change from one period to the next.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Dataset | DataFrame | Series
|
counts (or rates) in each period, in order, such as a table
from |
required |
periods
|
int
|
the number of periods to compare across (12 compares each month with the same month of the year before). Defaults to 1. |
1
|
Returns:
| Type | Description |
|---|---|
DataFrame | Series
|
The change, in percent, with the index of |
Source code in src/crimepy/analyze.py
rates(counts, population, *, on=None, count='count', per=100000, name='rate')
¶
Returns counts per per people.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
counts
|
Dataset | DataFrame
|
a table of counts (such as one from |
required |
population
|
Dataset | DataFrame | str
|
a column of |
required |
on
|
Sequence[str] | str | None
|
the columns that join |
None
|
count
|
str
|
the column of counts. Defaults to "count". |
'count'
|
per
|
float
|
the number of people that rates are per. Defaults to 100,000. |
100000
|
name
|
str
|
name of the column of rates. Defaults to "rate". |
'rate'
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
|
Source code in src/crimepy/analyze.py
trend(data, by=None, *, date='incident_date', freq='MS', top=None)
¶
Returns counts over time, with one column for each group.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Dataset | DataFrame
|
one row for each event. |
required |
by
|
str | None
|
column whose values become columns. Defaults to |
None
|
date
|
str
|
column of dates. Defaults to "incident_date". |
'incident_date'
|
freq
|
str
|
length of each period, as a |
'MS'
|
top
|
int | None
|
keep only the groups with the most rows. Defaults to |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
A table indexed by the start of each period, with a column of counts for each group (0 in periods with none). |