Skip to content

Commit 016aba2

Browse files
add city full
1 parent a45dc48 commit 016aba2

3 files changed

Lines changed: 24 additions & 5 deletions

File tree

src/data_fetching/fusion_energy_base.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,22 @@ def aggregate_data(organization_generator):
3030
all_data = []
3131
for data in organization_generator:
3232
all_data.extend(data["results"])
33-
return pd.DataFrame(all_data)
33+
34+
df = pd.DataFrame(all_data)
35+
36+
df["city_full"] = df.apply(
37+
lambda row: (
38+
float("nan")
39+
if pd.isna(row["city"])
40+
else ", ".join(
41+
[
42+
str(x)
43+
for x in [row.get("city"), row.get("region"), row.get("country")]
44+
if pd.notna(x) and str(x).strip()
45+
]
46+
)
47+
),
48+
axis=1,
49+
)
50+
51+
return df

src/data_fetching/google_analytics.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def run_report(
3535
dimensions = [Dimension(name="date")] if daily else []
3636
dimensions += [
3737
Dimension(name="city"),
38+
Dimension(name="region"),
3839
Dimension(name="country"),
3940
]
4041

src/geospatial/geocoding.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ def find_locations_from_cache(df, filename: str, update_cache=True):
1111
print(f"No cache found at {filename}, starting with an empty cache.")
1212
cache = gpd.GeoDataFrame(columns=["city", "geometry"], crs="EPSG:4326")
1313

14-
cities_found = set(df["city"]) & set(cache["city"])
14+
cities_found = set(df["city_full"]) & set(cache["city"])
1515
locations_found = cache[cache["city"].isin(cities_found)]
1616

1717
# for cities not found in cache, geocode and add to cache
18-
locs_not_found = set(df["city"]) - set(cache["city"])
18+
locs_not_found = set(df["city_full"]) - set(cache["city"])
1919
if locs_not_found:
2020
print(f"Locations not found in cache: {locs_not_found}")
2121
locs_new = gpd.tools.geocode(list(locs_not_found))
@@ -28,8 +28,8 @@ def find_locations_from_cache(df, filename: str, update_cache=True):
2828
)
2929

3030
# ensure locations matches the order of df and avoid dtype mismatch errors
31-
merge_df = df[["city"]].copy()
32-
merge_df["city"] = merge_df["city"].astype(str)
31+
merge_df = df[["city_full"]].copy()
32+
merge_df["city"] = merge_df["city_full"].astype(str)
3333
locations["city"] = locations["city"].astype(str)
3434
locations = merge_df.merge(locations, on="city", how="left")
3535
locations = gpd.GeoDataFrame(locations, geometry="geometry")

0 commit comments

Comments
 (0)