API Reference
Note: This page is auto-generated using
mkdocstrings. If you are viewing this on GitHub, please visit the Online Documentation to see the full API reference.
Core
varframe.VarFrame
Bases: DataFrame
A pandas DataFrame subclass with variable metadata and helper methods.
VarFrame behaves exactly like a pandas DataFrame, but also
maintains a registry of variable class definitions. This enables:
- Filtering columns by variable type (BaseVariable vs DerivedVariable)
- Accessing variable metadata and descriptions
- Indexing by variable class (e.g., vf[Gap])
Attributes:
| Name | Type | Description |
|---|---|---|
_variables |
List[Type]
|
List of variable classes (stored in _metadata). |
Example
vf = VarFrame(df_raw, [Lap, Gap, GapDelta]) vf.head() # Normal DataFrame operations work vf.filter_by_type(DerivedVariable) # Variable-aware filtering vf[Gap] # Access by variable class
Source code in varframe/dataframe.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 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 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 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 | |
df_raw
property
writable
Get the raw DataFrame (if stored).
variables
property
writable
Get the list of variable classes.
add_variable(*variables, compute=True, suppress_warnings=False)
Alias for add_variables.
Source code in varframe/dataframe.py
611 612 613 614 615 616 617 618 619 620 621 622 | |
add_variables(*variables, compute=True, suppress_warnings=False)
Register and optionally compute new variables (in-place).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*variables
|
VariableType
|
Variable classes to add. |
()
|
compute
|
bool
|
If True (default), compute variables immediately. If False, just register them (must already exist in DataFrame). |
True
|
suppress_warnings
|
bool
|
If True, suppress warnings for this call only. |
False
|
Returns:
| Type | Description |
|---|---|
VarFrame
|
Self, for method chaining. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If compute=True and dependencies are missing. |
RuntimeError
|
If implicit usage is disabled in VFConfig. |
ValueError
|
If compute=True and adding a BaseVariable (must come from raw). |
Source code in varframe/dataframe.py
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 | |
describe_variables()
Generate a summary DataFrame describing all variables.
Returns:
| Type | Description |
|---|---|
DataFrame
|
A DataFrame with columns: name, type, dtype, description, etc. |
Source code in varframe/dataframe.py
217 218 219 220 221 222 223 224 225 | |
explain_calculation(target_variables, legend=False)
Explain the calculation plan for the given variables given the current DataFrame state.
Prints a color-coded list indicating: - Variable Type (Base, Derived, Model) - Calculation Status (Ready vs Needs Calculation) - Warnings (Implicit computation, Auto-training, Inference)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_variables
|
VariableList
|
List of variable classes to explain. |
required |
legend
|
bool
|
If True, print a legend for the warning codes. |
False
|
Source code in varframe/dataframe.py
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 | |
filter_by_type(variable_type)
Filter to include only columns of a specific variable type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
variable_type
|
Type[Union[BaseVariable, DerivedVariable]]
|
The base class to filter by (BaseVariable or DerivedVariable). |
required |
Returns:
| Type | Description |
|---|---|
VarFrame
|
A new VarFrame containing only columns whose variables |
VarFrame
|
are subclasses of the specified type. |
Example
derived_df = vf.filter_by_type(DerivedVariable) base_df = vf.filter_by_type(BaseVariable)
Source code in varframe/dataframe.py
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | |
from_pandas(df, variables=None, df_raw=None, name='varframe')
classmethod
Create a VarFrame from a plain pandas DataFrame.
Use this to re-wrap a DataFrame after ML operations or when loading data that was previously converted with to_pandas().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
The pandas DataFrame to convert (already processed data). |
required |
variables
|
Optional[VariableList]
|
List of variable classes to associate with the DataFrame. |
None
|
df_raw
|
Optional[DataFrame]
|
Optional raw DataFrame to store for future resolve() calls. |
None
|
Returns:
| Type | Description |
|---|---|
VarFrame
|
A new VarFrame with the given data and variables. |
Example
plain_df = pd.read_pickle("data.pkl") vf = VarFrame.from_pandas(plain_df, variables=[Lap, Gap])
Source code in varframe/dataframe.py
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 | |
get_variable(name)
Retrieve a variable class by its name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the variable to retrieve. |
required |
Returns:
| Type | Description |
|---|---|
Optional[VariableType]
|
The variable class if found, None otherwise. |
Source code in varframe/dataframe.py
193 194 195 196 197 198 199 200 201 202 203 204 205 206 | |
list_variables()
Get a list of all variable names.
Returns:
| Type | Description |
|---|---|
List[str]
|
List of variable names in order. |
Source code in varframe/dataframe.py
208 209 210 211 212 213 214 215 | |
load_csv(path_or_buf, variables=None, exclude=None, discard_unmatched=True, ambiguity=None, **kwargs)
classmethod
Load a VarFrame from a CSV file, automatically discovering variables.
This method scans the current Python environment for variable definitions that match the columns in the CSV.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path_or_buf
|
Union[str, Any]
|
Path to the CSV file. |
required |
variables
|
Optional[List[VariableType]]
|
Whitelist of variable classes to load. If provided, only these variables will be matched. Acts as implicit disambiguation. |
None
|
exclude
|
Optional[List[VariableType]]
|
Blacklist of variable classes to exclude from loading.
Cannot be used together with |
None
|
discard_unmatched
|
bool
|
If True (default), columns not matching any known variable are dropped. If False, they are kept as plain columns. |
True
|
ambiguity
|
Optional[Dict[str, VariableType]]
|
Dict mapping variable names to specific classes for disambiguation when multiple definitions exist with the same name. |
None
|
**kwargs
|
Any
|
Arguments passed to pd.read_csv. |
{}
|
Returns:
| Type | Description |
|---|---|
VarFrame
|
A reconstructed VarFrame. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If both |
AmbiguityError
|
If multiple variable definitions match a column name and no disambiguation is provided. |
Source code in varframe/dataframe.py
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 | |
load_parquet(path, variables=None, exclude=None, discard_unmatched=True, ambiguity=None, **kwargs)
classmethod
Load a VarFrame from a Parquet file, using metadata or auto-discovery.
- Checks for 'varframe_variables' metadata in the file.
- If found, looks up those variables in the environment.
- If not found, falls back to matching column names (like load_csv).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Union[str, Any]
|
Path to the Parquet file. |
required |
variables
|
Optional[List[VariableType]]
|
Whitelist of variable classes to load. If provided, only these variables will be matched. Acts as implicit disambiguation and overrides hash-based resolution. |
None
|
exclude
|
Optional[List[VariableType]]
|
Blacklist of variable classes to exclude from loading.
Cannot be used together with |
None
|
discard_unmatched
|
bool
|
If True (default), columns not matching any known variable are dropped. If False, they are kept as plain columns. |
True
|
ambiguity
|
Optional[Dict[str, VariableType]]
|
Dict mapping variable names to specific classes for disambiguation. Overrides hash-based resolution for specified names. |
None
|
**kwargs
|
Any
|
Arguments passed to pyarrow/pandas read functions. |
{}
|
Returns:
| Type | Description |
|---|---|
VarFrame
|
A reconstructed VarFrame. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If both |
Source code in varframe/dataframe.py
978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 | |
resolve(*target_variables, suppress_warnings=False)
Resolve and compute target variables with automatic dependency resolution.
This method automatically determines all missing dependencies for the target variables, computes them in the correct DAG order, and adds them to the DataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*target_variables
|
VariableType
|
Variable classes to resolve and compute. |
()
|
suppress_warnings
|
bool
|
If True, suppress warnings for this call only. |
False
|
Returns:
| Type | Description |
|---|---|
VarFrame
|
Self, for method chaining. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If circular dependencies are detected. |
ValueError
|
If a BaseVariable is needed but df_raw is not available. |
RuntimeError
|
If implicit operations are disabled in VFConfig. |
Example
vf = VarFrame(df_raw, [Lap, Gap]) vf.resolve(PredictedGapDelta)
Source code in varframe/dataframe.py
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 | |
to_csv(path_or_buf=None, *args, include=None, variables=None, **kwargs)
Write object to a comma-separated values (csv) file.
Enhancements over pandas.to_csv:
- Supports include and variables to compute lazy variables on-the-fly.
- Warns if registered variables are not included in the export.
- Defaults to {self.name}.csv if path is not provided.
Source code in varframe/dataframe.py
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 | |
to_ml()
Alias for to_pandas(). Explicit conversion for ML pipelines.
Use this to clearly indicate the DataFrame is being prepared for machine learning operations.
Returns:
| Type | Description |
|---|---|
DataFrame
|
A plain pandas DataFrame suitable for ML libraries. |
Example
X_train = vf.to_ml() model.fit(X_train, y_train)
Source code in varframe/dataframe.py
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 | |
to_pandas()
Convert to a plain pandas DataFrame.
Use this before passing to ML libraries that may have issues with DataFrame subclasses (e.g., pickle, joblib, some sklearn pipelines).
Returns:
| Type | Description |
|---|---|
DataFrame
|
A plain pandas DataFrame with the same data (no variable metadata). |
Example
plain_df = vf.to_pandas() model.fit(plain_df, y)
Source code in varframe/dataframe.py
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 | |
to_parquet(path=None, *args, include=None, variables=None, **kwargs)
Write object to a binary parquet file.
Enhancements over pandas.to_parquet:
- Supports include and variables to compute lazy variables on-the-fly.
- Warns if registered variables are not included in the export.
- Defaults to {self.name}.parquet if path is not provided.
- Saves variable names in file metadata.
Source code in varframe/dataframe.py
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 | |
view(include=None, variables=None)
Create a DataFrame view containing only specific variables.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
include
|
Optional[List[str]]
|
List of variable categories to include. Options: 'base', 'derived', 'lazy', 'model', 'all'. Defaults to ['all'] if both include and variables are None. |
None
|
variables
|
Optional[VariableList]
|
Explicit list of variable classes to include. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
A pandas DataFrame with the requested variables. |
DataFrame
|
Lazy variables will be computed on-demand for this view. |
Example
vf.view(include=['base', 'lazy']) vf.view(variables=[LazySum])
Source code in varframe/dataframe.py
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 | |
Variables
varframe.BaseVariable
A declarative variable that maps to a column in a raw DataFrame.
Define subclasses with class attributes to create variable definitions. The class itself represents the variable - no instantiation needed.
Class Attributes
name (str): The name of the variable in the processed DataFrame. raw_column (str): The column name in the raw DataFrame to extract. dtype (str): The pandas dtype to cast the column to. Defaults to 'float'.
Note
Use the class docstring as the variable description.
Example
class LapNumber(BaseVariable): ... '''The current lap number in the race.''' ... name = "lap" ... raw_column = "lap_num" ... dtype = "int" ...
Use the class directly, not an instance
series = LapNumber.compute(raw_dataframe)
Source code in varframe/variables.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 | |
compute(df_raw)
classmethod
Extract and transform the column from a raw DataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df_raw
|
DataFrame
|
The raw DataFrame containing the source column. |
required |
Returns:
| Type | Description |
|---|---|
Series
|
A pandas Series with the extracted column cast to the specified dtype. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If |
ValueError
|
If the column cannot be cast to the specified |
Source code in varframe/variables.py
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | |
get_hash_components()
classmethod
Get the hash components of the variable definition.
Returns:
| Type | Description |
|---|---|
Dict[str, str]
|
Dict with keys: calc, deps, attrs, meta. |
Source code in varframe/variables.py
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 | |
info()
classmethod
Get variable metadata as a dictionary.
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dict with name, type, raw_column, dtype, and description. |
Source code in varframe/variables.py
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | |
varframe.DerivedVariable
A declarative variable computed from other variables.
Define subclasses with class attributes and a calculate() classmethod
to create computed variable definitions. Dependencies are resolved
automatically using the DataFrame as the single source of truth.
Class Attributes
name (str): The name of the variable in the processed DataFrame. dependencies (List[Type]): List of variable classes this depends on. dtype (str): The pandas dtype for the result. Defaults to 'float'. lazy (bool): If True, computed on access and not stored in DataFrame. Defaults to False.
Note
- Use the class docstring as the variable description.
- Override the
calculate()classmethod to define computation logic. - Access dependencies directly from df (e.g.,
df["gap"]), not memo.
Example
class GapDelta(DerivedVariable): ... '''Change in gap from previous measurement.''' ... name = "gap_delta" ... dependencies = [Gap] ... ... @classmethod ... def calculate(cls, df: pd.DataFrame) -> pd.Series: ... return df["gap"] - df["gap"].shift(1)
Source code in varframe/variables.py
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 | |
calculate(df)
classmethod
Calculate the derived variable's values.
Override this method in subclasses to define the computation logic.
Access dependency values directly from df (e.g., df["gap"]).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
The current processed DataFrame containing all computed variables so far (including dependencies). |
required |
Returns:
| Type | Description |
|---|---|
Series
|
A pandas Series with the computed values. |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If not overridden in subclass. |
Source code in varframe/variables.py
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | |
compute(df)
classmethod
Compute the derived variable from the DataFrame.
Dependencies must already exist in df before calling this method.
Use VarFrame to handle dependency ordering automatically.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
The DataFrame containing all dependency columns. |
required |
Returns:
| Type | Description |
|---|---|
Series
|
A pandas Series containing the computed values. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If a dependency column is missing from df. |
Source code in varframe/variables.py
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 | |
get_hash_components()
classmethod
Get the hash components of the variable definition.
Returns:
| Type | Description |
|---|---|
Dict[str, str]
|
Dict with keys: calc, deps, attrs, meta. |
Source code in varframe/variables.py
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 | |
info()
classmethod
Get variable metadata as a dictionary.
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dict with name, type, dependencies, dtype, and description. |
Source code in varframe/variables.py
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 | |
Machine Learning
varframe.BaseModel
A declarative base class for defining ML models.
Define subclasses with class attributes to specify input features, target variable, model type, and hyperparameters.
Class Attributes
name (str): Unique identifier for the model. input_vars (List[VariableType]): Variables used as features (X). target_var (VariableType): Variable to predict (y). model_class (Type): The model class (e.g., RandomForestRegressor). model_params (Dict): Hyperparameters passed to model_class(). model (Any): The trained model instance (set after training). is_trained (bool): Whether the model has been trained.
Example
class GapPredictor(BaseModel): ... name = "gap_predictor" ... input_vars = [Lap, Gap, TireAge] ... target_var = GapDelta ... model_class = RandomForestRegressor ... model_params = {"n_estimators": 100}
Source code in varframe/models.py
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 | |
evaluate(vf, metrics=None)
classmethod
Evaluate model performance.
Source code in varframe/models.py
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 | |
get_input_names()
classmethod
Get list of input variable names.
Source code in varframe/models.py
115 116 117 118 | |
get_target_name()
classmethod
Get target variable name.
Source code in varframe/models.py
120 121 122 123 | |
info()
classmethod
Get model metadata as a dictionary.
Source code in varframe/models.py
243 244 245 246 247 248 249 250 251 252 253 254 | |
load(path)
classmethod
Load a trained model from disk.
Source code in varframe/models.py
274 275 276 277 278 279 280 281 | |
predict(vf, add_to_df=True, column_name=None)
classmethod
Generate predictions.
Source code in varframe/models.py
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | |
save(path)
classmethod
Save the trained model to disk.
Source code in varframe/models.py
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | |
train(vf, **fit_kwargs)
classmethod
Train the model on the provided data.
Source code in varframe/models.py
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | |
train_with_validation(train_vf, val_vf, **fit_kwargs)
classmethod
Train with a validation set (for XGBoost, LightGBM, etc.).
Source code in varframe/models.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 | |
varframe.ModelVariable
Bases: DerivedVariable
A derived variable computed via model inference.
Class Attributes
name (str): The name of the prediction variable. model_class (Type[BaseModel]): The model class to use for predictions. dependencies: Auto-populated from model's input_vars.
Example
class PredictedGap(ModelVariable): ... name = "predicted_gap" ... model_class = GapPredictor
Source code in varframe/models.py
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 | |
calculate(df, suppress_warnings=False)
classmethod
Calculate predictions using the associated model.
If the model is not trained, it will be automatically trained using the available data in the DataFrame (with a warning).
Source code in varframe/models.py
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 | |
info()
classmethod
Get variable metadata including model info.
Source code in varframe/models.py
381 382 383 384 385 386 387 | |
varframe.ModelRegistry
Central registry for managing multiple models.
Example
registry = ModelRegistry() registry.register(GapPredictor) registry.train_all(training_vf) registry.save_all("models/")
Source code in varframe/models.py
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 | |
describe()
Generate a summary DataFrame of all registered models.
Source code in varframe/models.py
469 470 471 472 | |
evaluate_all(vf)
Evaluate all trained models.
Source code in varframe/models.py
439 440 441 442 443 444 445 446 447 | |
get(name)
Get a model class by name.
Source code in varframe/models.py
412 413 414 | |
list_models()
Get list of registered model names.
Source code in varframe/models.py
416 417 418 | |
load_all(directory)
Load all models from a directory.
Source code in varframe/models.py
460 461 462 463 464 465 466 467 | |
register(model_class)
Register a model class.
Source code in varframe/models.py
408 409 410 | |
save_all(directory)
Save all trained models to a directory.
Source code in varframe/models.py
449 450 451 452 453 454 455 456 457 458 | |
train_all(vf, models=None)
Train all registered models.
Source code in varframe/models.py
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 | |
Dependencies
varframe.resolve_dependencies(target_variables, include_model_training_deps=True)
Resolve all dependencies for the given variables using DAG traversal.
Performs a topological sort to determine the correct computation order, ensuring all dependencies are computed before their dependents.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_variables
|
'VariableList'
|
List of variable classes to compute. |
required |
include_model_training_deps
|
bool
|
If True, includes dependencies needed for model training (target_var of models). |
True
|
Returns:
| Type | Description |
|---|---|
'VariableList'
|
A topologically sorted list of all variables needed to compute |
'VariableList'
|
the target variables, with dependencies before dependents. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a circular dependency is detected. |
Example
Only specify final variables - dependencies auto-resolved!
all_vars = resolve_dependencies([PredictedGapDelta])
Returns: [Lap, Gap, TireAge, GapDelta, PredictedGapDelta]
Source code in varframe/dependencies.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 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 | |
Configuration
varframe.VFConfig
Global configuration for VarFrame warnings and implicit operations.
Controls whether implicit operations (like auto-training models or computing variables not in _variables) issue warnings or are blocked entirely.
This is a static configuration class - all attributes are class-level.
Class Attributes
warn_add_variable_no_compute (bool): Warn when adding variable without computing. warn_add_variable_compute (bool): Warn when computing variable not in _variables. warn_train_model (bool): Warn when auto-training a model. warn_infer_model (bool): Warn when inferring with a model implicitly. allow_implicit_train (bool): Allow implicit model training. allow_implicit_infer (bool): Allow implicit model inference. allow_implicit_compute (bool): Allow implicit variable computation. warnings_enabled (bool): Master switch for all warnings.
Example
Disable all warnings
VFConfig.warnings_enabled = False
Block implicit model training (raises RuntimeError)
VFConfig.allow_implicit_train = False
Suppress specific warning type
VFConfig.warn_train_model = False
Context manager for temporary suppression
with VFConfig.suppress_warnings(): ... vf.resolve(PredictedGapDelta)
Reset to defaults
VFConfig.reset()
Source code in varframe/config.py
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 | |
check_permission(operation, context='')
classmethod
Check if an implicit operation is allowed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operation
|
ImplicitOperation
|
The type of implicit operation. |
required |
context
|
str
|
Additional context for the error message. |
''
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the operation is not allowed. |
Source code in varframe/config.py
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 | |
null_context()
classmethod
Returns a context manager that does nothing.
Source code in varframe/config.py
211 212 213 214 215 216 | |
reset()
classmethod
Reset all configuration to default values.
Source code in varframe/config.py
198 199 200 201 202 203 204 205 206 207 208 209 | |
suppress_warnings()
classmethod
Context manager for temporarily suppressing all VF warnings.
Returns:
| Type | Description |
|---|---|
'_WarningSuppressionContext'
|
A context manager that suppresses warnings while active. |
Example
with VFConfig.suppress_warnings(): ... vf.resolve(PredictedGapDelta) # No warnings issued
Source code in varframe/config.py
184 185 186 187 188 189 190 191 192 193 194 195 196 | |
warn(operation, message, stacklevel=3)
classmethod
Issue a warning for an implicit operation if configured.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operation
|
ImplicitOperation
|
The type of implicit operation. |
required |
message
|
str
|
The warning message. |
required |
stacklevel
|
int
|
Stack level for the warning (default 3 for typical call depth). |
3
|
Source code in varframe/config.py
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 | |
varframe.ImplicitOperation
Bases: Enum
Enum for different implicit operations that can be warned or blocked.
These represent operations that happen automatically in the library, which users may want to be notified about or prevent entirely.
Attributes:
| Name | Type | Description |
|---|---|---|
ADD_VARIABLE_NO_COMPUTE |
Adding a variable to registry without computing it. |
|
ADD_VARIABLE_COMPUTE |
Computing a variable not explicitly in the registry. |
|
TRAIN_MODEL |
Automatically training a model that hasn't been trained. |
|
INFER_MODEL |
Performing inference with a model. |
Source code in varframe/config.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | |