generated from Tauffer-Consulting/domino_pieces_repository_template
-
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.
- Loading branch information
Showing
5 changed files
with
92 additions
and
1 deletion.
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,16 @@ | ||
{ | ||
"name": "RenameTabularColumnsPiece", | ||
"description": "A piece that receives an dataframe and a dictionary with the columns to rename and returns the dataframe with the columns renamed.", | ||
"dependency": { | ||
"requirements_file": "requirements_0.txt" | ||
}, | ||
"tags": [ | ||
"default", | ||
"pandas", | ||
"dataframe" | ||
], | ||
"style": { | ||
"node_label": "Rename Tabular Columns", | ||
"icon_class_name": "icon-park-twotone:data-file" | ||
} | ||
} |
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,35 @@ | ||
from pydantic import BaseModel, Field | ||
from typing import List | ||
|
||
class ColumnsToRename(BaseModel): | ||
""" | ||
ColumnsToRename Model | ||
""" | ||
old_column_name: str = Field( | ||
title='Old Column Name', | ||
) | ||
new_column_name: str = Field( | ||
title='New Column Name', | ||
) | ||
|
||
class InputModel(BaseModel): | ||
""" | ||
GetDateTimePiece Input Model | ||
""" | ||
input_data: str = Field( | ||
description='Input data.' | ||
) | ||
|
||
columns_to_rename: List[ColumnsToRename] = Field( | ||
title='Columns to Rename', | ||
description='List of columns to rename.' | ||
) | ||
|
||
|
||
class OutputModel(BaseModel): | ||
""" | ||
GetDateTimePiece Output Model | ||
""" | ||
output_data: str = Field( | ||
description='Output data.' | ||
) |
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,32 @@ | ||
from domino.base_piece import BasePiece | ||
from .models import InputModel, OutputModel | ||
import pandas as pd | ||
from pathlib import Path | ||
|
||
|
||
class RenameTabularColumnsPiece(BasePiece): | ||
|
||
def piece_function(self, input_data: InputModel): | ||
""" | ||
GetDateTimePiece piece function. | ||
""" | ||
input_data_file = input_data.input_data | ||
if input_data_file.endswith('.csv'): | ||
df = pd.read_csv(input_data_file) | ||
elif input_data_file.endswith('.json'): | ||
df = pd.read_json(input_data_file) | ||
else: | ||
raise ValueError("File format not supported. Please pass a CSV or JSON file.") | ||
|
||
columns_to_rename = {} | ||
for columns_data in input_data.columns_to_rename: | ||
columns_to_rename[columns_data.old_column_name] = columns_data.new_column_name | ||
|
||
df.rename(columns=columns_to_rename, inplace=True) | ||
|
||
output_data_path = Path(self.results_path) / 'output_data.csv' | ||
df.to_csv(output_data_path, index=False) | ||
|
||
return OutputModel(output_data=str(output_data_path)) | ||
|
||
|
8 changes: 8 additions & 0 deletions
8
pieces/RenameTabularColumnsPiece/test_rename_columns_piece.py
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,8 @@ | ||
from domino.testing import piece_dry_run | ||
|
||
|
||
|
||
|
||
def test_rename_columns_piece(): | ||
... | ||
|