Giter Club home page Giter Club logo

python-us's Introduction

Workflow status badge

US: The Greatest Package in the World

A package for easily working with US and state metadata.

  • all US states and territories
  • postal abbreviations
  • Associated Press style abbreviations
  • FIPS codes
  • capitals
  • years of statehood
  • time zones
  • phonetic state name lookup
  • is contiguous or continental
  • URLs to shapefiles for state, census, congressional districts, counties, and census tracts

Installation

As per usual:

pip install us

Features

Easy access to state information:

>>> import us
>>> us.states.MD
<State:Maryland>
>>> us.states.MD.fips
'24'
>>> us.states.MD.name
'Maryland'
>>> us.states.MD.is_contiguous
True

Includes territories too:

>>> us.states.VI.name
'Virgin Islands'
>>> us.states.VI.is_territory
True
>>> us.states.MD.is_territory
False

List of all (actual) states:

>>> us.states.STATES
[<State:Alabama>, <State:Alaska>, <State:Arizona>, <State:Arkansas>, ...
>>> us.states.TERRITORIES
[<State:American Samoa>, <State:Guam>, <State:Northern Mariana Islands>, ...

And the whole shebang, if you want it:

>>> us.states.STATES_AND_TERRITORIES
[<State:Alabama>, <State:Alaska>, <State:American Samoa>, ...

For convenience, STATES, TERRITORIES, and STATES_AND_TERRITORIES can be accessed directly from the us module:

>>> us.states.STATES
[<State:Alabama>, <State:Alaska>, <State:Arizona>, <State:Arkansas>, ...
>>> us.STATES
[<State:Alabama>, <State:Alaska>, <State:Arizona>, <State:Arkansas>, ...

Some states like to be fancy and call themselves commonwealths:

>>> us.states.COMMONWEALTHS
[<State:Kentucky>, <State:Massachusetts>, <State:Pennsylvania>, <State:Virginia>]

There's also a list of obsolete territories:

>>> us.states.OBSOLETE
[<State:Dakota>, <State:Orleans>, <State:Philippine Islands>]

The state lookup method allows matching by FIPS code, abbreviation, and name:

>>> us.states.lookup('24')
<State:Maryland>
>>> us.states.lookup('MD')
<State:Maryland>
>>> us.states.lookup('md')
<State:Maryland>
>>> us.states.lookup('maryland')
<State:Maryland>

Get useful information:

>>> state = us.states.lookup('maryland')
>>> state.abbr
'MD'

And for those days that you just can't remember how to spell Mississippi, we've got phonetic name matching too:

>>> us.states.lookup('misisipi')
<State:Mississippi>

Shapefiles

You want shapefiles too? As long as you want 2010 shapefiles, we've gotcha covered.

>>> urls = us.states.MD.shapefile_urls()
>>> sorted(urls.keys())
['block', 'blockgroup', 'cd', 'county', 'state', 'tract', 'zcta']
>>> urls['block']
'https://www2.census.gov/geo/tiger/TIGER2010/TABBLOCK/2010/tl_2010_24_tabblock10.zip'

The shapefile_urls() method on the State object generates shapefile URLs for the following regions:

  • block
  • blockgroup
  • census tract (tract)
  • congressional district (cd)
  • county
  • state
  • zcta

Mappings

Mappings between various state attributes are a common need. The mapping() method will generate a lookup between two specified fields.

>>> us.states.mapping('fips', 'abbr')
{'01': 'AL', '02': 'AK', '04': 'AZ', '05': 'AR', '06': 'CA', ...
>>> us.states.mapping('abbr', 'name')
{'AL': 'Alabama', 'AK': 'Alaska', 'AZ': 'Arizona', 'AR': 'Arkansas', ...

This method uses us.STATES_AND_TERRITORIES as the default list of states it will create a mapping for, but this can be overridden by passing an additional states argument:

>>> us.states.mapping('fips', 'abbr', states=[us.states.DC])
{'11': 'DC'}

DC should be granted statehood

Washington, DC does not appear in us.STATES or any of the related state lists, but is often treated as a state in practice and should be granted statehood anyway. DC can be automatically included in these lists by setting a DC_STATEHOOD environment variable to any truthy value before importing this package.

DC_STATEHOOD=1

CLI

When you need to know state information RIGHT AWAY, there's the states script.

$ states md

*** The great state of Maryland (MD) ***

    FIPS code: 24

    other attributes:
      ap_abbr: Md.
      capital: Annapolis
      capital_tz: America/New_York
      is_contiguous: True
      is_continental: True
      is_obsolete: False
      name_metaphone: MRLNT
      statehood_year: 1788
      time_zones: America/New_York

    shapefiles:
      tract: https://www2.census.gov/geo/tiger/TIGER2010/TRACT/2010/tl_2010_24_tract10.zip
      cd: https://www2.census.gov/geo/tiger/TIGER2010/CD/111/tl_2010_24_cd111.zip
      county: https://www2.census.gov/geo/tiger/TIGER2010/COUNTY/2010/tl_2010_24_county10.zip
      state: https://www2.census.gov/geo/tiger/TIGER2010/STATE/2010/tl_2010_24_state10.zip
      zcta: https://www2.census.gov/geo/tiger/TIGER2010/ZCTA5/2010/tl_2010_24_zcta510.zip
      block: https://www2.census.gov/geo/tiger/TIGER2010/TABBLOCK/2010/tl_2010_24_tabblock10.zip
      blockgroup: https://www2.census.gov/geo/tiger/TIGER2010/BG/2010/tl_2010_24_bg10.zip

Running Tests

GitHub Actions are set up to automatically run unit tests against any new commits to the repo. To run these tests yourself:

pip install -e .[dev]
pytest .

Changelog

3.2.0

3.1.1

  • add support for Python 3.11
  • upgrade to jellyfish 0.11.2

3.0.0

  • upgrade to jellyfish 0.7.2
  • drop support for Python 2.7
  • add us.states.COMMONWEALTHS list of states that call themselves commonwealths ๐ŸŽฉ
  • add DC to STATES, STATES_AND_TERRITORIES, STATES_CONTIGUOUS, or STATES_CONTINENTAL when DC_STATEHOOD environment variable is set
  • remove region parameter from shapefile_urls() method
  • mapping() no longer includes obsolete states
  • added type annotations

2.0.2

  • restore DC in lookup() and mapping()

2.0.1

  • fix Python 2.7 tests that ran with Python 3
  • revert to jellyfish 0.6.1 to support Python 2.7

2.0.0

  • add support for Python 3.7 and 3.8
  • remove support for Python 3.4 and 3.5
  • remove pickled objects and database in favor of pure Python code
  • upgrade jellyfish to 0.7.2 to fix metaphone bug
  • fixes for IN, KY, ND, and NM timezones
  • set AZ timezone to America/Phoenix
  • obsolete entries are no longer included in STATES_AND_TERRITORIES
  • DC is no longer included in STATES, STATES_AND_TERRITORIES, STATES_CONTIGUOUS, or STATES_CONTINENTAL

1.0.0

  • full Python 3.6 support
  • use pytest

0.10.0

  • upgrade jellyfish to 0.5.3 to fix metaphone bug

0.9.0

  • add information on whether a state is contiguous and/or continental, thanks to chebee7i

0.8.0

0.7.1

  • upgrade to jellyfish 0.5.1 to fix metaphone case bug

0.7

0.6

  • add AP-style state abbreviations
  • use jellyfish instead of Metaphone package
  • update to requests v1.0.4 for tests
  • Python 3.3 compatibility

0.5

  • fix state abbreviation for Nebraska

0.4

  • add state capitals
  • add years of statehood

0.3

  • add mapping method to generate dicts of arbitrary fields

0.2

  • add command line script for quick access to state data

0.1

  • initial release
  • state names and abbreviations
  • FIPS codes
  • lookup() method
  • shapefile URLs for various regions

python-us's People

Contributors

adamjstewart avatar am0z avatar bchartoff avatar br0ken- avatar chebee7i avatar dgilmanaidentified avatar garnertb avatar jakobovski avatar jcarbaugh avatar jeffpaine avatar mileswwatkins avatar momyc avatar owenam avatar pauldhawk avatar pedrocamargo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

python-us's Issues

Utah not working for lookup

Basically here's the deal (looking up "Utah" or "utah" returns None, but looking up with "UT" or "uta" returns the proper state object):

>>> us.states.lookup(u'UT')
<State:Utah>
>>> us.states.lookup(u'uta')
<State:Utah>
>>> us.states.lookup(u'Utah')
>>> us.states.lookup(u'utah')
>>> us.states.lookup(u'New Mexico')
<State:New Mexico>

This is with 0.9.1.

Installation Fails with Python 2.7

I don't find any mention of dropping python 2 support in the changelog for 2.0.0, but installing with pip fails due to the jellyfish==0.7.2 requirement.

Could you either document that you're dropping support or fix this dependency issue?

> pip install us==2.0.0
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Collecting us==2.0.0
  Downloading us-2.0.0.tar.gz (13 kB)
ERROR: Could not find a version that satisfies the requirement jellyfish==0.7.2 (from us==2.0.0) (from versions: 0.1, 0.1.1, 0.1.2, 0.2.0, 0.2.1, 0.2.2, 0.3.0, 0.3.1, 0.3.2, 0.3.3, 0.3.4, 0.4.0, 0.5.0, 0.5.1, 0.5.2, 0.5.3, 0.5.4, 0.5.5, 0.5.6, 0.6.0, 0.6.1)
ERROR: No matching distribution found for jellyfish==0.7.2 (from us==2.0.0)

Easier way to apply to DataFrames

In mapping a pandas DataFrame's numeric fips index to state name, I currently have to do this:

state['state'] = pd.Series(state.index).apply(
    lambda x: us.states.lookup(str(x).zfill(2)).name).tolist()

There are a couple things going on here that could be other issues*, but might also be nice to have the vectorization built-in for pandas. e.g. I'd like to be able to just do:
state['state'] = us.states.lookup(state.index)

* This one was verbose because things like us.states.lookup(1) and us.states.lookup('1') fail.

Timezone Inconsistencies

Using this package for just the state to timezone(s) info, it seems there are missing and incorrect timezones.

Some that I have found:

  • IN: Missing America/Chicago and America/New_York. Just outdated overall.
  • KY: Missing America/Chicago. Unnecessary America/Louisville (has America/Kentucky/Louisville)
  • NM: Should be America/Denver, not America/Chicago.

These are only a few, so there must be more inconsistencies around with other state timezone(s).

[Feature Request] return GeoJSON url

Version: '1.0.0'

Like the package a lot!

It would be nice if there is a method to return GeoJSON urls instead of .zip.

But I didn't find any resources to attach here and not sure if the Census API even supports returning GeoJSON.

Cut the 3.0 release

How can we help cut the 3.0 release so we can push for 3.9 and 3.10 support?

Extending to Zipcode lookup?

Would there be interest in adding a zipcode lookup functionality for this package? That would mean extending the data beyond just state level and return city, state, county, timezone, lat/long for a given zipcode.

I'd be open to submitting a PR if that would be welcome.

Push to pypi?

Hello! I love, and indeed have always loved, this package.

2.0.2 has an old jellyfish dependency, however, and I see that 3 has been pushed with a jellyfish upgrade.

I could install from github but was wondering if you intend to push to pypi soon-- if so, I can hold on.

Thanks!

sqlite to code

Due to ease of merging and other issues, I'm thinking the sqlite + pkl file no longer makes sense. The original intention was to have easier modification of data, but merging multiple changes to the database is pretty much impossible without manual intervention.

I'm thinking it's time to just convert it all to Python with a class for each state. Any thoughts @mileswwatkins?

Allow states to have a border attr?

Just ran into this case and wondering if it might be useful to others -

It might be handy to add a borders method on the state object that returns a list of states that border the state, e.g. <State:California>.borders (or another more appropriate method name) would return [<State:Nevada>, <State:Oregon>, <State:Arizona>]. For now I'll have to do a geospatial query to find the bordering states but it might be computationally expensive

Arizona capital timezone allows DST

Arizona's capital timezone is recorded as 'America/Denver'.
see us.states.mapping('name', 'time_zones')['Arizona']

Arizona does not do daylight savings time, but 'America/Denver' does. Arizona is always on MST (aka 'America/Phoenix')

Utah fails on .lookup

us.states.lookup(u'Utah') in version 0.9.1 returns nothing, while every other state seems to work fine.

us.states.lookup('New York State') returns None

It seems like it ought to return NY. In general, looking up 'XXX State' (or phonetic/misspelled equivalents?) should probably work for any of the 50 states, though I think it's far more common for New York State and Washington State than any others (to distinguish them from the cities).

But I actually do run into "New York State" as a state name in reasonably well curated data sets, so that one seems particularly important to include.

AttributeError: __exit__

Am I experiencing a load error related to 2.7, or something else?

Admins-iMac-2:data patsplat$ sudo pip install us --upgrade
Requirement already up-to-date: us in /Library/Python/2.7/site-packages/us-0.7-py2.7.egg
Requirement already up-to-date: jellyfish==0.2.0 in /Library/Python/2.7/site-packages/jellyfish-0.2.0-py2.7-macosx-10.9-intel.egg (from us)
Cleaning up...
Admins-iMac-2:data patsplat$ python
Python 2.7.5 (default, Aug 25 2013, 00:04:04) 
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import us
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "build/bdist.macosx-10.9-intel/egg/us/__init__.py", line 1, in <module>
  File "build/bdist.macosx-10.9-intel/egg/us/states.py", line 117, in <module>
  File "build/bdist.macosx-10.9-intel/egg/us/states.py", line 51, in load_states
AttributeError: __exit__
>>> 

conda-forge says us package incompatible with python 3.8

I see you guys added support for Python 3.8 in version 2.0.0, but still got this error in conda today (after solving the base environment for an age):

UnsatisfiableError: The following specifications were found
to be incompatible with the existing python installation in your environment:

Specifications:

  • us -> python[version='2.7.|3.5.|3.6.|3.4.']

Your python: python=3.8

If python is on the left-most side of the chain, that's the version you've asked for.
When python appears to the right, that indicates that the thing on the left is somehow
not available for the python version you are constrained to. Note that conda will not
change your python version to a different minor version unless you explicitly specify
that.

I'm running conda 4.8.4 and tried both conda install us and conda -c conda-forge us since I saw a bug suggesting I might need to specify the channel. Same message both times.

Not sure if this is something on your side or conda-forge not using the latest version. I succeeded in 2 seconds with pip install us. Let me know if I should be reporting this to conda instead.

Update: looks like conda-forge is still trying to use 1.0.0, no wonder:
image

MyPy Type Hint Support

Can we add type hints for this library? Currently mypy complains: error: Skipping analyzing "us": module is installed, but missing library stubs or py.typed marker [import-untyped]

Provide an OrderedDict of abbr : name

I often find it useful in my projects to have a hash/associative array of state abbreviations and names. What do you think about adding this to the package?

Add Associated states

Add the three associated states of the USA which are lists as Marshall Islands, Micronesia, and Palau here

OBSOLETE included in STATES_AND_TERRITORIES

Not sure if this is intentional, but it is odd behavior.

set(us.states.STATES_AND_TERRITORIES) == set(us.states.STATES + us.states.TERRITORIES + us.states.OBSOLETE)

I would expect STATES_AND_TERRITORIES to be just STATES + TERRITORIES

TypeError: str argument expected with fresh install on Python 3.10.1

On Python 3.10.1, a fresh pip install us yields this TypeError:

Python 3.10.1 (main, Dec 22 2021, 05:19:12) [GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import us
>>> us.states.lookup('maryland')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/max/.pyenv/versions/3.10.1/lib/python3.10/site-packages/us/states.py", line 86, in lookup
    val = jellyfish.metaphone(val)
TypeError: str argument expected

This is the output of pip freeze:

$ pip freeze
jellyfish==0.6.1
us==2.0.2

Is this a known issue?

KENTUCKY not found in lookup

Fails to find state Kentucky when using all caps.

import us
>>> us.states.lookup('KENTUCKY')
>>> us.states.lookup('kentucky')
<State:Kentucky>

Drop obsolete territories from package?

Thinking that we could completely drop Dakota, Orleans, and the Philippines from the version 2.0 release @jcarbaugh. Special-casing them presents problems regarding:

  • neighboring
  • capital
  • time_zones, since official time zones, much less IANA ones, may not have even been invented; eg, Orleans was admitted to the union as the State of Louisiana about 70 years before US time zones were implemented
  • abbr and ap_abbr, for similar timeline reasons

What are the core user stories that would support keeping these obsolete territories around? Seems like the headache of keeping them around wouldn't be worth it, but very happy to have my mind changed!

_lookup_cache

Hi.
For the cache to work, it may be necessary to refine the code.
Maybe after this line is required else or return matched_state :)

matched_state = _lookup_cache[cache_key]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.