DataFrame.from_pandas() Function | Teradata Package for Python - DataFrame.from_pandas Function - Teradata Package for Python

Teradata® Package for Python User Guide

Deployment
VantageCloud
VantageCore
Edition
VMware
Enterprise
IntelliFlex
Product
Teradata Package for Python
Release Number
20.00
Published
March 2025
ft:locale
en-US
ft:lastEdition
2025-12-05
dita:mapPath
nvi1706202040305.ditamap
dita:ditavalPath
plt1683835213376.ditaval
dita:id
rkb1531260709148
Product Category
Teradata Vantage

Use the DataFrame.from_pandas() function to create a teradataml DataFrame using a pandas DataFrame.

Required Parameter

pandas_df
Specifies the pandas DataFrame to be converted to teradataml DataFrame.

Optional Parameters

index
Specifies whether to save Pandas DataFrame index as a column or not.

Default value = True

index_label
Specifies the column labels for Pandas DataFrame index columns.
Refer to the 'index_label' parameter of copy_to_sql() for more details.
primary_index
Specifies which columns to use as primary index for the teradataml DataFrame.
persist
Specifies whether to persist the DataFrame.

Default value: false

Example setup

>>> import pandas as pd
>>> from teradataml import DataFrame
>>> pdf = pd.DataFrame({"col1": [1, 2, 3], "col2": [4, 5, 6]})
>>> pdf1 = pd.DataFrame([[1, 2], [3, 4]])

Example 1: Create a teradataml DataFrame from a pandas DataFrame

>>> df = DataFrame.from_pandas(pdf)
>>> df
    col1 col2 index_label
0      3    6           2
1      2    5           1
2      1    4           0

Example 2: Create a teradataml DataFrame from a pandas DataFrame and do not save the index as a column

>>> df = DataFrame.from_pandas(pdf, index=False)
>>> df
    col1 col2
0      3    6
1      2    5
2      1    4

Example 3: Create a teradataml DataFrame from a pandas DataFrame with index label as 'id' and set it as primary index

>>> df = DataFrame.from_pandas(pdf, index=True, index_label='id', primary_index='id')
>>> df
    col1 col2
id
2      3    6
1      2    5
0      1    4

Example 4: Create a teradataml DataFrame from a pandas DataFrame where columns are not explicitly defined in the pandas DataFrame

>>> df = DataFrame.from_pandas(pdf1)
>>> df
    col_0 col_1 index_label
0       3     4           1
1       1     2           0

Example 5: Persist the data from pandas DataFrame into a table which can be used across sessions

>>> df = DataFrame.from_pandas(pdf, persist=True)
>>> df
        col1  col2  index_label
    0     3     6            2
    1     2     5            1
    2     1     4            0

Get the database object name.

>>> df.db_object_name
'"ml__from_pandas_1761717824742117"'

Create the teradataml DataFrame using the database object name in different session.

>>> DataFrame('"ml__from_pandas_1761717824742117"')
        col1  col2  index_label
    0     3     6            2
    1     2     5            1