zipcode-features 0.1.3__tar.gz → 0.1.4__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {zipcode_features-0.1.3 → zipcode_features-0.1.4}/PKG-INFO +1 -1
- {zipcode_features-0.1.3 → zipcode_features-0.1.4}/pyproject.toml +1 -1
- {zipcode_features-0.1.3 → zipcode_features-0.1.4}/setup.py +1 -1
- {zipcode_features-0.1.3 → zipcode_features-0.1.4}/src/zipcode_features/__init__.py +54 -22
- {zipcode_features-0.1.3 → zipcode_features-0.1.4}/uv.lock +1 -1
- {zipcode_features-0.1.3 → zipcode_features-0.1.4}/LICENSE +0 -0
- {zipcode_features-0.1.3 → zipcode_features-0.1.4}/README.md +0 -0
- {zipcode_features-0.1.3 → zipcode_features-0.1.4}/azure-pipelines.yml +0 -0
- {zipcode_features-0.1.3 → zipcode_features-0.1.4}/clean_install.sh +0 -0
- {zipcode_features-0.1.3 → zipcode_features-0.1.4}/requirements.txt +0 -0
- {zipcode_features-0.1.3 → zipcode_features-0.1.4}/src/zipcode_features/data/CBSA_ZIP_122025.csv +0 -0
- {zipcode_features-0.1.3 → zipcode_features-0.1.4}/src/zipcode_features/data/ZIP_COUNTY_122025.csv +0 -0
- {zipcode_features-0.1.3 → zipcode_features-0.1.4}/src/zipcode_features/data/bls_2025_quarter_four.csv +0 -0
- {zipcode_features-0.1.3 → zipcode_features-0.1.4}/src/zipcode_features/data/bls_2025_quarter_one.csv +0 -0
- {zipcode_features-0.1.3 → zipcode_features-0.1.4}/src/zipcode_features/data/bls_2025_quarter_three.csv +0 -0
- {zipcode_features-0.1.3 → zipcode_features-0.1.4}/src/zipcode_features/data/bls_2025_quarter_two.csv +0 -0
- {zipcode_features-0.1.3 → zipcode_features-0.1.4}/src/zipcode_features/data/cbsa_codes.json +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: zipcode-features
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.4
|
|
4
4
|
Summary: A Python package to process and extract features from zipcode data.
|
|
5
5
|
Project-URL: Homepage, https://github.com/EricSchles/zipcode_features
|
|
6
6
|
Author-email: Eric Schles <your.email@example.com>
|
|
@@ -10,7 +10,7 @@ README = (HERE / "README.md").read_text()
|
|
|
10
10
|
# This call to setup() does all the work
|
|
11
11
|
setup(
|
|
12
12
|
name="zipcode_features",
|
|
13
|
-
version="0.1.
|
|
13
|
+
version="0.1.4",
|
|
14
14
|
description="A tool to get features based on census data from zipcodes",
|
|
15
15
|
long_description=README,
|
|
16
16
|
long_description_content_type="text/markdown",
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
__version__ = '0.1.
|
|
1
|
+
__version__ = '0.1.4'
|
|
2
2
|
|
|
3
3
|
import zipcodes
|
|
4
4
|
from zipcode3.search import SearchEngine
|
|
5
5
|
from importlib import resources
|
|
6
6
|
import pandas as pd
|
|
7
7
|
import json
|
|
8
|
+
from functools import partial
|
|
8
9
|
|
|
9
10
|
zip_to_fips = {
|
|
10
11
|
'00544': '36103', # Suffolk County
|
|
@@ -142,14 +143,22 @@ zip_to_fips = {
|
|
|
142
143
|
'14925': '36015', # Chemung County
|
|
143
144
|
}
|
|
144
145
|
|
|
145
|
-
def
|
|
146
|
-
if x["
|
|
147
|
-
return "
|
|
148
|
-
|
|
149
|
-
return "
|
|
146
|
+
def code_mapper(col, x):
|
|
147
|
+
if x[f"{col}_len"] == 2:
|
|
148
|
+
return "000" + x[col]
|
|
149
|
+
if x[f"{col}_len"] == 3:
|
|
150
|
+
return "00" + x[col]
|
|
151
|
+
elif x[f"{col}_len"] == 4:
|
|
152
|
+
return "0" + x[col]
|
|
150
153
|
else:
|
|
151
|
-
return x[
|
|
152
|
-
|
|
154
|
+
return x[col]
|
|
155
|
+
|
|
156
|
+
def map_zip_to_cbsa(zip_to_cbsa, zip_code):
|
|
157
|
+
if zip_code in zip_to_cbsa:
|
|
158
|
+
return zip_to_cbsa[zip_code]
|
|
159
|
+
else:
|
|
160
|
+
return '00000'
|
|
161
|
+
|
|
153
162
|
def _get_zip_to_cbsa_code() -> dict:
|
|
154
163
|
"""
|
|
155
164
|
This method gets a mapping from zipcode to cbsa code
|
|
@@ -158,10 +167,15 @@ def _get_zip_to_cbsa_code() -> dict:
|
|
|
158
167
|
"""
|
|
159
168
|
with resources.path("zipcode_features.data", "CBSA_ZIP_122025.csv") as csv_path:
|
|
160
169
|
df = pd.read_csv(csv_path, dtype={'ZIP': str, "CBSA": str})
|
|
161
|
-
df
|
|
162
|
-
|
|
163
|
-
return
|
|
170
|
+
df = correct_code(df, "ZIP")
|
|
171
|
+
mapping = df.set_index('ZIP')['CBSA'].to_dict()
|
|
172
|
+
return partial(map_zip_to_cbsa, mapping)
|
|
164
173
|
|
|
174
|
+
def map_cbsa_code_to_name(cbsa_code_to_name, cbsa_code):
|
|
175
|
+
if cbsa_code in cbsa_code_to_name:
|
|
176
|
+
return cbsa_code_to_name[cbsa_code]
|
|
177
|
+
else:
|
|
178
|
+
return 'Unknown'
|
|
165
179
|
|
|
166
180
|
def _get_cbsa_code_to_cbsa_name() -> dict:
|
|
167
181
|
"""
|
|
@@ -170,20 +184,38 @@ def _get_cbsa_code_to_cbsa_name() -> dict:
|
|
|
170
184
|
"""
|
|
171
185
|
with resources.path("zipcode_features.data", "cbsa_codes.json") as json_path:
|
|
172
186
|
code_to_name = json.load(open(json_path))
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
187
|
+
df = pd.DataFrame(columns=["code", "name"])
|
|
188
|
+
df["name"] = code_to_name.values()
|
|
189
|
+
df["code"] = code_to_name.keys()
|
|
190
|
+
df["name"] = df["name"].str.replace(" -", "-")
|
|
191
|
+
df["name"] = df["name"].str.split().str.join(' ')
|
|
192
|
+
code_to_name = df.set_index('code')['name'].to_dict()
|
|
193
|
+
return partial(map_cbsa_code_to_name, code_to_name)
|
|
194
|
+
|
|
195
|
+
def correct_code(df: pd.DataFrame, col: str) -> pd.DataFrame:
|
|
196
|
+
df[f"{col}_len"] = df[col].apply(lambda x: len(x))
|
|
197
|
+
corrector = partial(code_mapper, col)
|
|
198
|
+
df[col] = df.apply(corrector, axis=1)
|
|
199
|
+
return df
|
|
200
|
+
|
|
201
|
+
def map_zip_code_to_fips_code(zip_code_to_fips_code, zip_code):
|
|
202
|
+
if zip_code in zip_code_to_fips_code:
|
|
203
|
+
return zip_code_to_fips_code[zip_code]
|
|
204
|
+
else:
|
|
205
|
+
return '000000'
|
|
206
|
+
#fips codes are 5 digits long, so this should always
|
|
207
|
+
#map to nothing
|
|
177
208
|
|
|
178
209
|
def _get_zip_to_fips_code() -> dict:
|
|
179
210
|
with resources.path("zipcode_features.data", "ZIP_COUNTY_122025.csv") as csv_path:
|
|
180
211
|
df = pd.read_csv(csv_path, dtype={'ZIP': str, "COUNTY": str})
|
|
181
|
-
|
|
182
|
-
df
|
|
212
|
+
|
|
213
|
+
df = correct_code(df, "ZIP")
|
|
214
|
+
df = correct_code(df, "COUNTY")
|
|
183
215
|
df = df[["ZIP", "COUNTY"]]
|
|
184
216
|
zip_to_county = df.set_index('ZIP')['COUNTY'].to_dict()
|
|
185
217
|
zip_to_county.update(zip_to_fips)
|
|
186
|
-
return zip_to_county
|
|
218
|
+
return partial(map_zip_code_to_fips_code, zip_to_county)
|
|
187
219
|
|
|
188
220
|
def _get_bls_data() -> pd.DataFrame:
|
|
189
221
|
csvs = [
|
|
@@ -234,7 +266,7 @@ def _get_bls_data() -> pd.DataFrame:
|
|
|
234
266
|
final_df = final_df.rename({"area_fips": "fips_code"}, axis=1)
|
|
235
267
|
final_df["fips_code"] = final_df["fips_code"].astype(str)
|
|
236
268
|
return final_df
|
|
237
|
-
|
|
269
|
+
|
|
238
270
|
|
|
239
271
|
def us_get_demographics(state: str, city: str = None, zip_list: list = None) -> pd.DataFrame:
|
|
240
272
|
"""
|
|
@@ -279,11 +311,11 @@ def us_get_demographics(state: str, city: str = None, zip_list: list = None) ->
|
|
|
279
311
|
demographics.append(tmp_dict)
|
|
280
312
|
df = pd.DataFrame(demographics)
|
|
281
313
|
zip_to_cbsa = _get_zip_to_cbsa_code()
|
|
282
|
-
df["cbsa"] = df["zip_code"].
|
|
314
|
+
df["cbsa"] = df["zip_code"].apply(zip_to_cbsa)
|
|
283
315
|
cbsa_code_to_name = _get_cbsa_code_to_cbsa_name()
|
|
284
|
-
df["cbsa_name"] = df["cbsa"].
|
|
316
|
+
df["cbsa_name"] = df["cbsa"].apply(cbsa_code_to_name)
|
|
285
317
|
zip_code_to_fips_code = _get_zip_to_fips_code()
|
|
286
|
-
df["fips_code"] = df["zip_code"].
|
|
318
|
+
df["fips_code"] = df["zip_code"].apply(zip_code_to_fips_code)
|
|
287
319
|
bls_data = _get_bls_data()
|
|
288
320
|
return pd.merge(df, bls_data, how="inner", on="fips_code")
|
|
289
321
|
|
|
@@ -734,7 +734,7 @@ wheels = [
|
|
|
734
734
|
|
|
735
735
|
[[package]]
|
|
736
736
|
name = "zipcode-features"
|
|
737
|
-
version = "0.
|
|
737
|
+
version = "0.0.9"
|
|
738
738
|
source = { editable = "." }
|
|
739
739
|
dependencies = [
|
|
740
740
|
{ name = "numpy", version = "2.2.6", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version < '3.11'" },
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{zipcode_features-0.1.3 → zipcode_features-0.1.4}/src/zipcode_features/data/CBSA_ZIP_122025.csv
RENAMED
|
File without changes
|
{zipcode_features-0.1.3 → zipcode_features-0.1.4}/src/zipcode_features/data/ZIP_COUNTY_122025.csv
RENAMED
|
File without changes
|
|
File without changes
|
{zipcode_features-0.1.3 → zipcode_features-0.1.4}/src/zipcode_features/data/bls_2025_quarter_one.csv
RENAMED
|
File without changes
|
|
File without changes
|
{zipcode_features-0.1.3 → zipcode_features-0.1.4}/src/zipcode_features/data/bls_2025_quarter_two.csv
RENAMED
|
File without changes
|
|
File without changes
|