module
dframeio.postgres
Access PostgreSQL databases using psycopg3.
Classes
PostgresBackend— Backend to read and write PostgreSQL tables</>
class
dframeio.postgres.PostgresBackend(conninfo=None, autocommit=True)
Backend to read and write PostgreSQL tables
Parameters
conninfo(str, optional) — Connection string in libq format. See the PostgreSQL docs for details.
Raises
TypeError— If any of the input arguments has a diffent type as documentedValueError— If any of the input arguments are outside of the documented value ranges or if conflicting arguments are given.
Methods
read_to_dict(source,columns,row_filter,limit,sample,drop_duplicates)(dict(str: )) — Read data into a dict of named columns</>read_to_pandas(source,columns,row_filter,limit,sample,drop_duplicates)(DataFrame) — Read a postgres table into a pandas DataFrame</>write_append(target,dataframe)— Write data in append-mode to a Postgres table</>write_replace(target,dataframe)— Write data to a Postgres table after deleting all the existing content</>
method
read_to_pandas(source, columns=None, row_filter=None, limit=-1, sample=-1, drop_duplicates=False)
Read a postgres table into a pandas DataFrame
Parameters
source(str) — The table name (may include a database name)columns(list of str, optional) — List of column names to limit the reading torow_filter(str, optional) — Filter expression for selecting rowslimit(int, optional) — Maximum number of rows to return (limit to first n rows)sample(int, optional) — Size of a random sample to returndrop_duplicates(bool, optional) — Whether to drop duplicate rows from the final selection
Returns (DataFrame)
A pandas DataFrame with the requested data.
Raises
ValueError— If path specified withsourceis outside of the base path
The logic of the filtering arguments is as documented for
read_to_pandas().
method
read_to_dict(source, columns=None, row_filter=None, limit=-1, sample=-1, drop_duplicates=False)
Read data into a dict of named columns
Parameters
source(str) — A string specifying the data source (format differs by backend)columns(list of str, optional) — List of column names to limit the reading torow_filter(str, optional) — NOT IMPLEMENTED. Reserved keyword for filtering rows.limit(int, optional) — Maximum number of rows to return (top-n)sample(int, optional) — Size of a random sample to returndrop_duplicates(bool, optional) — Whether to drop duplicate rows
Returns (dict(str: ))
A dictionary with column names as key and a list with column values as values
The logic of the filtering arguments is as documented for
read_to_pandas().
method
write_replace(target, dataframe)
Write data to a Postgres table after deleting all the existing content
Parameters
target(str) — The database table to write to.dataframe(Union(dataframe, dict(str: ))) — The data to write as pandas.DataFrame or as a Python dictionary in the formatcolumn_name: [column_data]
Raises
TypeError— When the dataframe is neither a pandas.DataFrame nor a dictionary
method
write_append(target, dataframe)
Write data in append-mode to a Postgres table
Parameters
target(str) — The database table to write to.dataframe(Union(dataframe, dict(str: ))) — The data to write as pandas.DataFrame or as a Python dictionary in the formatcolumn_name: [column_data]
Raises
TypeError— When the dataframe is neither a pandas.DataFrame nor a dictionary