stats_can.sc
Functionality that extends on what the base StatsCan api returns in some way.
Todo
Function to delete tables
Extend getChangedCubeList with a function that returns all tables updated within a date range
get_tables_for_vectors(vectors: str | list[str]) -> dict[str | int, str | list[str]]
Get a list of dicts mapping vectors to tables.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vectors
|
str | list[str]
|
Vectors to find tables for |
required |
Returns:
| Type | Description |
|---|---|
dict[str | int, str | list[str]]
|
keys for each vector number return the table, plus a key for 'all_tables' that has a list of unique tables used by vectors |
Source code in src/stats_can/sc.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | |
table_subsets_from_vectors(vectors: str | list[str]) -> dict[str, list[str]]
Get a list of dicts mapping tables to vectors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vectors
|
str | list[str]
|
Vectors to find tables for |
required |
Returns:
| Type | Description |
|---|---|
dict[str, list[str]]
|
keys for each table used by the vectors, matched to a list of vectors |
Source code in src/stats_can/sc.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | |
download_tables(tables: str | list[str], path: pathlib.Path | None = None, csv: bool = True) -> list[int]
Download a json file and zip of data for a list of tables to path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tables
|
str | list[str]
|
tables to be downloaded |
required |
path
|
Path | None
|
Where to download the table and json |
None
|
csv
|
bool
|
download in CSV format, if not download SDMX |
True
|
Returns:
| Type | Description |
|---|---|
list[int]
|
list of tables that were downloaded |
Source code in src/stats_can/sc.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | |
zip_update_tables(path: pathlib.Path | None = None, csv: bool = True) -> list[str]
Check local json, update zips of outdated tables.
Grabs the json files in path, checks them against the metadata on StatsCan and grabs updated tables where there have been changes There isn't actually a "last modified date" part to the metadata What I'm doing is comparing the latest reference period. Almost all data changes will at least include incremental releases, so this should capture what I want
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | None
|
where to look for tables to update |
None
|
csv
|
bool
|
Downloads updates in CSV form by default, SDMX if false |
True
|
Returns:
| Type | Description |
|---|---|
list[str]
|
list of the tables that were updated |
Source code in src/stats_can/sc.py
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | |
zip_table_to_dataframe(table: str, path: pathlib.Path | None = None) -> pd.DataFrame
Read a StatsCan table into a pandas DataFrame.
If a zip file of the table does not exist in path, downloads it
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table
|
str
|
the table to load to dataframe from zipped csv |
required |
path
|
Path | None
|
where to download the tables or load them, default will go to current working directory |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
the table as a dataframe |
Source code in src/stats_can/sc.py
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | |
list_zipped_tables(path: pathlib.Path | None = None) -> list[str]
List StatsCan tables available.
defaults to looking in the current working directory and for zipped CSVs
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | None
|
Where to look for zipped tables, defaults to current working directory |
None
|
Returns:
| Type | Description |
|---|---|
list[str]
|
list of available tables json data |
Source code in src/stats_can/sc.py
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 | |
vectors_to_df(vectors: str | list[str], periods: int = 1, start_release_date: dt.date | None = None, end_release_date: dt.date | None = None) -> pd.DataFrame
Get DataFrame of vectors with n periods data or over range of release dates.
Wrapper on get_bulk_vector_data_by_range and get_data_from_vectors_and_latest_n_periods function to turn the resulting list of JSONs into a DataFrame
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vectors
|
str | list[str]
|
vector numbers to get info for |
required |
periods
|
int
|
number of periods to retrieve data for |
1
|
start_release_date
|
date | None
|
start release date for the data |
None
|
end_release_date
|
date | None
|
end release date for the data |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
vectors as columns and ref_date as the index (not release date) |
Source code in src/stats_can/sc.py
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 | |
code_sets_to_df_dict() -> dict[str, pd.DataFrame]
Get all code sets.
Code sets provide additional metadata to describe information. Code sets are grouped into scales, frequencies, symbols etc. and returned as dictionary of dataframes.
Returns:
| Type | Description |
|---|---|
dict[str, DataFrame]
|
dictionary of dataframes |
Source code in src/stats_can/sc.py
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 | |