Skip to main content

crime    

View Updated Documentation

Source code is here


Easily load online crime datasts. Explore available datasets from inside a python notebook, with descriptive cell outputs showing general info and descriptions of each dataset and documentation of each column.


Install & Use

pip install crime
import crime as cr

Later, run pip install -U crime every few days to make sure you've got the latest version.

Note: this library should work with any recent Python version, but it has only been tested with 3.9.



How does it work?

Crime pre-defines nicknames and ids for a collection of Socrata datasets like this one for you to pick from. This info isn't stored in the package itself, but rather in this json file on Github, which can be updated anytime without changing the code. Every time you import crime, a Github API request is made to retrieve this configuration, so you'll need internet. Calling cr.sources() without parameters will just return this info, without making any additional requests.

In addition to letting you load/preview any of these datasets, crime's most important feature is its ability to show a detailed description on each dataset, with full documentation on every column. When you run cr.sources('dataset_name'), an api request is made to Socrata to get the metadata on a particular dataset. The most useful information gets formatted & printed to your screen. Here is what that output would look like if you looped through each dataset name and printed its description.

Caching: Any dataset you load fully will get stored in memory. So next time you request it within the same Jupyter notebook session, it will be available immediately.



Getting Started

Use cr.help() for a quick intro.

Let's look at the crime data available

cr.sources() # returns a DataFrame
image

You'll get a DataFrame with basic info on all the sources. The index, Name is the nickname with which you'll refer to the dataset moving forward.


To examine a source, pass the name of the dataset to sources(). This will make an api request to get all of its metadata.

Let's see the details on crime_vs_incarceration rate. All the info below is coming from Socrata's api.

cr.sources('crime_vs_incarceration') 
Total Crime Rate vs Incarceration Rate Chart
https://dev.socrata.com/foundry/data.colorado.gov/ae3x-wvn9 

Total Crime includes: Violent crimes- Murder and non-negligent
manslaughter, forcible rape, robbery, and aggravated assault. Property
crimes - Burglary, larceny/theft, and motor vehicle theft. National or
state offense totals are based on data from all reporting agencies and
estimates for unreported areas. Rates are the number of reported
offenses per 100,000 population. These figures are based on end of
calendar year populations.

Rows:    31
Cols:    9
Period:  1982 to 2012

COLUMNS:
-------
Year
  Field:  year
  Type:   text
  Null:   -
  Count:  31

Population
  Field:  population
  Type:   number
  Null:   -
  Count:  31
  Min:    3,045,000
  Max:    5,187,582
  Avg:    4,019,137.06
  Sum:    124,593,249

Violent Crime Total
  Field:  violent_crime_total
  Type:   number
  Null:   -
  Count:  31
  Min:    13,811
  Max:    20,229
  Avg:    16,445.55
  Sum:    509,812

(output is truncated to save space)

Here's what you'll see for text/categorical columns...

Race
  Field:  race
  Type:   text
  Null:   30
  Count:  209,241
  ITEMS:
     White  (164,446)
     Black  (39,467)
     Asian/Pacific Islander  (2,236)
     Unknown  (1,889)
     American Indian/Alaskan Native  (1,203)

Now we'll load some data

cr.load('arrest_demographics')
image

Returns 5-row preview by default, because some datasets have several million rows. To get the full dataset:

cr.load('arrest_demographics', full=True)
image

Get more info on a source

Return dictionary with full metadata

cr.metadata('dataset_name')

Return dataframe with metrics on each column

cr.columns('dataset_name')

Caching

Any dataset loaded fully (by passing full=True) will be stored in memory, regardless of whether you've assigned it to a variable. Next time you load it, you'll receive a new copy (not a reference) of the data.

For example, if you run this at the top of your notebook...

cr.load('arrest_demographics', full=True)

Now, elsewhere in your notebook...

EACH of these 3 lines will return the same thing: a COPY of the full dataset, returned instantly

# Shorthand to fetch straight from the cache. Returns empty df if none are found in cache
cr.df('arrest_demographics')
cr.load('arrest_demographics', full=True)
cr.load('arrest_demographics')

Add/Modify Sources

First, select a dataset on OpenDataNetwork and hit "View API". If you're brought to an API page like this one, (not all datasets have one), locate the "Dataset Identifier" on top-right side of page. Use that as id. For base_url, use the section of the url that comes after /foundry/.

cr.add_source("crime_rates", "mb89-xnkg", "data.colorado.gov")

You can pass any additional values as keyword arguments. Valid ones include rows, full_name, web_url, from_year, to_year, location, type, and topic.

To restore the original list of sources, use:

cr.reset_sources()

Clear sources; start with a blank slate

cr.clear_sources()


No proper documentation yet. View the source code if needed.

If there's a dataset not yet listed in our pre-defined sources, you can use the sodapy API wrapper to retrieve it manually.


Metadata

Release files for crime 0.1.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for crime 0.1.5
File Size Uploaded
crime-0.1.5.tar.gz 39.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for crime 0.1.5
File Interpreter ABI Platform
crime-0.1.5-py3-none-any.whl Python 3 none any Details

Total release size: 51.8 kB

Release files / crime-0.1.5.tar.gz

Download URL crime-0.1.5.tar.gz
Size 39.1 kB
Tags Source
SHA-256 checksum
How to use checksums
24db1c0ba6cbefaa269ec75a399eb0c34bee4efc183583c8b758e47d06c0df1f
BLAKE2b-256 checksum
How to use checksums
2cb7f4784173a5300dba5bed61e8545e35e06bfd091b18139fe1d8778732ff4c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.8

Release files / crime-0.1.5-py3-none-any.whl

Download URL crime-0.1.5-py3-none-any.whl
Size 12.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
824aea8797c2e8a804199fe85f94e4d590c3f512a589b7110172b75b3df1829d
BLAKE2b-256 checksum
How to use checksums
881ae5ee4f899db885556a6d28e66fa254b3b6ccda16ad54cd3c5051b93da8c3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.8

Release history Release notifications | RSS feed

This release

0.1.5 This release

2 release files

0.1.4

1 release file

0.1.3

1 release file

0.1.2

1 release file

0.1.1

1 release file

0.1.0

1 release file

0.0.9

1 release file

0.0.8

1 release file

0.0.7

1 release file

0.0.6

1 release file

0.0.5

1 release file

0.0.4

1 release file

0.0.3

1 release file

0.0.2

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page