Remove columns on a SchemaModel #990
-
Problem import pandera as pa
from pandera.typing import DataFrame, Series
class SchemaA(pa.SchemaModel):
a: Series[int]
b: Series[int]
c: Series[int]
d: Series[int]
class SchemaB(SchemaA):
...
cols_to_keep = ["a", "b", "c"]
def foo(data: DataFrame[SchemaA]) -> DataFrame[SchemaB]:
return data[cols_to_keep] Unless I'm a bit blind, there is no way to define Possible Solutions
Alternatives SchemaB = (df_schema_a := SchemaA.to_schema()).remove_columns(
[col for col in df_schema_a.columns if col not in cols_to_keep]
) But then typing doesn't work any more, because Additional Context |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
hey @a-recknagel so unfortunately using It's perhaps a little less intuitive, but if you want to rely on Python's inheritence semantics, I typically use this pattern: import pandera as pa
from pandera.typing import DataFrame, Series
class BaseSchema(pa.SchemaModel):
# put all the common columns here
a: Series[int]
b: Series[int]
class SchemaA(BaseSchema):
c: Series[int]
d: Series[int]
class SchemaB(BaseSchema):
# suppose you drop but also add a bunch of columns. This is equivalent to
# dropping "a" and "b" and adding "e" and "f"
e: Series[int]
f: Series[int] This can get overly verbose, so an alternative would be to patch the import pandera as pa
from pandera.typing import DataFrame, Series
class SchemaA(pa.SchemaModel):
a: Series[int]
b: Series[int]
c: Series[int]
d: Series[int]
class SchemaB(SchemaA):
...
@classmethod
def to_schema(cls) -> pa.DataFrameSchema:
schema = super().to_schema()
return schema.remove_columns(["a", "b"])
print(SchemaB.to_schema())
# <Schema DataFrameSchema(
# columns={
# 'c': <Schema Column(name=c, type=DataType(int64))>
# 'd': <Schema Column(name=d, type=DataType(int64))>
# },
# checks=[],
# coerce=False,
# dtype=None,
# index=None,
# strict=False
# name=SchemaB,
# ordered=False,
# unique_column_names=False
# )> Note, though, that with this method the Lemme know if this works for you! Also, this question has come up before a few times, so probably worth adding a section in the docs about this... would you be interested in contributing a section on this page? |
Beta Was this translation helpful? Give feedback.
-
The second option hopefully works for me, thanks! I have some multiple inheritances, and always forget how to make overriding methods cooperative. I'll try to add the docs, too. |
Beta Was this translation helpful? Give feedback.
-
Cool, I'm gonna convert this issue into a discussion... would you mind marking my response as the answer? |
Beta Was this translation helpful? Give feedback.
hey @a-recknagel so unfortunately using
SchemaModel
s in this way isn't possible because inheritence is only additive.It's perhaps a little less intuitive, but if you want to rely on Python's inheritence semantics, I typically use this pattern:
This can get ov…