Skip to main content

Inspect csv files and lists of data

Project description

Penny
=====

Inspect csv files and lists of data to find out the truth!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. figure:: http://www.martianwatches.com/wp-content/uploads/2013/10/InspectorGadget.jpg
:alt: alt tag

alt tag
Uncle Gadget was great and all, but when it came to real detective work,
we all know Penny did the heavy lifting. Hence, Penny, the Python module
that inspects stuff. Feed it rows or columns from a dataset, and get
information about the column types -- including whether or not a given
column represents a category or date. Penny also finds column headers
(waaaay more reliably than the ``Sniffer`` class in to the standard
``csv`` module).

Why?
~~~~

If you're working with a few datasets, it's easy to figure out which
columns are supposed to be dates, integers and even categories just by
looking at the raw csv files. But if you need to programmatically deal
with lots of datasets, this gets tedious fast.

Setup
~~~~~

Grab the package.

::

pip install penny

Or grab the code from GitHub.

::

git clone https://github.com/gati/penny
cd penny
pip install -r requirements.txt

Getting Started
~~~~~~~~~~~~~~~

Guess the headers of a csv file.

.. code:: python

from penny.headers import get_headers

with open('your-awesome-file.csv') as csvfile:
has_header, headers = get_headers(csvfile)

# Prints True/False depending on whether or not headers were found
print has_header

# Prints column headers or placeholders if real headers weren't found
print headers # ['Example Header A', 'Example Header B']

Guess the data type of a column in your dataset.

.. code:: python

from penny.inspectors import column_types_probabilities

fileobj = open('your-awesome-file.csv')
rows = list(csv.reader(fileobj))

# Get the values from column 0
column_0 = [x[0] for x in rows]
probs = column_types_probabilities(column_0)

# Prints something like {'date': 1, 'int': .75, 'category': 0 ...}
print probs

Or get type guesses for all the rows in your dataset at once.

.. code:: python

from penny.inspectors import rows_types_probabilities

fileobj = open('your-awesome-file.csv')
rows = list(csv.reader(fileobj))
probs = rows_types_probabilities(rows)

Penny checks for a lot of data "types," not just the standard ``int``,
``str``, etc. Here's the list (for now):

- **date** something ``dateutil.parser`` can parse into a ``datetime``
object
- **int** a whole number
- **bool** y/n or yes/no or something true/falsey
- **float** a number with a decimal
- **category** something you might want to group records by
- **id** unique for each row
- **coord** a float that might be a latitude or longitude
- **coord\_pair** string that looks like "coord,coord"
- **city** one of the world's 80,000 largest cities
- **region** smaller than a country, bigger than a city. state,
province, etc
- **country** a country name on the `ISO 3166
list <http://en.wikipedia.org/wiki/ISO_3166-1#Current_codes>`__
- **address** a full address you could geocode with a service like
Google Maps

Last but not least, you can also inspect a column for a single type.

.. code:: python

from penny.list_check import column_probability_for_type

fileobj = open('your-awesome-file.csv')
rows = list(csv.reader(fileobj))

# Get the values from column 0
column_0 = [x[0] for x in rows]
prob = column_probability_for_type(column_0, 'date')

# Prints something like 0.78
print prob

Contributing & Credits
~~~~~~~~~~~~~~~~~~~~~~

This is a work in progress, so pull request at will. Some of this work
was inspired by `messytables <https://github.com/okfn/messytables>`__,
which looks great for xls files but wasn't quite what I needed. Thanks
to `Chris Albon <http://twitter.com/chrisalbon>`__ for putting together
a `repo of useful test
datasets <https://github.com/chrisalbon/Variable-Type-Identification-Test-Datasets>`__.

Questions, concerns, devoted fan mail to
[@jonathonmorgan](http://twitter.com/jonathonmorgan) on Twitter.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

penny-0.3.2.tar.gz (1.3 MB view details)

Uploaded Source

File details

Details for the file penny-0.3.2.tar.gz.

File metadata

  • Download URL: penny-0.3.2.tar.gz
  • Upload date:
  • Size: 1.3 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for penny-0.3.2.tar.gz
Algorithm Hash digest
SHA256 adfa23ad6b2a8498dc07c5a6eb3845da27bd89a9b1262a253795c381d1285b14
MD5 e256ca26a5634cf5b24c4b5eda693bd1
BLAKE2b-256 bad6610f1648e0511aa6c218e61007c10b814cfc1f52966b37b1f47b2253aa54

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page