This repository has been archived by the owner on Aug 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #145 from atc-net/feature/extended-transformer
Feature/select-and-drop-transformers
- Loading branch information
Showing
5 changed files
with
111 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
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,22 @@ | ||
from typing import List | ||
|
||
from pyspark.sql import DataFrame | ||
|
||
from atc.etl import TransformerNC | ||
|
||
|
||
class DropColumnsTransformer(TransformerNC): | ||
def __init__( | ||
self, | ||
columnList: List[str], | ||
dataset_input_key: str = None, | ||
dataset_output_key: str = None, | ||
): | ||
super().__init__( | ||
dataset_input_key=dataset_input_key, dataset_output_key=dataset_output_key | ||
) | ||
self.columnList = columnList | ||
|
||
def process(self, df: DataFrame) -> DataFrame: | ||
df = df.drop(*self.columnList) | ||
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,21 @@ | ||
from typing import List | ||
|
||
from pyspark.sql import DataFrame | ||
|
||
from atc.etl import TransformerNC | ||
|
||
|
||
class SelectColumnsTransformer(TransformerNC): | ||
def __init__( | ||
self, | ||
columnList: List[str], | ||
dataset_input_key: str = None, | ||
dataset_output_key: str = None, | ||
): | ||
super().__init__( | ||
dataset_input_key=dataset_input_key, dataset_output_key=dataset_output_key | ||
) | ||
self.columnList = columnList | ||
|
||
def process(self, df: DataFrame) -> DataFrame: | ||
return df.select(self.columnList) |
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,36 @@ | ||
import pyspark.sql.types as T | ||
from atc_tools.testing import DataframeTestCase | ||
|
||
from atc.spark import Spark | ||
from atc.transformers import DropColumnsTransformer | ||
|
||
|
||
class TestDropColumnsTransformer(DataframeTestCase): | ||
def test_drop_columns_transformer(self): | ||
|
||
inputSchema = T.StructType( | ||
[ | ||
T.StructField("id", T.LongType(), True), | ||
T.StructField("text1", T.StringType(), True), | ||
T.StructField("text2", T.StringType(), True), | ||
] | ||
) | ||
|
||
inputData = [ | ||
( | ||
1, | ||
"text1", | ||
"text2", | ||
), | ||
] | ||
|
||
df_input = Spark.get().createDataFrame(data=inputData, schema=inputSchema) | ||
|
||
expectedData = [ | ||
(1,), | ||
] | ||
|
||
df_transformed = DropColumnsTransformer(columnList=["text1", "text2"]).process( | ||
df_input | ||
) | ||
self.assertDataframeMatches(df_transformed, None, expectedData) |
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,30 @@ | ||
import pyspark.sql.types as T | ||
from atc_tools.testing import DataframeTestCase | ||
|
||
from atc.spark import Spark | ||
from atc.transformers import SelectColumnsTransformer | ||
|
||
|
||
class TestSelectColumnsTransformer(DataframeTestCase): | ||
def test_select_columns_transformer(self): | ||
|
||
inputSchema = T.StructType( | ||
[ | ||
T.StructField("Col1", T.StringType(), True), | ||
T.StructField("Col2", T.IntegerType(), True), | ||
T.StructField("Col3", T.DoubleType(), True), | ||
T.StructField("Col4", T.StringType(), True), | ||
T.StructField("Col5", T.StringType(), True), | ||
] | ||
) | ||
inputData = [("Col1Data", 42, 13.37, "Col4Data", "Col5Data")] | ||
|
||
input_df = Spark.get().createDataFrame(data=inputData, schema=inputSchema) | ||
|
||
expectedData = [(42, 13.37, "Col4Data")] | ||
|
||
transformed_df = SelectColumnsTransformer( | ||
columnList=["Col2", "Col3", "Col4"] | ||
).process(input_df) | ||
|
||
self.assertDataframeMatches(transformed_df, None, expectedData) |