Frames
DataFrame
dataclass
Bases: Generic[_S_co]
Shape-bound dataframe whose operations are type checked
Source code in typol/frame.py
54 55 56 57 58 59 60 61 62 63 64 65 66 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 | |
__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
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
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
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')
Join two tables into a common shape
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
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
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
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:
- 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
Source code in typol/frame.py
with_columns(*columns)
Use the provided expressions to update existing columns in the shape
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
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 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 | |
s
property
Provides a utility alias for accessing dataframe shape columns and attributes
As Shapes only have class-level operations, pretending this is an instance is equivalent
enough for direct usage of attributes (dimensions and shape_meta). To see the shape as a
shape-type, use .shape. This works around ty's limitation of resolving Unknown for
type[S & Q] (ty's fine with type[S] & type[Q]), by lowering to the instance level
__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
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
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
join(right, *on, how='inner')
Join two tables into a common shape. The common shape must be a subclass of both original
tables. To avoid creating the common subclass use .join_transform(...)
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
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
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:
- 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
Source code in typol/lazy.py
with_columns(*columns)
Use the provided expressions to update existing columns in the shape
LazyGroupBy
dataclass
Source code in typol/lazy.py
agg(*agg)
Define the aggregating expressions to group rows in the dataframe