Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reusing Field objects causes unexpected failure #1680

Open
2 of 3 tasks
lundybernard opened this issue Jun 12, 2024 · 0 comments
Open
2 of 3 tasks

Reusing Field objects causes unexpected failure #1680

lundybernard opened this issue Jun 12, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@lundybernard
Copy link
Contributor

Reusing a pandera.Field instance in a DataFrameModel definition causes unexpected behavior and an unhelpful error message.
If the same Field instance is assigned to two attributes, the first attribute will be dropped from the model. This should raise an exception when the model is built, but will only raise an error when validation fails.

  • I have checked that this issue has not already been reported.
  • I have confirmed this bug exists on the latest version of pandera.
  • (optional) I have confirmed this bug exists on the main branch of pandera.

Code Sample, a copy-pastable example

from pandera import DataFrameModel, Field
from pandas import DataFrame

# This Fails
# Attempt to create a field instance to be reused on multiple fields
GenericField: float = Field(ge=0)

class BadModelDF(DataFrameModel):
    field: float = GenericField
    field_1: float = GenericField  # Bug: this is breaks the model
    
    class Config:
        strict = True

# Works
# Workaround, create a new Field object for each model field
def generic_field() -> Field:
    return Field(ge=0)

# This creates a valid model that works as expected
class GoodModelDF(DataFrameModel):
    field: float = generic_field()
    field_1: float = generic_field()
    
    class Config:
        strict = True
        
df = DataFrame({'field': [0.0, 0.1], 'field_1': [0.2, 0.3]})

print(GoodModelDF(df))

# Raises SchemaError 
BadModelDF.validate(df)

Raises: SchemaError: column 'field' not in DataFrameSchema {'field_1': <Schema Column(name=field_1, type=DataType(float64))>}

Expected behavior

Reusing the GenericField instance should result in a valid model/schema that includes both field and field_1 columns.

OR

Assigning the same instance to multiple field attributes should raise an expressive exception when the class is created.

Ex:

class BadModelDF(DataFrameModel):
    field: float = GenericField
    field_1: float = GenericField  # Bug: this is breaks the model

Raises:
AttributeError: Field instances cannot be used for multiple columns. GenericField<pandera.api.dataframe.model_components.FieldInfo("None") object at 0x7fd460386490> assigned to 'field' and 'field_1' attributes for BadMdelDF

Desktop (please complete the following information):

  • OS: [Ubuntu]
  • Version: [e.g. 22.04]
@lundybernard lundybernard added the bug Something isn't working label Jun 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant