to_csv() Method | Teradata Package for Python - to_csv() Method - Teradata Vantage

Teradata® VantageCloud Lake

Deployment
VantageCloud
Edition
Lake
Product
Teradata Vantage
Published
January 2023
Language
English (United States)
Last Update
2024-02-17
dita:mapPath
phg1621910019905.ditamap
dita:ditavalPath
pny1626732985837.ditaval
dita:id
phg1621910019905

Use the to_csv() function to export data from a teradataml DataFrame to CSV file.

Required argument:
  • csv_file: Specifies the name of CSV file to export the data into.
Optional arguments:
  • num_rows: Specifies the number of rows to retrieve from DataFrame.

    The default is 99999.

    This argument is ignored if all_rows is set to True.
  • all_rows: Specifies whether all rows from the teradataml DataFrame to be exported to the CSV file or not.

    The default is False.

  • fastexport: Specifies whether FastExport protocol should be used while exporting data.
    Possible values for this argument:
    • The default value is False. Which means, by default, FastExport protocol is not used.
    • If the argument is set to True, data is exported using FastExport protocol.
    • If the argument is set to None, the approach is decided based on the number of rows to be exported:
      • If requested number of rows is greater than or equal to 100,000, then FastExport is used.
      • If requested number of rows is less than 100,000, regular mode is used.
    • Teradata recommends using FastExport when number of rows in teradataml DataFrame is at least 100,000.

      To extract lesser number of rows, ignore this option and go with regular approach. Because FastExport opens multiple data transfer connections to the database, which is time consuming.

    • FastExport does not support all data types in Vantage.

      For example, tables with BLOB and CLOB type columns cannot be extracted.

    • FastExport cannot be used to extract data from a volatile or temporary table.
    • For best efficiency, do not use DataFrame.groupby() and DataFrame.sort() with FastExport.

    See the FastExport section of https://pypi.org/project/teradatasql/ for more information about FastExport protocol through teradatasql driver.

  • sep: Specifies a single character string used to separate fields in a CSV file, with default value ' , '.
  • quotechar: Specifies a single character string used to quote fields in a CSV file, with default value ' " ' (double quote).
    • sep and quotechar cannot be line feed ('\\n') or carriage return ('\\r').
    • sep and quotechar should not be the same.
    • Length of sep and quotechar should be 1.
  • catch_errors_warnings: Specifies whether to catch errors and warnings, if any, raised by FastExport protocol while exporting data.
    This argument is ignored if fastexport argument is set to FALSE.
    When FastExport is used and this argument is set to True, to_csv() returns a tuple containing:
    • Errors, if any, in a list thrown by FastExport
    • Warnings, if any, in a list thrown by FastExport
  • kwargs: Specifies keyword arguments.

    Argument open_sessions can be passed as keyword arguments.

    open_sessions specifies the number of data transfer sessions to be opened for fastexport. This argument is only applicable in fastexport mode.
    If open_sessions argument is not set, by default, the number of data transfer sessions opened by teradataml is the smaller of 8 and the number of available AMPs in Vantage.

    See the FastExport section of https://pypi.org/project/teradatasql/ for more information about number of data transfer session opened during fastexport.

Example Setup

# Create a teradataml DataFrame.
>>> load_example_data("dataframe","admissions_train")
>>> df = DataFrame("admissions_train")

Example 1: Export data from teradataml DataFrame into CSV with only required argument

>>> df.to_csv("export_to_csv_1.csv")
Data is successfully exported into export_to_csv_1.csv

Example 2: Export all rows from teradataml DataFrame into CSV using FastExport protocol

>>> df.to_csv("export_to_csv_2.csv", all_rows=True, fastexport=True)
Data is successfully exported into export_to_csv_2.csv

Example 3: Export 20 rows from teradataml DataFrame into CSV

>>> df.to_csv("export_to_csv_3.csv", num_rows=20)
Data is successfully exported into export_to_csv_3.csv

Example 4: Export data from teradataml DataFrame into CSV with additional settings

This example exports data from teradataml DataFrame into CSV using FastExport protocol by opening one Teradata data transfer session and saves errors and warnings thrown by fastexport.

>>> err, warn = df.to_csv("export_to_csv_4.csv", fastexport=True, catch_errors_warnings=True, open_sessions=1 )
Data is successfully exported into export_to_csv_4.csv
>>>err
[]
>>>warn
[]

Example 5: Export data from teradataml DataFrame into CSV with specified separator and quote character

This example exports data from teradataml DataFrame into CSV with '|' as field separator and single quote(') as field quote character.

>>>df.to_csv("export_to_csv_5.csv", sep="|", quotechar="'" )