Skip to content
This repository was archived by the owner on Jan 18, 2023. It is now read-only.

Commit ac6a3de

Browse files
authored
Merge pull request #63 from rht/perf
format_aal_summary_property: Replace .apply(pd.Series) with more performant alternative
2 parents 8882dc6 + d9bfb35 commit ac6a3de

1 file changed

Lines changed: 13 additions & 11 deletions

File tree

firststreet/api/csv_format.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,16 +1174,19 @@ def format_aal_summary_property(data):
11741174
df = df.explode('depth_loss')
11751175

11761176
if not df[['annual_loss', 'depth_loss']].isna().values.all():
1177-
df = pd.concat([df.drop(['annual_loss'], axis=1), df['annual_loss'].apply(pd.Series)], axis=1)
1178-
if 'data' in df.columns:
1179-
df = pd.concat([df.drop(['data'], axis=1), df['data'].apply(pd.Series)], axis=1)
1180-
else:
1181-
df['year'] = pd.NA
1182-
df['low'] = pd.NA
1183-
df['mid'] = pd.NA
1184-
df['high'] = pd.NA
1185-
1186-
df = pd.concat([df.drop(['depth_loss'], axis=1), df['depth_loss'].apply(pd.Series)], axis=1)
1177+
def expand_al(al):
1178+
if pd.isnull(al):
1179+
return pd.NA, pd.NA, pd.NA, pd.NA
1180+
return al['year'], al['data']['low'], al['data']['mid'], al['data']['high']
1181+
df['year'], df['low'], df['mid'], df['high'] = zip(*df['annual_loss'].apply(expand_al))
1182+
df.drop(['annual_loss'], axis=1)
1183+
1184+
def expand_dl(dl):
1185+
if pd.isnull(dl):
1186+
return pd.NA, pd.NA
1187+
return dl['depth'], dl['data']
1188+
df['depth'], df['damage'] = zip(*df['depth_loss'].apply(expand_dl))
1189+
df.drop(['depth_loss'], axis=1)
11871190
else:
11881191
df['fsid'] = df['fsid'].apply(str)
11891192
df.drop(['annual_loss'], axis=1, inplace=True)
@@ -1195,7 +1198,6 @@ def format_aal_summary_property(data):
11951198
df['mid'] = pd.NA
11961199
df['high'] = pd.NA
11971200

1198-
df.rename(columns={'data': 'damage'}, inplace=True)
11991201
df['fsid'] = df['fsid'].apply(str)
12001202
df['year'] = df['year'].astype('Int64').apply(str)
12011203
df['low'] = df['low'].astype('Int64').apply(str)

0 commit comments

Comments
 (0)