Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update validate_state() in helpers.py #26

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 43 additions & 37 deletions pygris/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,50 +78,56 @@ def fips_codes():
def validate_state(state, quiet = False):
# Standardize as lowercase
original_input = state
state = str(state).lower()
# Get rid of whitespace
state = state.strip()

# If the FIPS code is supplied
if state.isdigit():
if isinstance(state, str):
state = str(state).lower()
# Get rid of whitespace
state = state.strip()
if state.isdigit():
# Left-pad if necessary
state = state.zfill(2)
# Return the result
return state

# If the FIPS code is supplied as an int
elif isinstance(state, int):
#convert to string
state=str(state)
# Left-pad if necessary
state = state.zfill(2)

# Return the result
return state
else:
# Get the FIPS codes dataset
fips = fips_codes()
# If a state abbreviation, use the state postal code
if len(state) == 2:
fips['postal_lower'] = fips.state.str.lower()
state_sub = fips.query('postal_lower == @state')

if state_sub.shape[0] == 0:
raise ValueError("You have likely entered an invalid state code, please revise.")
else:
state_fips = state_sub.state_code.unique()[0]

if not quiet:
print(f"Using FIPS code '{state_fips}' for input '{original_input}'")

return state_fips

# Get the FIPS codes dataset
fips = fips_codes()
# If a state abbreviation, use the state postal code
if len(state) == 2:
fips['postal_lower'] = fips.state.str.lower()
state_sub = fips.query('postal_lower == @state')

if state_sub.shape[0] == 0:
raise ValueError("You have likely entered an invalid state code, please revise.")
else:
# If a state name, grab the appropriate info from fips_codes
fips['name_lower'] = fips.state_name.str.lower()
state_sub = fips.query('name_lower == @state')

if state_sub.shape[0] == 0:
raise ValueError("You have likely entered an invalid state code, please revise.")
else:
state_fips = state_sub.state_code.unique()[0]

if not quiet:
print(f"Using FIPS code '{state_fips}' for input '{original_input}'")

return state_fips
state_fips = state_sub.state_code.unique()[0]

if not quiet:
print(f"Using FIPS code '{state_fips}' for input '{original_input}'")

return state_fips
else:
# If a state name, grab the appropriate info from fips_codes
fips['name_lower'] = fips.state_name.str.lower()
state_sub = fips.query('name_lower == @state')

if state_sub.shape[0] == 0:
raise ValueError("You have likely entered an invalid state code, please revise.")
else:
state_fips = state_sub.state_code.unique()[0]

if not quiet:
print(f"Using FIPS code '{state_fips}' for input '{original_input}'")

return state_fips

def validate_county(state, county, quiet = False):
state = validate_state(state)

Expand Down