pandera.backends.narwhals.container.DataFrameSchemaBackendΒΆ
- class pandera.backends.narwhals.container.DataFrameSchemaBackend[source]ΒΆ
Methods
- add_missing_columns(check_obj, schema, column_info)[source]ΒΆ
Add schema columns missing from the frame.
Absent columns must either declare a default value or be nullable; otherwise an
ADD_MISSING_COLUMN_NO_DEFAULTerror is raised. Missing columns are inserted in schema order relative to the existing columns.Column construction is hybrid, mirroring the coerce path: plain numpy dtypes with a concrete default are built Narwhals-native (
nw.lit(value).cast(...)); extension dtypes (nullableInt64etc.) and null-valued columns are given their schema dtype through the pandas dtype engine, sincenw.castcannot represent them (e.g. a null integer).
- check_column_presence(check_obj, schema, column_info)[source]ΒΆ
Check that all columns in the schema are present in the dataframe.
- Return type:
list[CoreCheckResult]
- check_column_values_are_unique(check_obj, schema)[source]ΒΆ
Check that column values are unique.
- Return type:
CoreCheckResult
- check_native_column_names_unique(check_obj, schema)[source]ΒΆ
Return a
SchemaErrorif duplicate column labels are present.Mirrors the native pandas
check_column_names_are_unique. Runs on the native pandas frame (before Narwhals wrapping) because Narwhals rejects duplicate column labels at construction time. ReturnsNonewhen the check passes orunique_column_namesis not set.
- coerce_dtype(check_obj, schema)[source]ΒΆ
Coerce dtypes to the schema (Narwhals-native).
Two cases are handled:
Row-wise auto_coerce dtypes (e.g.
PydanticModel): coerced by the dtype engine itself over the whole frame β works for any backend.Column- and schema-level dtypes: coerced with
nw.castfor eager pandas-like frames. Narwhals normalizes pandas dtypes, so native pandas dtype fidelity (nullableInt64,Categorical, tz-aware datetimes) is not guaranteed. Cast failures are reported asDATATYPE_COERCIONerrors.
Column-level coercion remains a no-op for non-pandas Narwhals backends (a known gap). Accepts and returns either a Narwhals frame (validate path) or a native frame (direct
schema.coerce_dtype(df)calls).
- coerce_native_index(check_obj, schema, error_handler)[source]ΒΆ
Coerce the pandas index dtype via the native Index component.
Index/MultiIndex is the one pandas concept Narwhals cannot express, so index coercion is delegated to the native pandas Index backend. Done on the native frame before wrapping so the coerced index propagates through Narwhals (which preserves the pandas index) to the output.
- collect_schema_components(check_obj, schema, column_info)[source]ΒΆ
Collects all schema components to use for validation.
- run_checks(check_obj, schema)[source]ΒΆ
Run a list of checks on the check object.
- Return type:
list[CoreCheckResult]
- run_index_checks(check_obj, schema, lazy)[source]ΒΆ
Validate the pandas
index/MultiIndexcomponent.Delegates to the native pandas Index/MultiIndex backends (which stay registered even when the Narwhals backend is active). Narwhals preserves the pandas index through its operations, so
_to_nativeof the parsed Narwhals frame still carries the original index.For non-pandas frames this is a no-op β those schemas have no
indexcomponent, and thevalidatemethod already warned if one is present.- Return type:
list[CoreCheckResult]
- run_native_parsers(check_obj, schema)[source]ΒΆ
Run custom
schema.parserson the native pandas frame.Narwhals has no parser step, and custom parsers are arbitrary user code written against the native pandas frame. The parser function is applied directly here (self-contained β no dispatch to the pandas schema backend): dataframe-level parsers receive the whole frame, and
element_wiseparsers are applied row-wise.
- run_schema_component_checks(check_obj, schema, schema_components, lazy)[source]ΒΆ
Run checks for all schema components.
- Return type:
list[CoreCheckResult]
- set_defaults(check_obj, schema)[source]ΒΆ
Fill null values in columns that declare a default (Narwhals-native).