This repository has been archived by the owner on May 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from pyspark.sql.dataframe import DataFrame | ||
from pyspark.sql.functions import expr, regexp_replace, col | ||
|
||
def fix_zero_length_arrays(df:DataFrame): | ||
"""For every field of type array, turn zero length arrays into true nulls | ||
Args: | ||
df (DataFrame): Input Spark dataframe | ||
Returns: | ||
DataFrame: Spark Dataframe with clean arrays | ||
""" | ||
|
||
array_cols = [item[0] for item in df.dtypes if item[1].startswith('array')] | ||
|
||
stmt = """ | ||
case | ||
when size({c}) > 0 then {c} | ||
else null | ||
end | ||
""" | ||
|
||
for c in array_cols: | ||
df = df.withColumn(c, expr(stmt.format(c=c))) | ||
|
||
return df |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import pytest | ||
import pandas as pd | ||
|
||
from splink_data_normalisation.arrays import fix_zero_length_arrays | ||
from pyspark.sql import Row | ||
|
||
|
||
def test_fix_1(spark): | ||
|
||
names_list = [ | ||
{"id": 1, "my_arr1": ["a", "b", "c"], "other_arr": [ ],"my_str": "a"}, | ||
{"id": 2, "my_arr1": [ ], "other_arr": [1],"my_str": "a"}, | ||
|
||
] | ||
|
||
df = spark.createDataFrame(Row(**x) for x in names_list) | ||
df = df.select(list(names_list[0].keys())) | ||
|
||
df = fix_zero_length_arrays(df) | ||
|
||
df_result = df.toPandas() | ||
|
||
df_expected = [ | ||
{"id": 1, "my_arr1": ["a", "b", "c"], "other_arr": None,"my_str": "a"}, | ||
{"id": 2, "my_arr1": None, "other_arr": [1] ,"my_str": "a"}, | ||
] | ||
|
||
df_expected = pd.DataFrame(df_expected) | ||
|
||
pd.testing.assert_frame_equal(df_result,df_expected) | ||
|