Skip to content

Commit 7013f59

Browse files
committed
add stats to index.qmd
1 parent b8ce935 commit 7013f59

1 file changed

Lines changed: 74 additions & 3 deletions

File tree

index.qmd

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ import re
105105

106106
## Data
107107

108+
- Read data
109+
- Calculate Ranking
110+
- Set color mapping
111+
- Calculate stats
112+
108113
```{python}
109114
labour = read_labourcan(LABOUR_DATA_FILE)
110115
@@ -124,7 +129,7 @@ labour_processed = calculate_centered_rank(labour_filtered)
124129
125130
# Bin % difference
126131
labour_processed_cutted = cut_pdiff(labour_processed, DEFAULT_CUTS)
127-
plot_data = labour_processed_cutted.filter(
132+
labour_processed_filtered = labour_processed_cutted.filter(
128133
pl.col("YEAR") >= FILTER_YEAR[0], pl.col("YEAR") <= FILTER_YEAR[1]
129134
)
130135
@@ -160,16 +165,75 @@ LEGEND_LABELS = [
160165
]
161166
```
162167

168+
Stats
169+
170+
```{python}
171+
def make_subtitle_for_industry(df, INDUSTRY):
172+
# Define offsets
173+
offsets = {
174+
"1M": 1,
175+
"5M": 5,
176+
"1Y": 12,
177+
"5Y": 60,
178+
}
179+
180+
# Sort by industry + date
181+
labour_offset = df
182+
labour_offset = labour_offset.sort(["Industry", "DATE_YMD"])
183+
184+
# Compute diffs and %diffs for each horizon
185+
for label, months in offsets.items():
186+
labour_offset = labour_offset.with_columns(
187+
[
188+
(pl.col("DATE_YMD").shift(months).alias(f"DATE_YMD_{label}")),
189+
(
190+
pl.col("VALUE")
191+
.shift(months)
192+
.over("Industry")
193+
.alias(f"VALUE_{label}")
194+
),
195+
(
196+
pl.col("VALUE") - pl.col("VALUE").shift(months).over("Industry")
197+
).alias(f"DIFF_{label}"),
198+
(
199+
(pl.col("VALUE") - pl.col("VALUE").shift(months).over("Industry"))
200+
/ pl.col("VALUE").shift(months).over("Industry")
201+
* 100
202+
).alias(f"PDIFF_{label}"),
203+
]
204+
)
205+
# convert to dictionary for easier access
206+
stats = labour_offset.filter(
207+
pl.col("Industry") == INDUSTRY, pl.col("DATE_YMD") == pl.col("DATE_YMD").max()
208+
).to_dicts()[0]
209+
210+
periods = [
211+
f"{stats['DIFF_1M'] * 1000:<+8,.0f} {f'({stats["PDIFF_1M"]:+.2f}%)':<10} Past Month",
212+
f"{stats['DIFF_5M'] * 1000:<+8,.0f} {f'({stats["PDIFF_5M"]:+.2f}%)':<10} Past 5 Months",
213+
f"{stats['DIFF_1Y'] * 1000:<+8,.0f} {f'({stats["PDIFF_1Y"]:+.2f}%)':<10} Past Year",
214+
f"{stats['DIFF_5Y'] * 1000:<+8,.0f} {f'({stats["PDIFF_5Y"]:+.2f}%)':<10} Past 5 Years",
215+
]
216+
217+
subtitle_text = "\n".join(periods)
218+
return subtitle_text
219+
```
220+
163221
# Sector Shifts: Where Canada's Jobs Are Moving
164222

165-
Track the number of industries gaining or losing jobs each month. Boxes are shaded based on percentage change from previous month in each industry's employment levels. Data pulled directly from StatCan @statcan.
223+
Track the number of industries gaining or losing jobs each month. Boxes are shaded based on percentage change from previous month in each industry's employment levels. Change in employment levels is listed for 1 month, 5 months, 1 year, and 5 years. Data pulled directly from StatCan @statcan.
166224

167225
::: {.panel-tabset .nav-pills .column-page-inset}
168226

169227
### Select an Industry
170228

171229
```{python}
172230
# | output: asis
231+
plot_data = labour_processed_filtered
232+
INDUSTRY = "Total employed, all industries"
233+
234+
# use the unfiltered labour df to generate the stats for the total
235+
subtitle_text = make_subtitle_for_industry(labour, INDUSTRY)
236+
173237
plot = (
174238
ggplot(
175239
plot_data,
@@ -206,6 +270,8 @@ plot = (
206270
+ labs(
207271
x="",
208272
y="< SECTORS FALLING SECTORS RISING >",
273+
subtitle=subtitle_text,
274+
title=re.sub(r" \[.*?\]$", "", INDUSTRY),
209275
)
210276
)
211277
plot
@@ -220,7 +286,12 @@ ALL_INDUSTRIES = (
220286
for INDUSTRY in ALL_INDUSTRIES:
221287
display(Markdown(f"### {re.sub(r' \[.*?\]$', '', INDUSTRY)}"))
222288
plot_data_subsetted = plot_data.filter(pl.col("Industry") == INDUSTRY)
223-
p = plot + geom_point(data=plot_data_subsetted, color="black", fill="black")
289+
subtitle_text = make_subtitle_for_industry(labour, INDUSTRY)
290+
p = (
291+
plot
292+
+ geom_point(data=plot_data_subsetted, color="black", fill="black")
293+
+ labs(subtitle=subtitle_text, title=re.sub(r" \[.*?\]$", "", INDUSTRY))
294+
)
224295
p.show()
225296
display(Markdown(f" "))
226297
```

0 commit comments

Comments
 (0)