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

Teradata® Package for Python User Guide

Deployment
VantageCloud
VantageCore
Edition
Enterprise
IntelliFlex
VMware
Product
Teradata Package for Python
Release Number
20.00
Published
March 2024
Language
English (United States)
Last Update
2024-04-09
dita:mapPath
nvi1706202040305.ditamap
dita:ditavalPath
plt1683835213376.ditaval
dita:id
rkb1531260709148
Product Category
Teradata Vantage

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 must 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 cannot be the same.
    • Length of sep and quotechar must 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="'" )