Skip to content

Commit

Permalink
rename columns piece
Browse files Browse the repository at this point in the history
  • Loading branch information
vinicvaz committed May 7, 2024
1 parent a8316e5 commit eca927c
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pieces/CustomPythonPiece/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class InputModel(BaseModel):
}
)
script: str = Field(
default="""# Do not modify the function definition line
default="""# Do not modify the function name in definition line
def custom_function(kwarg_1, kwarg_2):
# Write your code here
print(f"First argument: {kwarg_1}")
Expand Down
16 changes: 16 additions & 0 deletions pieces/RenameTabularColumnsPiece/metadata.json
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"
}
}
35 changes: 35 additions & 0 deletions pieces/RenameTabularColumnsPiece/models.py
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.'
)
32 changes: 32 additions & 0 deletions pieces/RenameTabularColumnsPiece/piece.py
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 pieces/RenameTabularColumnsPiece/test_rename_columns_piece.py
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():
...

0 comments on commit eca927c

Please sign in to comment.