Load data

Teradata Developer Guides

ft:locale
en-US
ft:lastEdition
2026-08-18

Amazon SageMaker AI trains machine learning models using data stored in an Amazon S3 bucket. Follow these steps to load training data from Teradata into an Amazon S3 bucket:

  1. In the Amazon SageMaker AI console, select Applications and IDEs -> Notebooks -> Notebook instances, and then select Create notebook instance. See the Amazon SageMaker AI Developer Guide for instructions on how to create a notebook instance:

Create notebook instance

  1. Open your notebook instance: Open notebook instance

  2. Start a new file by clicking on New -> conda_python3: Start new file

  3. Install Teradata Python library:

    !pip install teradataml
    
  4. In a new cell and import additional libraries:

    import teradataml as tdml
    from teradataml import create_context, get_context, remove_context
    from teradataml.dataframe.dataframe import DataFrame
    import pandas as pd
    import boto3, os
    
  5. In a new cell, connect to Teradata. Replace <hostname>, <database user name>, <database password> to match your Teradata environment:

    create_context(host = '<hostname>', username = '<database user name>', password = '<database password>')
    
  6. Retrieve data from the table where the training dataset resides using TeradataML DataFrame API:

    train_data = tdml.DataFrame('table_with_training_data')
    trainDF = train_data.to_pandas()
    

    Note

    For the XGBoost training job used in this how-to, place the target column first and encode categorical values as numeric values before exporting the data. The CSV file must not contain a header row.

  7. Write data to a local file:

    trainFileName = 'train.csv'
    trainDF.to_csv(trainFileName, header=None, index=False)
    
  8. Upload the file to Amazon S3. Replace <s3_bucket_name> with the name of your Amazon S3 bucket:

    bucket = '<s3_bucket_name>'
    prefix = 'sagemaker/train'
    
    with open(trainFileName, 'rb') as trainFile:
        boto3.Session().resource('s3') \
            .Bucket(bucket) \
            .Object(os.path.join(prefix, trainFileName)) \
            .upload_fileobj(trainFile)