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

Fix validating pyspark dataframes with regex columns #1397

Merged
merged 1 commit into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pandera/api/pyspark/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,9 @@ def get_dtypes(self, dataframe: DataFrame) -> Dict[str, DataType]:
regex_dtype.update(
{
c: column.dtype
for c in column.BACKEND.get_regex_columns(
for c in column.get_backend(
dataframe
).get_regex_columns(
column,
dataframe.columns,
)
Expand Down
8 changes: 4 additions & 4 deletions pandera/backends/pyspark/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@
if col_schema.regex:
try:
column_names.extend(
col_schema.BACKEND.get_regex_columns(
col_schema.get_backend(check_obj).get_regex_columns(
col_schema, check_obj.columns
)
)
Expand Down Expand Up @@ -457,9 +457,9 @@
for colname, col_schema in schema.columns.items():
if col_schema.regex:
try:
matched_columns = col_schema.BACKEND.get_regex_columns(
col_schema, obj.columns
)
matched_columns = col_schema.get_backend(

Check warning on line 460 in pandera/backends/pyspark/container.py

View check run for this annotation

Codecov / codecov/patch

pandera/backends/pyspark/container.py#L460

Added line #L460 was not covered by tests
obj
).get_regex_columns(col_schema, obj.columns)
except SchemaError:
matched_columns = None

Expand Down
28 changes: 28 additions & 0 deletions tests/pyspark/test_pyspark_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,31 @@ def test_pyspark_sample():
df_out = schema.validate(df, sample=0.5)

assert isinstance(df_out, DataFrame)


def test_pyspark_regex_column():
"""
Test creating a pyspark DataFrameSchema object with regex columns
"""

schema = DataFrameSchema(
{
# Columns with all caps names must have string values
"[A-Z]+": Column(T.StringType(), regex=True),
}
)

data = [("Neeraj", 35), ("Jask", 30)]

df = spark.createDataFrame(data=data, schema=["NAME", "AGE"])
df_out = schema.validate(df)

assert df_out.pandera.errors is not None

data = [("Neeraj", "35"), ("Jask", "a")]

df2 = spark.createDataFrame(data=data, schema=["NAME", "AGE"])

df_out = schema.validate(df2)

assert not df_out.pandera.errors