patito.duckdb.Relation
- Relation.__init__(derived_from, database=None, model=None)
Create a new relation object containing data to be queried with DuckDB.
- Parameters:
derived_from (
Union[DataFrame,DataFrame,DataFrame,Path,str,DuckDBPyRelation,Relation]) –Data to be represented as a DuckDB relation object. Can be one of the following types:
A pandas or polars DataFrame.
An SQL query represented as a string.
A
Pathobject pointing to a CSV or a parquet file. The path must point to an existing file with either a.csvor.parquetfile extension.A native DuckDB relation object (
duckdb.DuckDBPyRelation).A
patito.duckdb.Relationobject.
database (
Optional[Database]) – Which database to load the relation into. If not provided, the default DuckDB database will be used.model (
Optional[Type[TypeVar(ModelType, bound= Model)]]) –Sub-class of
patito.Modelwhich specifies how to deserialize rows when fetched with methods such as Relation.get() and__iter__().Will also be used to create a strict table schema if Relation.create_table(). schema should be constructed.
If not provided, a dynamic model fitting the relation schema will be created when required.
Can also be set later dynamically by invoking Relation.set_model().
- Raises:
ValueError – If any one of the following cases are encountered: - If a provided
Pathobject does not have a.csvor.parquetfile extension. - If a database and relation object is provided, but the relation object does not belong to the database.TypeError – If the type of
derived_fromis not supported.
Examples
Instantiated from a dataframe:
>>> import patito as pt >>> df = pt.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]}) >>> pt.duckdb.Relation(df).filter("a > 2").to_df() shape: (1, 2) ┌─────┬─────┐ │ a ┆ b │ │ --- ┆ --- │ │ i64 ┆ i64 │ ╞═════╪═════╡ │ 3 ┆ 6 │ └─────┴─────┘
Instantiated from an SQL query:
>>> pt.duckdb.Relation("select 1 as a, 2 as b").to_df() shape: (1, 2) ┌─────┬─────┐ │ a ┆ b │ │ --- ┆ --- │ │ i64 ┆ i64 │ ╞═════╪═════╡ │ 1 ┆ 2 │ └─────┴─────┘
Methods & Properties
- add_prefix
- add_suffix
- aggregate
- alias
- all
- case
- cast
- coalesce
- columns
- count
- create_table
- create_view
- distinct
- drop
- except_
- execute
- filter
- get
- inner_join
- insert_into
- intersect
- join
- left_join
- limit
- model
- order
- rename
- select
- set_alias
- set_model
- to_df
- to_pandas
- to_series
- types
- union
- with_columns
- with_missing_defaultable_columns
- with_missing_nullable_columns
- __add__
- __getitem__
- __iter__
- __len__
- __str__