fsspeckit.datasets API Reference¶
datasets
¶
Dataset-level operations for fsspeckit.
This package contains dataset-specific functionality including: - DuckDB parquet handlers for high-performance dataset operations - PyArrow handlers for dataset I/O operations - PyArrow utilities for schema management and type conversion - Dataset merging and optimization tools
Classes¶
fsspeckit.datasets.DatasetError
¶
Bases: Exception
Base exception for fsspeckit dataset operations.
Attributes:
| Name | Type | Description |
|---|---|---|
message |
Human-readable error description. |
|
operation |
The operation that failed (e.g., 'read', 'write', 'merge'). |
|
details |
Additional structured context about the error. |
Source code in src/fsspeckit/core/exceptions.py
fsspeckit.datasets.DatasetFileError
¶
DatasetFileError(
message: str,
operation: str | None = None,
details: dict[str, Any] | None = None,
)
Bases: DatasetError
Raised when file I/O operations fail.
Use this for file read/write errors, permission issues, etc.
Source code in src/fsspeckit/core/exceptions.py
fsspeckit.datasets.DatasetMergeError
¶
DatasetMergeError(
message: str,
operation: str | None = None,
details: dict[str, Any] | None = None,
)
Bases: DatasetError
Raised when merge operations fail.
Use this for merge-specific failures (key column issues, schema mismatches, etc.).
Source code in src/fsspeckit/core/exceptions.py
fsspeckit.datasets.DatasetOperationError
¶
DatasetOperationError(
message: str,
operation: str | None = None,
details: dict[str, Any] | None = None,
)
Bases: DatasetError
Raised when a dataset operation fails.
Use this for general operation failures that don't fit more specific categories.
Source code in src/fsspeckit/core/exceptions.py
fsspeckit.datasets.DatasetPathError
¶
DatasetPathError(
message: str,
operation: str | None = None,
details: dict[str, Any] | None = None,
)
Bases: DatasetError
Raised when path-related operations fail.
Used for path normalization failures, missing paths, and invalid protocols surfaced through core path validation. The datasets layer preserves and enriches this error type at the public boundary.
Source code in src/fsspeckit/core/exceptions.py
fsspeckit.datasets.DatasetSchemaError
¶
DatasetSchemaError(
message: str,
operation: str | None = None,
details: dict[str, Any] | None = None,
)
Bases: DatasetError
Raised when schema-related operations fail.
Use this for schema validation, casting, and compatibility issues.
Source code in src/fsspeckit/core/exceptions.py
fsspeckit.datasets.DatasetValidationError
¶
DatasetValidationError(
message: str,
operation: str | None = None,
details: dict[str, Any] | None = None,
)
Bases: DatasetError
Raised when input validation fails.
Use this when user-provided input is invalid (e.g., invalid mode, missing columns).
Source code in src/fsspeckit/core/exceptions.py
fsspeckit.datasets.PyarrowDatasetIO
¶
Bases: BaseDatasetHandler
PyArrow-based dataset I/O operations.
This class provides methods for reading and writing parquet files and datasets using PyArrow's high-performance parquet engine.
The class inherits from BaseDatasetHandler to leverage shared implementations while providing PyArrow-specific optimizations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filesystem
|
AbstractFileSystem | None
|
Optional fsspec filesystem instance. If None, uses local filesystem. |
None
|
Example
Initialize PyArrow dataset I/O.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filesystem
|
AbstractFileSystem | None
|
Optional fsspec filesystem. If None, uses local filesystem. |
None
|
Source code in src/fsspeckit/datasets/pyarrow/io.py
Attributes¶
fsspeckit.datasets.PyarrowDatasetIO.filesystem
property
¶
Return the filesystem instance.
Functions¶
fsspeckit.datasets.PyarrowDatasetIO.__enter__
¶
__enter__() -> PyarrowDatasetIO
Enter context manager.
Provided for API symmetry with DuckDB. PyArrow has no connection to close, so this is a no-op context manager.
fsspeckit.datasets.PyarrowDatasetIO.__exit__
¶
__exit__(
exc_type: type[BaseException] | None,
exc_value: BaseException | None,
traceback: Any,
) -> None
Exit context manager (no-op for PyArrow).
fsspeckit.datasets.PyarrowDatasetIO.merge
¶
merge(
data: Table | list[Table],
path: str,
strategy: Literal["insert", "update", "upsert"],
key_columns: list[str] | str,
*,
partition_columns: list[str] | str | None = None,
schema: Schema | None = None,
compression: str | None = "snappy",
max_rows_per_file: int | None = 5000000,
row_group_size: int | None = 500000,
merge_chunk_size_rows: int = 100000,
enable_streaming_merge: bool = True,
merge_max_memory_mb: int = 1024,
merge_max_process_memory_mb: int | None = None,
merge_min_system_available_mb: int = 512,
merge_progress_callback: Callable[[int, int], None]
| None = None,
use_merge: bool | None = None,
) -> MergeResult
Merge data into an existing parquet dataset.
This method performs an incremental merge (insert, update, or upsert) of the provided data into the target dataset. It uses PyArrow's high-performance operations and supports both in-memory and streaming merge strategies.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Table | list[Table]
|
PyArrow table or list of tables to merge |
required |
path
|
str
|
Target dataset path |
required |
strategy
|
Literal['insert', 'update', 'upsert']
|
Merge strategy ('insert', 'update', or 'upsert') |
required |
key_columns
|
list[str] | str
|
Column(s) used as unique identifiers |
required |
partition_columns
|
list[str] | str | None
|
Optional column(s) to partition by |
None
|
schema
|
Schema | None
|
Optional schema to enforce on the source data |
None
|
compression
|
str | None
|
Compression codec (default: snappy) |
'snappy'
|
max_rows_per_file
|
int | None
|
Max rows per file in output (default: 5,000,000) |
5000000
|
row_group_size
|
int | None
|
Rows per row group (default: 500_000) |
500000
|
merge_chunk_size_rows
|
int
|
Rows per processing chunk (default: 100_000) |
100000
|
enable_streaming_merge
|
bool
|
Whether to use streaming merge (default: True) |
True
|
merge_max_memory_mb
|
int
|
Max PyArrow memory in MB (default: 1024) |
1024
|
merge_max_process_memory_mb
|
int | None
|
Optional max process RSS in MB |
None
|
merge_min_system_available_mb
|
int
|
Min system available memory in MB (default: 512) |
512
|
merge_progress_callback
|
Callable[[int, int], None] | None
|
Optional callback for progress updates |
None
|
use_merge
|
bool | None
|
Reserved for backward compatibility (ignored by current implementations) |
None
|
Returns:
| Type | Description |
|---|---|
MergeResult
|
MergeResult with detailed statistics |
Source code in src/fsspeckit/datasets/pyarrow/io.py
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 | |
fsspeckit.datasets.PyarrowDatasetIO.read_parquet
¶
read_parquet(
path: str,
columns: list[str] | None = None,
filters: Any | None = None,
use_threads: bool = True,
) -> Table
Read parquet file(s) using PyArrow.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Path to parquet file or directory |
required |
columns
|
list[str] | None
|
Optional list of columns to read |
None
|
filters
|
Any | None
|
Optional row filter expression. Accepts PyArrow compute expressions, DNF tuples, or SQL-like strings (converted to PyArrow expressions). Note: DuckDB backend only accepts SQL WHERE clause strings.. Accepts PyArrow compute expressions, DNF tuples, or SQL-like strings (converted to PyArrow expressions). Note: DuckDB backend only accepts SQL WHERE clause strings. |
None
|
use_threads
|
bool
|
Whether to use parallel reading (default: True) |
True
|
Returns:
| Type | Description |
|---|---|
Table
|
PyArrow table containing the data |
Example
Source code in src/fsspeckit/datasets/pyarrow/io.py
fsspeckit.datasets.PyarrowDatasetIO.write_dataset
¶
write_dataset(
data: Table | list[Table],
path: str,
*,
mode: Literal["append", "overwrite"] = "append",
basename_template: str | None = None,
schema: Schema | None = None,
partition_by: str | list[str] | None = None,
partitioning_flavor: Literal["hive", "directory"]
| None = None,
compression: str | None = "snappy",
max_rows_per_file: int | None = 5000000,
row_group_size: int | None = 500000,
) -> "WriteDatasetResult"
Write a parquet dataset and return per-file metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Table | list[Table]
|
PyArrow table or list of tables to write |
required |
path
|
str
|
Target dataset path |
required |
mode
|
Literal['append', 'overwrite']
|
Write mode - "append" or "overwrite" |
'append'
|
basename_template
|
str | None
|
Template for output filenames |
None
|
schema
|
Schema | None
|
Optional schema to enforce on the data |
None
|
partition_by
|
str | list[str] | None
|
Column(s) to partition by |
None
|
partitioning_flavor
|
Literal['hive', 'directory'] | None
|
Partitioning style - "hive" for Hive-style (col=val) or "directory" for simple directory partitioning. Default is hive when partition_by is provided. |
None
|
compression
|
str | None
|
Compression codec (default: snappy) |
'snappy'
|
max_rows_per_file
|
int | None
|
Maximum rows per output file |
5000000
|
row_group_size
|
int | None
|
Rows per row group in parquet files |
500000
|
Returns:
| Type | Description |
|---|---|
'WriteDatasetResult'
|
WriteDatasetResult with file metadata and statistics |
Source code in src/fsspeckit/datasets/pyarrow/io.py
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 | |
fsspeckit.datasets.PyarrowDatasetIO.write_parquet
¶
write_parquet(
data: Table | list[Table],
path: str,
compression: str | None = "snappy",
row_group_size: int | None = None,
use_threads: bool = False,
) -> None
Write parquet file using PyArrow.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Table | list[Table]
|
PyArrow table or list of tables to write |
required |
path
|
str
|
Output file path |
required |
compression
|
str | None
|
Compression codec to use (default: snappy) |
'snappy'
|
row_group_size
|
int | None
|
Rows per row group |
None
|
use_threads
|
bool
|
Whether to use parallel writing (ignored by PyArrow) |
False
|
Example
Source code in src/fsspeckit/datasets/pyarrow/io.py
Functions¶
fsspeckit.datasets.normalize_path
¶
Normalize path based on filesystem type.
This function now delegates to core/filesystem/paths.normalize_path to provide unified path normalization across the codebase. The delegation preserves all existing behavior while eliminating duplicate normalization logic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
The path to normalize. |
required |
filesystem
|
AbstractFileSystem
|
The filesystem instance. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The normalized path. |
Source code in src/fsspeckit/datasets/path_utils.py
fsspeckit.datasets.validate_dataset_path
¶
Comprehensive path validation for dataset operations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
The path to validate. |
required |
filesystem
|
AbstractFileSystem
|
The filesystem instance. |
required |
operation
|
str
|
The operation being performed ('read', 'write', 'merge', etc.) |
required |
Raises:
| Type | Description |
|---|---|
DatasetPathError
|
If the path is invalid for the given operation. |
Source code in src/fsspeckit/datasets/path_utils.py
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 | |
fsspeckit.datasets.collect_dataset_stats_pyarrow
¶
collect_dataset_stats_pyarrow(
path: str,
filesystem: AbstractFileSystem | None = None,
partition_filter: list[str] | None = None,
) -> dict[str, Any]
Collect file-level statistics for a parquet dataset using shared core logic.
This function delegates to the shared fsspeckit.core.maintenance.collect_dataset_stats
function, ensuring consistent dataset discovery and statistics across both DuckDB
and PyArrow backends.
The helper walks the given dataset directory on the provided filesystem, discovers parquet files (recursively), and returns basic statistics:
- Per-file path, size in bytes, and number of rows
- Aggregated total bytes and total rows
The function is intentionally streaming/metadata-driven and never
materializes the full dataset as a single :class:pyarrow.Table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Root directory of the parquet dataset. |
required |
filesystem
|
AbstractFileSystem | None
|
Optional fsspec filesystem. If omitted, a local "file" filesystem is used. |
None
|
partition_filter
|
list[str] | None
|
Optional list of partition prefix filters
(e.g. ["date=2025-11-04"]). Only files whose path relative to
|
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dict with keys: |
dict[str, Any]
|
|
dict[str, Any]
|
|
dict[str, Any]
|
|
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the path does not exist or no parquet files match the optional partition filter. |
Note
This is a thin wrapper around the shared core function. See
:func:fsspeckit.core.maintenance.collect_dataset_stats for the
authoritative implementation.
Source code in src/fsspeckit/datasets/pyarrow/dataset.py
fsspeckit.datasets.cast_schema
¶
Cast a PyArrow table to a given schema, updating the schema to match the table's columns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table
|
Table
|
The PyArrow table to cast. |
required |
schema
|
Schema
|
The target schema to cast table to. |
required |
Returns:
| Type | Description |
|---|---|
Table
|
pa.Table: A new PyArrow table with the specified schema. |
Source code in src/fsspeckit/datasets/schema.py
fsspeckit.datasets.convert_large_types_to_normal
¶
Convert large types in a PyArrow schema to their standard types.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
Schema
|
The PyArrow schema to convert. |
required |
Returns:
| Type | Description |
|---|---|
Schema
|
pa.Schema: A new PyArrow schema with large types converted to standard types. |
Source code in src/fsspeckit/datasets/schema.py
fsspeckit.datasets.opt_dtype_pa
¶
Optimize dtypes in a PyArrow table based on data analysis.
This function analyzes the data in each column and attempts to downcast to more appropriate types (e.g., int64 -> int32, float64 -> float32, string -> int/bool where applicable).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table
|
Table
|
PyArrow table to optimize |
required |
strict
|
bool
|
Whether to use strict type checking |
False
|
columns
|
list[str] | None
|
List of columns to optimize (None for all) |
None
|
Returns:
| Type | Description |
|---|---|
Table
|
Table with optimized dtypes |
Example
Source code in src/fsspeckit/datasets/schema.py
fsspeckit.datasets.unify_schemas_pa
¶
unify_schemas_pa(
schemas: list[Schema],
use_large_dtypes: bool = False,
timezone: str | None = None,
standardize_timezones: bool = True,
verbose: bool = False,
remove_conflicting_columns: bool = False,
) -> Schema
Unify a list of PyArrow schemas into a single schema using intelligent conflict resolution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schemas
|
list[Schema]
|
List of PyArrow schemas to unify. |
required |
use_large_dtypes
|
bool
|
If True, keep large types like large_string. |
False
|
timezone
|
str | None
|
If specified, standardize all timestamp columns to this timezone. If "auto", use the most frequent timezone across schemas. If None, remove timezone from all timestamp columns. |
None
|
standardize_timezones
|
bool
|
If True, standardize all timestamp columns to most frequent timezone. |
True
|
verbose
|
bool
|
If True, print conflict resolution details for debugging. |
False
|
remove_conflicting_columns
|
bool
|
If True, allows removal of columns with type conflicts as a fallback strategy instead of converting them (e.g. to string). Defaults to False. |
False
|
Returns:
| Type | Description |
|---|---|
Schema
|
pa.Schema: A unified PyArrow schema. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no schemas are provided. |
Source code in src/fsspeckit/datasets/schema.py
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 | |
fsspeckit.datasets.opt_dtype_pl
¶
opt_dtype_pl(
df: DataFrame,
include: str | list[str] | None = None,
exclude: str | list[str] | None = None,
time_zone: str | None = None,
shrink_numerics: bool = False,
allow_unsigned: bool = True,
allow_null: bool = True,
sample_size: int | None = 1024,
sample_method: SampleMethod = "first",
strict: bool = False,
*,
force_timezone: str | None = None,
) -> DataFrame
Optimize data types of a Polars DataFrame for performance and memory efficiency.
This function analyzes each column and converts it to the most appropriate data type based on content, handling string-to-type conversions and numeric type downcasting.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
The Polars DataFrame to optimize. |
required |
include
|
str | list[str] | None
|
Column(s) to include in optimization (default: all columns). |
None
|
exclude
|
str | list[str] | None
|
Column(s) to exclude from optimization. |
None
|
time_zone
|
str | None
|
Optional time zone hint during datetime parsing. |
None
|
shrink_numerics
|
bool
|
Whether to downcast numeric types when possible. |
False
|
allow_unsigned
|
bool
|
Whether to allow unsigned integer types. |
True
|
allow_null
|
bool
|
Whether to allow columns with all null values to be cast to Null type. |
True
|
sample_size
|
int | None
|
Maximum number of cleaned values to inspect for regex-based inference. Use None to inspect the entire column. |
1024
|
sample_method
|
SampleMethod
|
Which subset to inspect ( |
'first'
|
strict
|
bool
|
If True, will raise an error if any column cannot be optimized. |
False
|
force_timezone
|
str | None
|
If set, ensure all parsed datetime columns end up with this timezone. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with optimized data types. |
Source code in src/fsspeckit/datasets/polars.py
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 | |