Skip to content

Commit 685b48a

Browse files
committed
Improved performance in SeasonLookup - see HEA-787
1 parent 2ae935e commit 685b48a

2 files changed

Lines changed: 5 additions & 1 deletion

File tree

apps/metadata/lookups.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ def prepare_lookup_df(self) -> pd.DataFrame:
5959
# so duplicate any rows with a null purpose for all possible purposes for that country.
6060
all_purposes = [choice[0] for choice in self.model._meta.get_field("purpose").choices]
6161
all_countries = df["country_id"].unique().tolist()
62+
extra_dfs = []
6263
for country in all_countries:
6364
country_df = df[df["country_id"] == country]
6465
null_purpose_rows = country_df[country_df["purpose"].isnull()]
@@ -67,7 +68,9 @@ def prepare_lookup_df(self) -> pd.DataFrame:
6768
if purpose not in country_df["purpose"].unique():
6869
purpose_df = null_purpose_rows.copy()
6970
purpose_df["purpose"] = purpose
70-
df = pd.concat([df, purpose_df], ignore_index=True)
71+
extra_dfs.append(purpose_df)
72+
if extra_dfs:
73+
df = pd.concat([df] + extra_dfs, ignore_index=True)
7174
return df
7275

7376
def do_lookup(

apps/metadata/tests/factories.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ class Meta:
128128
description_ar = factory.LazyAttribute(lambda o: f"Description {o.name_ar} {o.season_type}")
129129
country = factory.SubFactory(CountryFactory)
130130
season_type = factory.Iterator([Season.SeasonType.WET, Season.SeasonType.DRY, Season.SeasonType.MILD])
131+
purpose = None
131132
order = factory.Iterator([1, 2, 3])
132133
start = factory.Iterator((25, 95, 200))
133134
end = factory.Iterator((95, 199, 360))

0 commit comments

Comments
 (0)