Skip to content
This repository has been archived by the owner on Nov 2, 2023. It is now read-only.

Maintain Pyndantic's Class Config Option for extras=ignore #59

Open
ethunk opened this issue Mar 17, 2023 · 1 comment
Open

Maintain Pyndantic's Class Config Option for extras=ignore #59

ethunk opened this issue Mar 17, 2023 · 1 comment

Comments

@ethunk
Copy link

ethunk commented Mar 17, 2023

Pydantic models allow you configure how to handle extra fields. You can forbid, ignore, or allow

https://docs.pydantic.dev/usage/model_config/#options

It would be great if this configuration and settings could be carried over into the patito implementation. For example, given the following model:

import patito as pt

class TestClass(pt.Model):
    column1: str
    column2: str
    
    class Config:
        ignore_extra = True

Then I would be able do the following:

import polars as pl

data = {'column1': ['value1', 'value2'], 'column2': ['value3', 'value4'], 'column3': ['value4', 'value5']}
df = pl.from_dict(data)

df_validated = TestClass.validate(df)

df_validated == df.select(pl.col('column1', 'column2'))

>> True

df_validated
>>
shape: (2, 2)
┌─────────┬─────────┐
│ column1 ┆ column2 │
│ ---     ┆ ---     │
│ str     ┆ str     │
╞═════════╪═════════╡
│ value1  ┆ value3  │
│ value2  ┆ value4  │
└─────────┴─────────┘

df
>>
shape: (2, 3)
┌─────────┬─────────┬─────────┐
│ column1 ┆ column2 ┆ column3 │
│ ---     ┆ ---     ┆ ---     │
│ str     ┆ str     ┆ str     │
╞═════════╪═════════╪═════════╡
│ value1  ┆ value3  ┆ value4  │
│ value2  ┆ value4  ┆ value5  │
└─────────┴─────────┴─────────┘

Currently, the only way I could figure out might work to get a similar function is to define a class that is inclusive of all columns and then define a derivative class and use the drop() class method of which fields to "ignore".

@ethunk
Copy link
Author

ethunk commented Mar 17, 2023

I did some more digging in the examples and documentation and one workaround/functional equivalent is to build the polars dataframe directly from the patito model:

Using the same TestClass model and data value from above:

TestClass.DataFrame(data).drop()
>>
shape: (2, 2)
┌─────────┬─────────┐
│ column1 ┆ column2 │
│ ---     ┆ ---     │
│ str     ┆ str     │
╞═════════╪═════════╡
│ value1  ┆ value3  │
│ value2  ┆ value4  │
└─────────┴─────────┘

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant