Dimensions
toolbox_pyspark.dimensions
🔗
Summary
The dimensions
module is used for checking the dimensions of pyspark
dataframe
's.
get_dims
🔗
get_dims(
dataframe: psDataFrame,
use_names: bool = True,
use_comma: bool = True,
) -> Union[
dict[str, str],
dict[str, int],
tuple[str, str],
tuple[int, int],
]
Summary
Extract the dimensions of a given dataframe
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataframe
|
DataFrame
|
The table to check. |
required |
use_names
|
bool
|
Whether or not to add |
True
|
use_comma
|
bool
|
Whether or not to add a comma |
True
|
Raises:
Type | Description |
---|---|
TypeError
|
If any of the inputs parsed to the parameters of this function are not the correct type. Uses the |
Returns:
Type | Description |
---|---|
Union[Dict[str, Union[str, int]], tuple[str, ...], tTuple[int, ...]]
|
The dimensions of the given |
Examples
Set up | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
5000
2
Names and commas | |
---|---|
1 |
|
{"rows": "5,000", "cols": "2"}
Names but no commas | |
---|---|
1 |
|
{"rows": 5000, "cols": 2}
Commas but no names | |
---|---|
1 |
|
("5,000", "2")
Neither names nor commas | |
---|---|
1 |
|
(5000, 2)
Source code in src/toolbox_pyspark/dimensions.py
80 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 131 132 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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
|
get_dims_of_tables
🔗
get_dims_of_tables(
tables: str_list,
scope: Optional[dict] = None,
use_comma: bool = True,
) -> pdDataFrame
Summary
Take in a list of the names of some tables, and for each of them, check their dimensions.
Details
This function will check against the global()
scope. So you need to be careful if you're dealing with massive amounts of data in memory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tables
|
str_list
|
The list of the tables that will be checked. |
required |
scope
|
dict
|
This is the scope against which the tables will be checked. |
None
|
use_comma
|
bool
|
Whether or not the dimensions from the tables should be formatted as a string with a comma as the thousandths delimiter. |
True
|
Raises:
Type | Description |
---|---|
TypeError
|
If any of the inputs parsed to the parameters of this function are not the correct type. Uses the |
Returns:
Type | Description |
---|---|
DataFrame
|
A |
Examples
Set up | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
{"rows": "5000", "cols": "2"}
{"rows": "10000", "cols": "3"}
Basic usage | |
---|---|
1 |
|
table type rows cols
0 df1 5,000 2
1 df2 1,000 3
No commas | |
---|---|
1 |
|
table type rows cols
0 df1 5000 2
1 df2 1000 3
Missing DF | |
---|---|
1 |
|
table type rows cols
0 df1 5000 2
1 df2 1000 3
1 df3 NaN NaN
Notes
- The first column of the returned table is the name of the table from the
scope
provided. - The second column of the returned table is the
type
of the table. That is, whether the table is one of["prd", "arc", "acm"]
, which are for 'production', 'archive', accumulation' categories. This is designated by the table containing an underscore (_
), and having a suffic of either one of:"prd"
,"arc"
, or"acm"
. If the table does not contain this info, then the value in this second column will just be blank. - If one of the tables given in the
tables
list does not exist in thescope
, then the values given in therows
andcols
columns will either be the values:np.nan
or"Did not load"
.
Source code in src/toolbox_pyspark/dimensions.py
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 234 235 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 264 265 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 319 320 321 322 323 324 325 326 327 328 329 |
|
make_dimension_table
🔗
make_dimension_table(
dataframe: psDataFrame,
columns: Union[str, str_collection],
index_prefix: str = "id",
) -> psDataFrame
Summary
Create a dimension table from the specified columns of a given pyspark
dataframe.
Details
This function will create a dimension table from the specified columns of a given pyspark
dataframe. The dimension table will contain the unique values of the specified columns, along with an index column that will be used to replace the original columns in the original dataframe.
index column will be named according to the index_prefix
parameter. If only one column is specified, then the index column will be named according to the index_prefix
parameter followed by the name of the column. If multiple columns are specified, then the index column will be named according to the index_prefix
parameter only. The index column will be created by using the row_number()
window function over the specified columns.
The dimension table will be created by selecting the specified columns from the original dataframe, then applying the distinct()
function to get the unique values, and finally applying the row_number()
window function to create the index column.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataframe
|
DataFrame
|
The DataFrame to create the dimension table from. |
required |
columns
|
Union[str, str_collection]
|
The column(s) to include in the dimension table. |
required |
index_prefix
|
str
|
The prefix to use for the index column. |
'id'
|
Raises:
Type | Description |
---|---|
TypeError
|
If any of the inputs parsed to the parameters of this function are not the correct type. Uses the |
ColumnDoesNotExistError
|
If any of the columns specified do not exist in the dataframe. |
Returns:
Type | Description |
---|---|
DataFrame
|
The dimension table. |
Examples
Set up | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
+---+---+---+---+---+
| a | b | c | d | e |
+---+---+---+---+---+
| 1 | a | 1 | a | x |
| 2 | b | 1 | b | x |
| 3 | c | 2 | b | y |
| 4 | d | 2 | b | z |
+---+---+---+---+---+
Example 1: Create dimension table with single column | |
---|---|
1 2 |
|
+------+---+
| id_d | d |
+------+---+
| 1 | a |
| 2 | b |
+------+---+
Conclusion: Successfully created dimension table with single column.
Example 2: Create dimension table with multiple columns | |
---|---|
1 2 |
|
+----+---+---+
| id | c | d |
+----+---+---+
| 1 | 1 | a |
| 2 | 1 | b |
| 3 | 2 | b |
+----+---+---+
Conclusion: Successfully created dimension table with multiple columns.
Example 3: Use different prefix | |
---|---|
1 2 |
|
+---------+---+
| index_e | e |
+---------+---+
| 1 | x |
| 2 | y |
| 3 | z |
+---------+---+
Conclusion: Successfully created dimension table with different prefix.
Example 4: Invalid column | |
---|---|
1 |
|
ColumnDoesNotExistError: Column '123' does not exist in the DataFrame.
Conclusion: Failed to create dimension table due to invalid column name.
See Also
Source code in src/toolbox_pyspark/dimensions.py
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 |
|
replace_columns_with_dimension_id
🔗
replace_columns_with_dimension_id(
fct_dataframe: psDataFrame,
dim_dataframe: psDataFrame,
cols_to_replace: Union[str, str_collection],
dim_id_col: Optional[str] = None,
) -> psDataFrame
Summary
Replace the specified columns in a given pyspark
dataframe with the corresponding dimension table IDs.
Details
This function will replace the specified columns in a given pyspark
dataframe with the corresponding dimension table IDs. The dimension table IDs will be obtained by joining the dimension table with the original dataframe on the specified columns. The original columns will then be dropped from the original dataframe.
The dimension table IDs will be added to the original dataframe to replace the columns specified in cols_to_replace
. The dimension table IDs will be obtained by joining the dimension table with the original dataframe on the specified columns.
The join will be performed using a left join, so that any rows in the original dataframe that do not have a corresponding row in the dimension table will have a null
value for the dimension table ID. The original columns will be dropped from the original dataframe after the join. The resulting dataframe will have the same number of rows as the original dataframe, but with the specified columns replaced by the dimension table IDs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fct_dataframe
|
DataFrame
|
The DataFrame to replace the columns in. |
required |
dim_dataframe
|
DataFrame
|
The dimension table containing the IDs. |
required |
cols_to_replace
|
Union[str, str_collection]
|
The column(s) to replace with the dimension table IDs. |
required |
dim_id_col
|
str
|
The name of the column in the dimension table containing the IDs. |
None
|
Raises:
Type | Description |
---|---|
TypeError
|
If any of the inputs parsed to the parameters of this function are not the correct type. Uses the |
ColumnDoesNotExistError
|
If any of the columns specified do not exist in the dataframes. |
Returns:
Type | Description |
---|---|
DataFrame
|
The DataFrame with the columns replaced by the dimension table IDs. |
Examples
Set up | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
+---+---+---+---+---+
| a | b | c | d | e |
+---+---+---+---+---+
| 1 | a | 1 | a | x |
| 2 | b | 1 | b | x |
| 3 | c | 2 | b | y |
| 4 | d | 2 | b | z |
+---+---+---+---+---+
+------+---+
| id_d | d |
+------+---+
| 1 | a |
| 2 | b |
+------+---+
+------+---+
| id_e | e |
+------+---+
| 1 | x |
| 2 | y |
| 3 | z |
+------+---+
+----+---+---+
| id | c | d |
+----+---+---+
| 1 | 1 | a |
| 2 | 1 | b |
| 3 | 2 | b |
+----+---+---+
Example 1: Replace single column with dimension ID | |
---|---|
1 2 |
|
+---+---+---+------+---+
| a | b | c | id_d | e |
+---+---+---+------+---+
| 1 | a | 1 | 1 | x |
| 2 | b | 1 | 2 | x |
| 3 | c | 2 | 2 | y |
| 4 | d | 2 | 2 | z |
+---+---+---+------+---+
Conclusion: Successfully replaced single column with dimension ID.
Example 2: Replace single column with dimension ID | |
---|---|
1 2 |
|
+---+---+---+---+------+
| a | b | c | d | id_e |
+---+---+---+---+------+
| 1 | a | 1 | a | 1 |
| 2 | b | 1 | b | 1 |
| 3 | c | 2 | b | 2 |
| 4 | d | 2 | b | 3 |
+---+---+---+---+------+
Conclusion: Successfully replaced single column with dimension ID.
Example 3: Replace multiple columns with dimension IDs | |
---|---|
1 2 |
|
+---+---+----+---+
| a | b | id | e |
+---+---+----+---+
| 1 | a | 1 | x |
| 2 | b | 2 | x |
| 3 | c | 3 | y |
| 4 | d | 3 | z |
+---+---+----+---+
Conclusion: Successfully replaced multiple columns with dimension IDs.
Example 4: Invalid column type | |
---|---|
1 |
|
ColumnDoesNotExistError: Column '123' does not exist in the DataFrame.
Conclusion: Failed to replace columns due to invalid column type.
See Also
Source code in src/toolbox_pyspark/dimensions.py
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 |
|