class documentation

class SQLTableDataSet(AbstractDataSet[pd.DataFrame, pd.DataFrame]): (source)

View In Hierarchy

SQLTableDataSet loads data from a SQL table and saves a pandas dataframe to a table. It uses pandas.DataFrame internally, so it supports all allowed pandas options on read_sql_table and to_sql methods. Since Pandas uses SQLAlchemy behind the scenes, when instantiating SQLTableDataSet one needs to pass a compatible connection string either in credentials (see the example code snippet below) or in load_args and save_args. Connection string formats supported by SQLAlchemy can be found here: https://docs.sqlalchemy.org/en/13/core/engines.html#database-urls

SQLTableDataSet modifies the save parameters and stores the data with no index. This is designed to make load and save methods symmetric.

Example usage for the YAML API:

shuttles_table_dataset:
  type: pandas.SQLTableDataSet
  credentials: db_credentials
  table_name: shuttles
  load_args:
    schema: dwschema
  save_args:
    schema: dwschema
    if_exists: replace

Sample database credentials entry in credentials.yml:

db_credentials:
  con: postgresql://scott:tiger@localhost/test

Example usage for the Python API:

>>> from kedro.extras.datasets.pandas import SQLTableDataSet
>>> import pandas as pd
>>>
>>> data = pd.DataFrame({"col1": [1, 2], "col2": [4, 5],
>>>                      "col3": [5, 6]})
>>> table_name = "table_a"
>>> credentials = {
>>>     "con": "postgresql://scott:tiger@localhost/test"
>>> }
>>> data_set = SQLTableDataSet(table_name=table_name,
>>>                            credentials=credentials)
>>>
>>> data_set.save(data)
>>> reloaded = data_set.load()
>>>
>>> assert data.equals(reloaded)
Class Method create_connection Given a connection string, create singleton connection to be used across all instances of SQLTableDataSet that need to connect to the same source.
Method __init__ Creates a new SQLTableDataSet.
Constant DEFAULT_LOAD_ARGS Undocumented
Constant DEFAULT_SAVE_ARGS Undocumented
Class Variable engines Undocumented
Method _describe Undocumented
Method _exists Undocumented
Method _load Undocumented
Method _save Undocumented
Instance Variable _connection_str Undocumented
Instance Variable _load_args Undocumented
Instance Variable _save_args Undocumented

Inherited from AbstractDataSet:

Class Method from_config Create a data set instance using the configuration provided.
Method __str__ Undocumented
Method exists Checks whether a data set's output already exists by calling the provided _exists() method.
Method load Loads data by delegation to the provided load method.
Method release Release any cached data.
Method save Saves data by delegation to the provided save method.
Method _copy Undocumented
Method _release Undocumented
Property _logger Undocumented
@classmethod
def create_connection(cls, connection_str: str): (source)

Given a connection string, create singleton connection to be used across all instances of SQLTableDataSet that need to connect to the same source.

def __init__(self, table_name: str, credentials: Dict[str, Any], load_args: Dict[str, Any] = None, save_args: Dict[str, Any] = None): (source)

Creates a new SQLTableDataSet.

Parameters
table_name:strThe table name to load or save data to. It overwrites name in save_args and table_name parameters in load_args.
credentials:Dict[str, Any]A dictionary with a SQLAlchemy connection string. Users are supposed to provide the connection string 'con' through credentials. It overwrites con parameter in load_args and save_args in case it is provided. To find all supported connection string formats, see here: https://docs.sqlalchemy.org/en/13/core/engines.html#database-urls
load_args:Dict[str, Any]Provided to underlying pandas read_sql_table function along with the connection string. To find all supported arguments, see here: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_sql_table.html To find all supported connection string formats, see here: https://docs.sqlalchemy.org/en/13/core/engines.html#database-urls
save_args:Dict[str, Any]Provided to underlying pandas to_sql function along with the connection string. To find all supported arguments, see here: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_sql.html To find all supported connection string formats, see here: https://docs.sqlalchemy.org/en/13/core/engines.html#database-urls It has index=False in the default parameters.
Raises
DataSetErrorWhen either table_name or con is empty.
DEFAULT_LOAD_ARGS: Dict[str, Any] = (source)

Undocumented

Value
{}
DEFAULT_SAVE_ARGS: Dict[str, Any] = (source)

Undocumented

Value
{'index': False}

Undocumented

def _describe(self) -> Dict[str, Any]: (source)

Undocumented

def _exists(self) -> bool: (source)

Undocumented

def _load(self) -> pd.DataFrame: (source)

Undocumented

def _save(self, data: pd.DataFrame): (source)

Undocumented

_connection_str = (source)

Undocumented

_load_args = (source)

Undocumented

_save_args = (source)

Undocumented