@transform
Transform Decorator
Summary:
transform
decorators are applied to the main functions that perform tabular data transformations in Morph's Python cells. This decorator is used to ensure that the function returns a pandas.DataFrame
object.
Parameters:
- None.
Return value:
- The decorator wraps the input function and ensures that the result of its execution is a
pandas.DataFrame
instance before returning it.
Usage example:
from morphdb_utils.annotations import transform
@transform
def main(data: Dict[str, pd.DataFrame]):
df = data["sheet_cell"]
# Extract only data that contains 'Annual Average'
df = df[df['Name'] == "Nitrogen dioxide (NO2)"]
df = df[df['Time Period'].str.contains("Annual Average")]
# Select necessary columns and rename them
df = df[['Time Period', 'Data Value', 'Geo Place Name']]
df.columns = ['Year', 'NO2_Concentration', 'Location']
# Extract year from 'Year' column which contains 'Annual Average YYYY'
df['Year'] = df['Year'].str.extract(r'(\d{4})').astype(int)
# Filter data to include only 'Manhattan'
df = df[df['Location'] == "Manhattan"]
# Sort the data by year in ascending order
df = df.sort_values(by='Year', ascending=True)
return df