openavmkit.data
Core data loading, processing, and enrichment.
Defines :class:SalesUniversePair (the central data structure used throughout
OpenAVMKit), loads tabular and geospatial files described in
settings.json, performs spatial joins, and orchestrates the enrichment
pipeline (basic geometry, Census, distances/proximity, OpenStreetMap streets,
spatial lag, spatial inference, building permits, Overture footprints).
A :class:SalesUniversePair (or sup) bundles two DataFrames:
- universe — every parcel in the jurisdiction, regardless of whether it has sold. Carries current characteristics.
- sales — only parcels with valid sales in the study period. Carries characteristics as they were at the time of sale.
Most public functions take or return a sup.
See Also
openavmkit.pipeline : High-level wrappers for the loading and enrichment
steps used by the notebooks.
openavmkit.cleaning : Operates on the sup after data is loaded.
SalesUniversePair
dataclass
SalesUniversePair(sales, universe)
A container for the sales and universe DataFrames, many functions operate on this data structure. This data structure is necessary because the sales and universe DataFrames are often used together and need to be passed around together. The sales represent transactions and any known data at the time of the transaction, while the universe represents the current state of all parcels. The sales dataframe specifically allows for duplicate primary parcel transaction keys, since an individual parcel may have sold multiple times. The universe dataframe forbids duplicate primary parcel keys.
Attributes:
| Name | Type | Description |
|---|---|---|
sales |
DataFrame
|
DataFrame containing sales data. |
universe |
DataFrame
|
DataFrame containing universe (parcel) data. |
copy
copy()
Create a copy of the SalesUniversePair object.
Returns:
| Type | Description |
|---|---|
SalesUniversePair
|
A new SalesUniversePair object with copied DataFrames. |
Source code in openavmkit/data.py
146 147 148 149 150 151 152 153 154 | |
limit_sales_to_keys
limit_sales_to_keys(new_sale_keys)
Update the sales DataFrame to only those that match a key in new_sale_keys
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_sale_keys
|
list[str]
|
List of sale keys to filter to |
required |
Source code in openavmkit/data.py
179 180 181 182 183 184 185 186 187 188 189 190 191 | |
set
set(key, value)
Set the sales or universe DataFrame.
Attributes:
| Name | Type | Description |
|---|---|---|
key |
str
|
Either "sales" or "universe". |
value |
DataFrame
|
The new DataFrame to set for the specified key. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If an invalid key is provided |
Source code in openavmkit/data.py
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | |
update_sales
update_sales(new_sales, allow_remove_rows)
Update the sales DataFrame with new information as an overlay without redundancy.
This function lets you push updates to "sales" while keeping it as an "overlay" that doesn't contain any redundant information.
- First we note what fields were in sales last time.
- Then we note what sales are in universe but were not in sales.
- Finally, we determine the new fields generated in new_sales that are not in the previous sales or in the universe.
- A modified version of df_sales is created with only two changes:
- Reduced to the correct selection of keys.
- Addition of the newly generated fields.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_sales
|
DataFrame
|
New sales DataFrame with updates. |
required |
allow_remove_rows
|
bool
|
If True, allows the update to remove rows from sales. If False, preserves all original rows. |
required |
Source code in openavmkit/data.py
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 | |
compute_lookback_test_size
compute_lookback_test_size(test_count, lb_size, nlb_size, floor=None, cap_ratio=None)
Decide how many test sales should come from the lookback period.
Two constraints:
- cap_ratio: lookback's test-share is capped at this multiple of the
non-lookback test-share. This is the upper bound — it prevents the lookback
period from dominating the test set when other years are available.
- floor: never less than this many lookback sales in test (capped by what's
actually available). The floor is a hard minimum: if cap_ratio would otherwise
push us below floor, floor wins and cap is silently violated. The purpose of
the floor is to guarantee enough lookback sales for a usable IAAO-style ratio
study CI.
The function returns as many lookback sales as cap_ratio and availability allow,
bumped up to floor if needed. When cap_ratio is None or there are no
non-lookback sales to compare against, the cap is disabled and the function falls
back to min(test_count, lb_size) — i.e. fill the test set from lookback.
Source code in openavmkit/data.py
4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 | |
enrich_df_streets
enrich_df_streets(df_in, settings, spacing=1.0, max_ray_length=25.0, network_buffer=500.0, verbose=False)
Enrich a GeoDataFrame with street network data.
This function enriches the input GeoDataFrame with street network data by calculating frontage, depth, distance to street, and many other related metrics, for every road vs. every parcel in the GeoDataFrame, using OpenStreetMap data.
WARNING: This function can be VERY computationally and memory intensive for large datasets and may take a long time to run.
We definitely need to work on its performance or make it easier to split into smaller chunks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df_in
|
GeoDataFrame
|
Input GeoDataFrame containing parcels. |
required |
settings
|
dict
|
Settings dictionary containing configuration for the enrichment. |
required |
spacing
|
float
|
Spacing in meters for ray casting to calculate distances to streets. Default is 1.0. |
1.0
|
max_ray_length
|
float
|
Maximum length of rays to shoot for distance calculations, in meters. Default is 25.0. |
25.0
|
network_buffer
|
float
|
Buffer around the street network to consider for distance calculations, in meters. Default is 500.0. |
500.0
|
verbose
|
bool
|
If True, prints progress information. Default is False. |
False
|
Returns:
| Type | Description |
|---|---|
GeoDataFrame
|
Enriched GeoDataFrame with additional columns for street-related metrics. |
Source code in openavmkit/data.py
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 | |
enrich_sup_spatial_lag
enrich_sup_spatial_lag(sup, settings, verbose=False)
Enrich the sales and universe DataFrames with spatial lag features.
This function calculates "spatial lag", that is, the spatially-weighted average, of the sale price and other fields, based on nearest neighbors.
For sales, the spatial lag is calculated based on the training set of sales. For non-sale characteristics, the spatial lag is calculated based on the universe parcels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sup
|
SalesUniversePair
|
SalesUniversePair containing sales and universe DataFrames. |
required |
settings
|
dict
|
Settings dictionary. |
required |
verbose
|
bool
|
If True, prints progress information. |
False
|
Returns:
| Type | Description |
|---|---|
SalesUniversePair
|
Enriched SalesUniversePair with spatial lag features. |
Source code in openavmkit/data.py
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 | |
enrich_time
enrich_time(df, time_formats, settings)
Enrich the DataFrame by converting specified time fields to datetime and deriving additional fields.
For each key in time_formats, converts the column to datetime. Then, if a field with the prefix "sale" exists, enriches the DataFrame with additional time fields (e.g., "sale_year", "sale_month", "sale_age_days").
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Input DataFrame. |
required |
time_formats
|
dict
|
Dictionary mapping field names to datetime formats. |
required |
settings
|
dict
|
Settings dictionary. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with enriched time fields. |
Source code in openavmkit/data.py
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 | |
filter_df_by_date_range
filter_df_by_date_range(df, start_date, end_date)
Filter df to rows where 'sale_date' is between start_date and end_date (inclusive). - start_date/end_date may be 'YYYY-MM-DD' strings or date/datetime/Timestamp. - Time-of-day and time zones are ignored. - Rows with missing/unparseable 'sale_date' are dropped.
Source code in openavmkit/data.py
5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 | |
get_dtypes_from_settings
get_dtypes_from_settings(settings)
Generate a dictionary mapping fields to their designated data types based on settings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settings
|
dict
|
Settings dictionary. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary of field names to data type strings. |
Source code in openavmkit/data.py
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 | |
get_field_classifications
get_field_classifications(settings)
Retrieve a mapping of field names to their classifications (land, improvement or other) as well as their types (numeric, categorical, or boolean).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settings
|
dict
|
Settings dictionary. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary mapping field names to type and class. |
Source code in openavmkit/data.py
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 | |
get_hydrated_sales_from_sup
get_hydrated_sales_from_sup(sup)
Merge the sales and universe DataFrames to "hydrate" the sales data.
The sales data represents transactions and any known data at the time of the transaction, while the universe data represents the current state of all parcels. When we merge the two sets, the sales data overrides any existing data in the universe data. This is useful for creating a "hydrated" sales DataFrame that contains all the information available at the time of the sale (it is assumed that any difference between the current state of the parcel and the state at the time of the sale is accounted for in the sales data).
If the merged DataFrame contains a "geometry" column and the original sales did not, the result is converted to a GeoDataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sup
|
SalesUniversePair
|
SalesUniversePair containing sales and universe DataFrames. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame or GeoDataFrame
|
The merged (hydrated) sales DataFrame. |
Source code in openavmkit/data.py
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 | |
get_important_field
get_important_field(settings, field_name, df=None)
Retrieve the important field name for a given field alias from settings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settings
|
dict
|
Settings dictionary. |
required |
field_name
|
str
|
Identifier for the field. |
required |
df
|
DataFrame
|
Optional DataFrame to check field existence. |
None
|
Returns:
| Type | Description |
|---|---|
str or None
|
The mapped field name if found, else None. |
Source code in openavmkit/data.py
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 | |
get_important_fields
get_important_fields(settings, df=None)
Retrieve important field names from settings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settings
|
dict
|
Settings dictionary. |
required |
df
|
DataFrame
|
Optional DataFrame to filter fields. |
None
|
Returns:
| Type | Description |
|---|---|
list[str]
|
List of important field names. |
Source code in openavmkit/data.py
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 | |
get_report_locations
get_report_locations(settings, df=None)
Retrieve report location fields from settings.
These are location fields that will be used in report breakdowns, such as for ratio studies.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settings
|
dict
|
Settings dictionary. |
required |
df
|
DataFrame
|
Optional DataFrame to filter available locations. |
None
|
Returns:
| Type | Description |
|---|---|
list[str]
|
List of report location field names. |
Source code in openavmkit/data.py
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 | |
get_sale_field
get_sale_field(settings, df=None)
Determine the appropriate sale price field ("sale_price" or "sale_price_time_adj") based on time adjustment settings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settings
|
dict
|
Settings dictionary. |
required |
df
|
DataFrame
|
Optional DataFrame to check field existence. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Field name to be used for sale price. |
Source code in openavmkit/data.py
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 | |
get_train_test_keys
get_train_test_keys(df_in, settings)
Get the training and testing keys for the sales DataFrame.
This function gets the train/test keys for each model group defined in the settings, combines them into a single mask for the sales DataFrame, and returns the keys for training and testing as numpy arrays.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df_in
|
DataFrame
|
Input DataFrame containing sales data. |
required |
settings
|
dict
|
Settings dictionary |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
A tuple containing two numpy arrays: keys_train and keys_test. - keys_train: keys for training set - keys_test: keys for testing set |
Source code in openavmkit/data.py
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 | |
get_train_test_masks
get_train_test_masks(df_in, settings)
Get the training and testing masks for the sales DataFrame.
This function gets the train/test masks for each model group defined in the settings, combines them into a single mask for the sales DataFrame, and returns the masks as pandas Series
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df_in
|
DataFrame
|
Input DataFrame containing sales data. |
required |
settings
|
dict
|
Settings dictionary |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
A tuple containing two pandas Series: mask_train and mask_test. - mask_train: boolean mask for training set - mask_test: boolean mask for testing set |
Source code in openavmkit/data.py
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 | |
get_vacant
get_vacant(df_in, settings, invert=False)
Filter the DataFrame based on the 'is_vacant' column.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df_in
|
DataFrame
|
Input DataFrame. |
required |
settings
|
dict
|
Settings dictionary. |
required |
invert
|
bool
|
If True, return non-vacant rows. |
False
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame filtered by the |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the |
Source code in openavmkit/data.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 | |
get_vacant_sales
get_vacant_sales(df_in, settings, invert=False)
Filter the sales DataFrame to return only vacant (unimproved) sales.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df_in
|
DataFrame
|
Input DataFrame. |
required |
settings
|
dict
|
Settings dictionary. |
required |
invert
|
bool
|
If True, return non-vacant (improved) sales. |
False
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with an added |
Source code in openavmkit/data.py
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 | |
load_dataframe
load_dataframe(entry, settings, verbose=False, fields_cat=None, fields_bool=None, fields_num=None)
Load a DataFrame from a file based on instructions and perform calculations and type adjustments.
Source code in openavmkit/data.py
3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 | |
process_data
process_data(dataframes, settings, verbose=False)
Process raw dataframes according to settings and return a SalesUniversePair.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataframes
|
dict[str, DataFrame]
|
Dictionary mapping keys to DataFrames. |
required |
settings
|
dict
|
Settings dictionary. |
required |
verbose
|
bool
|
If True, prints progress information. |
False
|
Returns:
| Type | Description |
|---|---|
SalesUniversePair
|
A SalesUniversePair containing processed sales and universe data. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If required merge instructions or columns are missing. |
Source code in openavmkit/data.py
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 | |
write_csv
write_csv(df, path)
Write a DataFrame to a CSV file with UTF-8 encoding and no index.
Source code in openavmkit/data.py
5454 5455 5456 5457 5458 | |
write_gpkg
write_gpkg(df, path)
Write data to a geopackage file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Data to be written |
required |
path
|
str
|
File path for saving the geopackage. |
required |
Source code in openavmkit/data.py
5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 | |
write_parquet
write_parquet(df, path)
Write data to a parquet file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Data to be written |
required |
path
|
str
|
File path for saving the parquet. |
required |
Source code in openavmkit/data.py
5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 | |
write_shapefile
write_shapefile(df, path)
Write data to a shapefile file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Data to be written |
required |
path
|
str
|
File path for saving the shapefile. |
required |
Source code in openavmkit/data.py
5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 | |
write_zipped_shapefile
write_zipped_shapefile(df, path)
Write a zipped ESRI Shapefile. Produces a single {name}.shp.zip with the shapefile parts (name.shp, .shx, .dbf, .prj, .cpg, etc.) at the ZIP root.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame or GeoDataFrame
|
Data to be written (must include a 'geometry' column and a CRS). |
required |
path
|
str
|
Destination path ending with '.shp.zip' (e.g., 'out/roads.shp.zip'). |
required |
Returns:
| Type | Description |
|---|---|
Path
|
Path to the created .shp.zip |
Source code in openavmkit/data.py
5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 | |