|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import TYPE_CHECKING, Literal |
| 4 | + |
| 5 | +from polars._utils.unstable import issue_unstable_warning |
| 6 | + |
| 7 | +if TYPE_CHECKING: |
| 8 | + from collections.abc import Collection |
| 9 | + |
| 10 | + from typing_extensions import TypeAlias |
| 11 | + |
| 12 | + |
| 13 | +FloatCastOption: TypeAlias = Literal["upcast", "downcast"] |
| 14 | +DatetimeCastOption: TypeAlias = Literal["nanosecond-downcast", "convert-timezone"] |
| 15 | + |
| 16 | + |
| 17 | +class ScanCastOptions: |
| 18 | + """Options for type-casting when scanning files.""" |
| 19 | + |
| 20 | + def __init__( |
| 21 | + self, |
| 22 | + *, |
| 23 | + integer_cast: Literal["upcast", "forbid"] = "forbid", |
| 24 | + float_cast: Literal["forbid"] |
| 25 | + | FloatCastOption |
| 26 | + | Collection[FloatCastOption] = "forbid", |
| 27 | + datetime_cast: Literal["forbid"] |
| 28 | + | DatetimeCastOption |
| 29 | + | Collection[DatetimeCastOption] = "forbid", |
| 30 | + missing_struct_fields: Literal["insert", "raise"] = "raise", |
| 31 | + extra_struct_fields: Literal["ignore", "raise"] = "raise", |
| 32 | + _internal_call: bool = False, |
| 33 | + ) -> None: |
| 34 | + """ |
| 35 | + Configuration for type-casting of columns when reading files. |
| 36 | +
|
| 37 | + This can be useful for scanning datasets with schemas that have been |
| 38 | + modified. This configuration object is generally passed to a supported |
| 39 | + `scan_*` function via the `cast_options` parameter. |
| 40 | +
|
| 41 | + .. warning:: |
| 42 | + This functionality is considered **unstable**. It may be changed |
| 43 | + at any point without it being considered a breaking change. |
| 44 | +
|
| 45 | + Parameters |
| 46 | + ---------- |
| 47 | + integer_cast |
| 48 | + Configuration for casting from integer types: |
| 49 | +
|
| 50 | + * `upcast`: Allow lossless casting to wider integer types. |
| 51 | + * `forbid`: Raises an error if dtypes do not match. |
| 52 | +
|
| 53 | + float_cast |
| 54 | + Configuration for casting from float types: |
| 55 | +
|
| 56 | + * `upcast`: Allow casting to higher precision float types. |
| 57 | + * `downcast`: Allow casting to lower precision float types. |
| 58 | + * `forbid`: Raises an error if dtypes do not match. |
| 59 | +
|
| 60 | + datetime_cast |
| 61 | + Configuration for casting from datetime types: |
| 62 | +
|
| 63 | + * `nanosecond-downcast`: Allow nanosecond precision datetime to be \ |
| 64 | + downcasted to any lower precision. This has a similar effect to \ |
| 65 | + PyArrow's `coerce_int96_timestamp_unit`. |
| 66 | + * `convert-timezone`: Allow casting to a different timezone. |
| 67 | + * `forbid`: Raises an error if dtypes do not match. |
| 68 | +
|
| 69 | + missing_struct_fields |
| 70 | + Configuration for behavior when struct fields defined in the schema |
| 71 | + are missing from the data: |
| 72 | +
|
| 73 | + * `insert`: Inserts the missing fields. |
| 74 | + * `raise`: Raises an error. |
| 75 | +
|
| 76 | + extra_struct_fields |
| 77 | + Configuration for behavior when extra struct fields outside of the |
| 78 | + defined schema are encountered in the data: |
| 79 | +
|
| 80 | + * `ignore`: Silently ignores. |
| 81 | + * `raise`: Raises an error. |
| 82 | +
|
| 83 | + """ |
| 84 | + if not _internal_call: |
| 85 | + issue_unstable_warning("ScanCastOptions is considered unstable.") |
| 86 | + |
| 87 | + self.integer_cast = integer_cast |
| 88 | + self.float_cast = float_cast |
| 89 | + self.datetime_cast = datetime_cast |
| 90 | + self.missing_struct_fields = missing_struct_fields |
| 91 | + self.extra_struct_fields = extra_struct_fields |
| 92 | + |
| 93 | + # This is called from the Rust-side, we have it so that we don't accidentally |
| 94 | + # print unstable messages. |
| 95 | + @staticmethod |
| 96 | + def _default() -> ScanCastOptions: |
| 97 | + return ScanCastOptions(_internal_call=True) |
0 commit comments