class SQLTableDataSet(AbstractDataSet[
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 |
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 |
Undocumented |
Constant | DEFAULT |
Undocumented |
Class Variable | engines |
Undocumented |
Method | _describe |
Undocumented |
Method | _exists |
Undocumented |
Method | _load |
Undocumented |
Method | _save |
Undocumented |
Instance Variable | _connection |
Undocumented |
Instance Variable | _load |
Undocumented |
Instance Variable | _save |
Undocumented |
Inherited from AbstractDataSet
:
Class Method | from |
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 |
Given a connection string, create singleton connection
to be used across all instances of SQLTableDataSet
that
need to connect to the same source.
str
, credentials: Dict[ str, Any]
, load_args: Dict[ str, Any]
= None, save_args: Dict[ str, Any]
= None):
(source)
¶
Creates a new SQLTableDataSet.
Parameters | |
tablestr | The table name to load or save data to. It overwrites name in save_args and table_name parameters in load_args. |
credentials:Dict[ | 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 |
loadDict[ | 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 |
saveDict[ | 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 | |
DataSetError | When either table_name or con is empty. |