Skip to content

Commit

Permalink
fixing unique multi index in SchemaModel (#870)
Browse files Browse the repository at this point in the history
* fixing unique multi index in SchemaModel

* fix noxfile issue

* fix noxfile issue

Co-authored-by: matt <m.bouchy@gmail.com>
Co-authored-by: cosmicBboy <niels.bantilan@gmail.com>
  • Loading branch information
3 people committed Aug 10, 2022
1 parent da8ee94 commit bc6d8f6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
6 changes: 6 additions & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,12 @@ def install_extras(
pandas_stubs: bool = True,
) -> None:
"""Install dependencies."""

if isinstance(session.virtualenv, nox.virtualenv.PassthroughEnv):
# skip this step if there's no virtual environment specified
session.run("pip", "install", "-e", ".", "--no-deps")
return

specs, pip_specs = [], []
pandas_version = "" if pandas == "latest" else f"=={pandas}"
for spec in REQUIRES[extra].values():
Expand Down
2 changes: 2 additions & 0 deletions pandera/typing/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class BaseConfig: # pylint:disable=R0903
#: coerce types of all MultiIndex components
multiindex_coerce: bool = False

#: make sure the MultiIndex is unique along the list of columns
multiindex_unique = None
#: make sure all specified columns are in validated MultiIndex -
#: if ``"filter"``, removes indexes not specified in the schema
multiindex_strict: StrictType = False
Expand Down
34 changes: 34 additions & 0 deletions tests/core/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,40 @@ class Config:
assert expected == Child.to_schema()


def test_multiindex_unique() -> None:
class Base(pa.SchemaModel):
a: Series[int]
idx_1: Index[str]
idx_2: Index[str]

class Config:
name = "Base schema"
coerce = True
ordered = True
multiindex_coerce = True
multiindex_strict = True
multiindex_unique = ["idx_1", "idx_2"]
multiindex_name: Optional[str] = "mi"
unique_column_names = True

expected = pa.DataFrameSchema(
columns={"a": pa.Column(int)},
index=pa.MultiIndex(
[pa.Index(str, name="idx_1"), pa.Index(str, name="idx_2")],
coerce=True,
strict=True,
name="mi",
unique=["idx_1", "idx_2"],
),
name="Base schema",
coerce=True,
ordered=True,
unique_column_names=True,
)

assert expected == Base.to_schema()


def test_config_docstrings() -> None:
class Model(pa.SchemaModel):
"""foo"""
Expand Down

0 comments on commit bc6d8f6

Please sign in to comment.