Frames
DataFrame
dataclass
Bases: Generic[_S_co]
Shape-bound dataframe whose operations are type checked
Source code in typol/frame.py
73 74 75 76 77 78 79 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 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 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 330 331 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 483 484 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 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 | |
s
property
Provides a utility alias for accessing frame shape columns and attributes
This is particularly useful when the shape has been constructed implicitly rather than is explicitly defined:
__dataframe__(nan_as_null=False, allow_copy=True)
Deprecated compatibility with the Dataframe Interchange Protocol
__getitem__(s)
Construct a series of the value of expr s for each row in the frame
Source code in typol/frame.py
__len__()
agg(*agg)
Define the aggregating expressions to group rows in the dataframe. Any columns not
aggregated will be treated as the group by keys, since all columns must be preserved. To
drop columns instead use transform before agg
Source code in typol/frame.py
agg_transform(shape, *agg)
Define the aggregating expressions to group rows in the dataframe. Any columns not
aggregated will be treated as the group by keys, since all columns must be preserved. To
drop columns instead use transform before agg.
This allows transforming the aggregated columns since aggregation may change types
Source code in typol/frame.py
bottom_k(k, *, by, reverse=False)
Take only the smallest k rows, using by as the key
Source code in typol/frame.py
explode(*explosions)
Take a series of list columns and create a new row for each value in the list:
accounts.explode(
Account.link_name.implode().over(Account.type).list.explode_to(Account.link_name)
)
The above will create a new row for every linked name from any account for the same type
Source code in typol/frame.py
explode_transform(shape, *explosions)
Take a series of list columns and create a new row for each value in the list
Source code in typol/frame.py
filter(*condition)
Only keep rows where the boolean conditions evaluate to True
gather(indices, *, null_on_oob=False)
For each index in the provided indices, take the row at that index
Source code in typol/frame.py
gather_every(n, offset=0)
glimpse(*, return_type=None)
group_by(*keys)
Determine a series of expressions to group the dataframe by, this should be followed by an agg to apply aggregations to the grouped frame
Source code in typol/frame.py
group_by_transform(shape, *keys)
Determine a series of expressions to group the dataframe by, this should be followed by an agg to apply aggregations to the grouped frame
Source code in typol/frame.py
head(n=5)
interpolate()
iter_dicts()
Yield each row of the frame as a dictionary of column name to value. Use iter_rows for
well-typed access
iter_raw()
Yield each row of the frame as a tuple of values. Use iter_rows for well-typed access
iter_rows()
Yield a shape-typed Row for each row in the frame. Access to fields of these rows can
be done in a well typed manner, using row[S.column], which will have the right output type
Source code in typol/frame.py
join(right, *on, how='inner', **options)
Join two tables into a common shape, the intersection of the two provided shapes:
customers.join(
purchases,
customers.s.name.on(purchases.s.customer),
customers.s.phone.on(purchases.s.billing_phone),
) # resultant frame of type Customer & Purchase
If there are conflicting columns, Polars will not be able to distinguish the results.
Explicitly distinguish two shapes with df.suffix(), particularly important for self-joins:
# Add a suffix to all the columns so they can be referred to independently
other_customers = customers.suffix()
# Join customers against itself to find ones where the names conflict
customers_with_the_same_name = customers.join(
other_customers,
# To refer to suffixed columns, do suffixed_shape(original_column)
customers.s.name.on(other_customers.s(Customer.name))
how="cross"
).filter(customer.s.phone != other_customers.s(Customer.phone))
Parameters
on : BoundDimension[S, _] Join on the same columns for the left and the right shapes based on the joint shape. The column must be available in both original shapes how : Literal["inner", "left", "right", "full", "semi", "anti", "cross", "outer"] Type of join to apply. "anti" and "semi" joins are different in that they return the left shape only **options : JoinOptions Other Polars-support join options, may vary by Polars version
Source code in typol/frame.py
join_asof(right, on, strategy='backward')
Join two tables into a common shape, by nearest
Parameters
on : BoundDimension[S, _] Join on the same columns for the left and the right shapes based on the joint shape. The column must be available in both original shapes
Source code in typol/frame.py
limit(n=5)
max()
mean()
median()
Take the median of all numeric columns in the frame, leaving other columns as null
min()
quantile(quantile, interpolation='nearest')
Take the quantileth (0.25, 0.75, etc.) value, using interpolation if there is no such
exact value
Source code in typol/frame.py
reverse()
shift(n)
Progress all rows in the frame n entries forward, so now the 0th is the nth. If negative
this would make the last one now the length-nth. Blank null rows will be inserted in
the introduced gaps, and rows at the end will fall off, being removed from the resultant
frame
Source code in typol/frame.py
suffix(suffixed=None)
Suffix the columns of the shape to distinguish them from conflicts with other shape column
names, retyping the dataframe as Suffixed[CurrentShape]:
Suffixed shapes cannot have their fields accessed directly, instead must be accessed through projecting the dimensions via the suffixed shape:
# Note, suffixed.s(...) converts an original `Customer` dimension into a suffixed dimension
suffixed[suffixed.s(Customer.name)].to_list()
This is most useful in joint/intersection shapes, or self-joins:
# Add a suffix to all the columns so they can be referred to independently
other_customers = customers.suffix()
# Join customers against itself to find ones where the names conflict
customers_with_the_same_name = customers.join(
other_customers,
# To refer to suffixed columns, do suffixed_shape(original_column)
customers.s.name.on(other_customers.s(Customer.name))
how="cross"
).filter(customer.s.phone != other_customers.s(Customer.phone))
see expr.suffix for more info on shape suffixing
Source code in typol/frame.py
sum()
tail(n=5)
to_dicts()
Return a list of the rows of the frame as a dictionary of column name to value. This is particularly useful for debugging for getting a Python object out of a Polars frame
Source code in typol/frame.py
top_k(k, *, by, reverse=False)
Take only the greatest k rows, using by as the key
Source code in typol/frame.py
transform(shape, *transforms)
Convert from one shape to another shape, using the provided expressions to map columns in the current shape to columns in the new shape:
# Transform also acts like a select, picking all `Person` columns from a `Customer`
customers.transform(Person)
# You can map any Customer column to any Person column in the meantime
customers.transform(Person, customers.s.name.str.strip_chars(), customers.s.age + 1)
# Don't use transform when staying within the same shape, just use with_columns
customers.with_columns(customers.s.name.str.strip_chars(), customers.s.age + 1)
- Any columns with the same name in both the current and new shapes without an expression mapping to them will be preserved
- Any columns in the new shape not in the original shape, and not mapped to, will throw a runtime error
- Any columns in the current shape not in the new shape will be dropped
See with_columns when not changing between shapes
Source code in typol/frame.py
var()
Take the variance value of all numeric columns in the frame, leaving all other columns as null
with_columns(*columns)
with_columns(
alias: Alias[_S_co, A, AT],
/,
*columns: EndoExpr[_S_co, Any] | Alias[_S_co, A, AT],
) -> DataFrame[Intersection[_S_co, AliasShape[A, AT]]]
Use the provided expressions to update existing columns in the shape:
customers.with_columns(
customers.s.age + 1, # Add one to their age
customers.s.name.fill_null(customers.s.phone) # Use their phone number as a backup name
)
If adding or dropping columns, use transform instead
Source code in typol/frame.py
write_csv(sink=None, mappings=None, *, include_header=True, null_marker=None, quote_style=None, float_scientific=None, float_precision=None, line_terminator='\n')
write_csv(
sink: None = None,
mappings: Mapping[BoundDimension[_S_co, Any], str]
| Sequence[BoundDimension[_S_co, Any]]
| None = None,
*,
include_header: bool = True,
null_marker: str | None = None,
quote_style: CsvQuoteStyle | None = None,
float_scientific: bool | None = None,
float_precision: int | None = None,
line_terminator: str = "\n",
) -> str
write_csv(
sink: IO[str] | str | Path | IO[bytes],
mappings: Mapping[BoundDimension[_S_co, Any], str]
| Sequence[BoundDimension[_S_co, Any]]
| None = None,
*,
include_header: bool = True,
null_marker: str | None = None,
quote_style: CsvQuoteStyle | None = None,
float_scientific: bool | None = None,
float_precision: int | None = None,
line_terminator: str = "\n",
) -> None
Output the dataframe to a file. By default, this uses the column names in the Shape. Use
mappings to select which columns to output in order and rename them, e.g.
or just provide a sequence of relevant columns in order: (Account.name, Account.broker)
Source code in typol/frame.py
write_csv_of(sink, *exprs, include_header=True, null_marker=None, quote_style=None, float_scientific=None, float_precision=None, line_terminator='\n')
Output the given expressions to a CSV. This is useful to apply a final transformation to the
dataframe (e.g. for formatting or tidying up), without having to define a new Shape.
The columns will be named based on the source shape unless renamed, use to_out("...") to
rename the columns to an arbitrary value:
``` df.write_csv_of( output_path, Account.balance.round(5).to_out("balance"), Account.closed.dt.strftime("%d/%m/%Y").to_out("Closed On") Account.code, # column will just be "code" )
Source code in typol/frame.py
GroupBy
dataclass
Source code in typol/frame.py
agg(*agg)
Define the aggregating expressions to group rows in the dataframe
enforce_shape(shape, dataframe)
Select the relevant columns from the Polars frame and strict cast them to ensure they are
typed correctly. This is effectively to project-and-assert shape
Source code in typol/frame.py
LazyFrame
dataclass
Bases: Generic[_S_co]
Shape-bound dataframe whose operations are type checked
Source code in typol/lazy.py
67 68 69 70 71 72 73 74 75 76 77 78 79 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 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 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 330 331 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 483 484 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 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 | |
s
property
Provides a utility alias for accessing frame shape columns and attributes
This is particularly useful when the shape has been constructed implicitly rather than is explicitly defined:
__getitem__(s)
Construct a lazy series of values transformed by s for each row in the frame
agg(*agg)
Define the aggregating expressions to group rows in the dataframe. Any columns not
aggregated will be treated as the group by keys, since all columns must be preserved. To
drop columns instead use transform before agg
Source code in typol/lazy.py
agg_transform(shape, *exprs)
Define the aggregating expressions to group rows in the dataframe. Any columns not
aggregated will be treated as the group by keys, since all columns must be preserved. To
drop columns instead use transform before agg.
This allows transforming the aggregated columns since aggregation may change types
Source code in typol/lazy.py
bottom_k(k, *, by, reverse=False)
Take only the smallest k rows, using by as the key
Source code in typol/lazy.py
explode(*explosions)
Take a series of list columns and create a new row for each value in the list:
accounts.explode(
Account.link_name.implode().over(Account.type).list.explode_to(Account.link_name)
)
The above will create a new row for every linked name from any account for the same type
Source code in typol/lazy.py
explode_transform(shape, *explosions)
Take a series of list columns and create a new row for each value in the list
Source code in typol/lazy.py
filter(*condition)
Only keep rows where the boolean conditions evaluate to True
first()
gather(indices, *, null_on_oob=False)
For each index in the provided indices, take the row at that index
Source code in typol/lazy.py
gather_every(n, offset=0)
group_by(*keys)
Determine a series of expressions to group the dataframe by, this should be followed by an agg to apply aggregations to the grouped frame
Source code in typol/lazy.py
group_by_transform(shape, *keys)
Determine a series of expressions to group the dataframe by, this should be followed by an agg to apply aggregations to the grouped frame
Source code in typol/lazy.py
head(n=5)
interpolate()
join(right, *on, how='inner', **options)
join(
right: LazyFrame[Q],
*on: JoinOnable[_S_co, Q],
how: JoinTogetherType = "inner",
**options: Unpack[JoinOptions],
) -> LazyFrame[Intersection[_S_co, Q]]
Join two tables into a common shape, the intersection of the two provided shapes:
customers.join(
purchases,
customers.s.name.on(purchases.s.customer),
customers.s.phone.on(purchases.s.billing_phone),
) # resultant frame of type Customer & Purchase
If there are conflicting columns, Polars will not be able to distinguish the results.
Explicitly distinguish two shapes with df.suffix(), particularly important for self-joins:
# Add a suffix to all the columns so they can be referred to independently
other_customers = customers.suffix()
# Join customers against itself to find ones where the names conflict
customers_with_the_same_name = customers.join(
other_customers,
# To refer to suffixed columns, do suffixed_shape(original_column)
customers.s.name.on(other_customers.s(Customer.name))
how="cross"
).filter(customer.s.phone != other_customers.s(Customer.phone))
Parameters
on : BoundDimension[S, _] Join on the same columns for the left and the right shapes based on the joint shape. The column must be available in both original shapes how : Literal["inner", "left", "right", "full", "semi", "anti", "cross", "outer"] Type of join to apply. "anti" and "semi" joins are different in that they return the left shape only **options : JoinOptions Other Polars-support join options, may vary by Polars version
Source code in typol/lazy.py
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 | |
join_asof(right, on, strategy='backward')
Join two tables into a common shape, by nearest
Parameters
on : BoundDimension[S, _] Join on the same columns for the left and the right shapes based on the joint shape. The column must be available in both original shapes
Source code in typol/lazy.py
last()
limit(n=5)
max()
mean()
median()
Take the median of all numeric columns in the frame, leaving other columns as null
min()
quantile(quantile, interpolation='nearest')
Take the quantileth (0.25, 0.75, etc.) value, using interpolation if there is no such
exact value
Source code in typol/lazy.py
reverse()
shift(n)
Progress all rows in the frame n entries forward, so now the 0th is the nth. If negative
this would make the last one now the length-nth. Blank null rows will be inserted in
the introduced gaps, and rows at the end will fall off, being removed from the resultant
frame
Source code in typol/lazy.py
suffix(suffixed=None)
Suffix the columns of the shape to distinguish them from conflicts with other shape column
names, retyping the dataframe as Suffixed[CurrentShape]:
Suffixed shapes cannot have their fields accessed directly, instead must be accessed through projecting the dimensions via the suffixed shape:
# Note, suffixed.s(...) converts an original `Customer` dimension into a suffixed dimension
suffixed[suffixed.s(Customer.name)].collect().to_list()
This is most useful in joint/intersection shapes, or self-joins:
# Add a suffix to all the columns so they can be referred to independently
other_customers = customers.suffix()
# Join customers against itself to find ones where the names conflict
customers_with_the_same_name = customers.join(
other_customers,
# To refer to suffixed columns, do suffixed_shape(original_column)
customers.s.name.on(other_customers.s(Customer.name))
how="cross"
).filter(customer.s.phone != other_customers.s(Customer.phone))
see expr.suffix for more info on shape suffixing
Source code in typol/lazy.py
sum()
tail(n=5)
top_k(k, *, by, reverse=False)
Take only the greatest k rows, using by as the key
Source code in typol/lazy.py
transform(shape, *transforms)
Convert from one shape to another shape, using the provided expressions to map columns in the current shape to columns in the new shape:
# Transform also acts like a select, picking all `Person` columns from a `Customer`
customers.transform(Person)
# You can map any Customer column to any Person column in the meantime
customers.transform(Person, customers.s.name.str.strip_chars(), customers.s.age + 1)
# Don't use transform when staying within the same shape, just use with_columns
customers.with_columns(customers.s.name.str.strip_chars(), customers.s.age + 1)
- Any columns with the same name in both the current and new shapes without an expression mapping to them will be preserved
- Any columns in the new shape not in the original shape, and not mapped to, will throw a runtime error
- Any columns in the current shape not in the new shape will be dropped
See with_columns when not changing between shapes
Source code in typol/lazy.py
var()
Take the variance value of all numeric columns in the frame, leaving all other columns as null
with_columns(*columns)
with_columns(
alias: Alias[_S_co, A, AT],
/,
*columns: EndoExpr[_S_co, Any] | Alias[_S_co, A, AT],
) -> LazyFrame[Intersection[_S_co, AliasShape[A, AT]]]
Use the provided expressions to update existing columns in the shape:
customers.with_columns(
customers.s.age + 1, # Add one to their age
customers.s.name.fill_null(customers.s.phone) # Use their phone number as a backup name
)
If adding or dropping columns, use transform instead
Source code in typol/lazy.py
LazyGroupBy
dataclass
Source code in typol/lazy.py
agg(*agg)
Define the aggregating expressions to group rows in the dataframe