name: Pandas Transform DataFrame in CSV format
description: |-
  Transform DataFrame loaded from a CSV file.

      Inputs:
          table: Table to transform.
          transform_code: Transformation code. Code is written in Python and can consist of multiple lines.
              The DataFrame variable is called "df".
              Examples:
              - `df['prod'] = df['X'] * df['Y']`
              - `df = df[['X', 'prod']]`
              - `df.insert(0, "is_positive", df["X"] > 0)`

      Outputs:
          transformed_table: Transformed table.

      Annotations:
          author: Alexey Volkov <alexey.volkov@ark-kun.com>
inputs:
- {name: table, type: CSV}
- {name: transform_code, type: PythonCode}
outputs:
- {name: transformed_table, type: CSV}
metadata:
  annotations:
    author: Alexey Volkov <alexey.volkov@ark-kun.com>
    canonical_location: 'https://raw.githubusercontent.com/Ark-kun/pipeline_components/master/components/pandas/Transform_DataFrame/in_CSV_format/component.yaml'
implementation:
  container:
    image: python:3.7
    command:
    - sh
    - -c
    - (PIP_DISABLE_PIP_VERSION_CHECK=1 python3 -m pip install --quiet --no-warn-script-location
      'pandas==1.0.4' || PIP_DISABLE_PIP_VERSION_CHECK=1 python3 -m pip install --quiet
      --no-warn-script-location 'pandas==1.0.4' --user) && "$0" "$@"
    - python3
    - -u
    - -c
    - |
      def _make_parent_dirs_and_return_path(file_path: str):
          import os
          os.makedirs(os.path.dirname(file_path), exist_ok=True)
          return file_path

      def Pandas_Transform_DataFrame_in_CSV_format(
          table_path,
          transformed_table_path,
          transform_code,
      ):
          '''Transform DataFrame loaded from a CSV file.

          Inputs:
              table: Table to transform.
              transform_code: Transformation code. Code is written in Python and can consist of multiple lines.
                  The DataFrame variable is called "df".
                  Examples:
                  - `df['prod'] = df['X'] * df['Y']`
                  - `df = df[['X', 'prod']]`
                  - `df.insert(0, "is_positive", df["X"] > 0)`

          Outputs:
              transformed_table: Transformed table.

          Annotations:
              author: Alexey Volkov <alexey.volkov@ark-kun.com>
          '''
          import pandas

          df = pandas.read_csv(
              table_path,
          )
          # The namespace is needed so that the code can replace `df`. For example df = df[['X']]
          namespace = locals()
          exec(transform_code, namespace)
          namespace['df'].to_csv(
              transformed_table_path,
              index=False,
          )

      import argparse
      _parser = argparse.ArgumentParser(prog='Pandas Transform DataFrame in CSV format', description='Transform DataFrame loaded from a CSV file.\n\n    Inputs:\n        table: Table to transform.\n        transform_code: Transformation code. Code is written in Python and can consist of multiple lines.\n            The DataFrame variable is called "df".\n            Examples:\n            - `df[\'prod\'] = df[\'X\'] * df[\'Y\']`\n            - `df = df[[\'X\', \'prod\']]`\n            - `df.insert(0, "is_positive", df["X"] > 0)`\n\n    Outputs:\n        transformed_table: Transformed table.\n\n    Annotations:\n        author: Alexey Volkov <alexey.volkov@ark-kun.com>')
      _parser.add_argument("--table", dest="table_path", type=str, required=True, default=argparse.SUPPRESS)
      _parser.add_argument("--transform-code", dest="transform_code", type=str, required=True, default=argparse.SUPPRESS)
      _parser.add_argument("--transformed-table", dest="transformed_table_path", type=_make_parent_dirs_and_return_path, required=True, default=argparse.SUPPRESS)
      _parsed_args = vars(_parser.parse_args())

      _outputs = Pandas_Transform_DataFrame_in_CSV_format(**_parsed_args)
    args:
    - --table
    - {inputPath: table}
    - --transform-code
    - {inputValue: transform_code}
    - --transformed-table
    - {outputPath: transformed_table}
