patito 0.4.3__py3-none-any.whl → 0.5.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- patito/__init__.py +12 -6
- patito/database.py +658 -0
- patito/duckdb.py +153 -186
- patito/polars.py +52 -45
- patito/pydantic.py +99 -88
- patito/sql.py +2 -3
- patito/validators.py +87 -1
- patito/xdg.py +22 -0
- {patito-0.4.3.dist-info → patito-0.5.0.dist-info}/LICENSE +1 -0
- {patito-0.4.3.dist-info → patito-0.5.0.dist-info}/METADATA +18 -17
- patito-0.5.0.dist-info/RECORD +14 -0
- {patito-0.4.3.dist-info → patito-0.5.0.dist-info}/WHEEL +1 -1
- patito-0.4.3.dist-info/RECORD +0 -12
patito/duckdb.py
CHANGED
|
@@ -104,7 +104,7 @@ def create_pydantic_model(relation: "duckdb.DuckDBPyRelation") -> Type[Model]:
|
|
|
104
104
|
return create_model( # type: ignore
|
|
105
105
|
relation.alias,
|
|
106
106
|
__base__=Model,
|
|
107
|
-
**pydantic_annotations,
|
|
107
|
+
**pydantic_annotations, # pyright: ignore
|
|
108
108
|
)
|
|
109
109
|
|
|
110
110
|
|
|
@@ -115,7 +115,7 @@ def _enum_type_name(field_properties: dict) -> str:
|
|
|
115
115
|
The same enum values, regardless of ordering, will always be given the same name.
|
|
116
116
|
"""
|
|
117
117
|
enum_values = ", ".join(repr(value) for value in sorted(field_properties["enum"]))
|
|
118
|
-
value_hash = hashlib.md5(enum_values.encode("utf-8")).hexdigest() # noqa:
|
|
118
|
+
value_hash = hashlib.md5(enum_values.encode("utf-8")).hexdigest() # noqa: #S303
|
|
119
119
|
return f"enum__{value_hash}"
|
|
120
120
|
|
|
121
121
|
|
|
@@ -170,24 +170,24 @@ class Relation(Generic[ModelType]):
|
|
|
170
170
|
The path must point to an existing file with either a ``.csv``
|
|
171
171
|
or ``.parquet`` file extension.
|
|
172
172
|
- A native DuckDB relation object (``duckdb.DuckDBPyRelation``).
|
|
173
|
-
- A ``patito.Relation`` object.
|
|
173
|
+
- A ``patito.duckdb.Relation`` object.
|
|
174
174
|
|
|
175
175
|
database: Which database to load the relation into. If not provided,
|
|
176
176
|
the default DuckDB database will be used.
|
|
177
177
|
|
|
178
178
|
model: Sub-class of ``patito.Model`` which specifies how to deserialize rows
|
|
179
|
-
when fetched with methods such as
|
|
180
|
-
and ``__iter__()``.
|
|
179
|
+
when fetched with methods such as
|
|
180
|
+
:ref:`Relation.get()<duckdb.Relation.get>` and ``__iter__()``.
|
|
181
181
|
|
|
182
182
|
Will also be used to create a strict table schema if
|
|
183
|
-
:ref:`Relation.create_table()<Relation.create_table>`.
|
|
183
|
+
:ref:`Relation.create_table()<duckdb.Relation.create_table>`.
|
|
184
184
|
schema should be constructed.
|
|
185
185
|
|
|
186
|
-
If not provided, a dynamic model fitting the relation schema will be
|
|
187
|
-
when required.
|
|
186
|
+
If not provided, a dynamic model fitting the relation schema will be
|
|
187
|
+
created when required.
|
|
188
188
|
|
|
189
189
|
Can also be set later dynamically by invoking
|
|
190
|
-
:ref:`Relation.set_model()<Relation.set_model>`.
|
|
190
|
+
:ref:`Relation.set_model()<duckdb.Relation.set_model>`.
|
|
191
191
|
|
|
192
192
|
Raises:
|
|
193
193
|
ValueError: If any one of the following cases are encountered:
|
|
@@ -204,7 +204,7 @@ class Relation(Generic[ModelType]):
|
|
|
204
204
|
|
|
205
205
|
>>> import patito as pt
|
|
206
206
|
>>> df = pt.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
|
|
207
|
-
>>> pt.Relation(df).filter("a > 2").to_df()
|
|
207
|
+
>>> pt.duckdb.Relation(df).filter("a > 2").to_df()
|
|
208
208
|
shape: (1, 2)
|
|
209
209
|
┌─────┬─────┐
|
|
210
210
|
│ a ┆ b │
|
|
@@ -216,7 +216,7 @@ class Relation(Generic[ModelType]):
|
|
|
216
216
|
|
|
217
217
|
Instantiated from an SQL query:
|
|
218
218
|
|
|
219
|
-
>>> pt.Relation("select 1 as a, 2 as b").to_df()
|
|
219
|
+
>>> pt.duckdb.Relation("select 1 as a, 2 as b").to_df()
|
|
220
220
|
shape: (1, 2)
|
|
221
221
|
┌─────┬─────┐
|
|
222
222
|
│ a ┆ b │
|
|
@@ -296,7 +296,7 @@ class Relation(Generic[ModelType]):
|
|
|
296
296
|
Examples:
|
|
297
297
|
>>> import patito as pt
|
|
298
298
|
>>> df = pt.DataFrame({"a": [1, 2, 3], "b": ["X", "Y", "X"]})
|
|
299
|
-
>>> relation = pt.Relation(df)
|
|
299
|
+
>>> relation = pt.duckdb.Relation(df)
|
|
300
300
|
>>> relation.aggregate(
|
|
301
301
|
... "b",
|
|
302
302
|
... "sum(a)",
|
|
@@ -311,7 +311,6 @@ class Relation(Generic[ModelType]):
|
|
|
311
311
|
│ str ┆ f64 ┆ str ┆ i64 │
|
|
312
312
|
╞═════╪════════╪═════════════╪═══════╡
|
|
313
313
|
│ X ┆ 4.0 ┆ X ┆ 3 │
|
|
314
|
-
├╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
|
|
315
314
|
│ Y ┆ 2.0 ┆ Y ┆ 2 │
|
|
316
315
|
└─────┴────────┴─────────────┴───────┘
|
|
317
316
|
"""
|
|
@@ -347,7 +346,7 @@ class Relation(Generic[ModelType]):
|
|
|
347
346
|
|
|
348
347
|
Examples:
|
|
349
348
|
>>> import patito as pt
|
|
350
|
-
>>> relation = pt.Relation("select 1 as column_1, 2 as column_2")
|
|
349
|
+
>>> relation = pt.duckdb.Relation("select 1 as column_1, 2 as column_2")
|
|
351
350
|
>>> relation.add_suffix("_renamed").to_df()
|
|
352
351
|
shape: (1, 2)
|
|
353
352
|
┌──────────────────┬──────────────────┐
|
|
@@ -413,7 +412,7 @@ class Relation(Generic[ModelType]):
|
|
|
413
412
|
|
|
414
413
|
Examples:
|
|
415
414
|
>>> import patito as pt
|
|
416
|
-
>>> relation = pt.Relation("select 1 as column_1, 2 as column_2")
|
|
415
|
+
>>> relation = pt.duckdb.Relation("select 1 as column_1, 2 as column_2")
|
|
417
416
|
>>> relation.add_prefix("renamed_").to_df()
|
|
418
417
|
shape: (1, 2)
|
|
419
418
|
┌──────────────────┬──────────────────┐
|
|
@@ -464,8 +463,8 @@ class Relation(Generic[ModelType]):
|
|
|
464
463
|
"""
|
|
465
464
|
Return ``True`` if the given predicate(s) are true for all rows in the relation.
|
|
466
465
|
|
|
467
|
-
See :
|
|
468
|
-
|
|
466
|
+
See :func:`Relation.filter()` for additional information regarding the
|
|
467
|
+
parameters.
|
|
469
468
|
|
|
470
469
|
Args:
|
|
471
470
|
filters: SQL predicates to satisfy.
|
|
@@ -480,7 +479,7 @@ class Relation(Generic[ModelType]):
|
|
|
480
479
|
... "zero": [0, 0, 0],
|
|
481
480
|
... }
|
|
482
481
|
... )
|
|
483
|
-
>>> relation = pt.Relation(df)
|
|
482
|
+
>>> relation = pt.duckdb.Relation(df)
|
|
484
483
|
>>> relation.all(zero=0)
|
|
485
484
|
True
|
|
486
485
|
>>> relation.all(
|
|
@@ -520,14 +519,14 @@ class Relation(Generic[ModelType]):
|
|
|
520
519
|
The following case statement...
|
|
521
520
|
|
|
522
521
|
>>> import patito as pt
|
|
523
|
-
>>> db = pt.Database()
|
|
522
|
+
>>> db = pt.duckdb.Database()
|
|
524
523
|
>>> relation = db.to_relation("select 1 as a union select 2 as a")
|
|
525
524
|
>>> relation.case(
|
|
526
525
|
... from_column="a",
|
|
527
526
|
... to_column="b",
|
|
528
527
|
... mapping={1: "one", 2: "two"},
|
|
529
528
|
... default="three",
|
|
530
|
-
... ).to_df()
|
|
529
|
+
... ).order(by="a").to_df()
|
|
531
530
|
shape: (2, 2)
|
|
532
531
|
┌─────┬─────┐
|
|
533
532
|
│ a ┆ b │
|
|
@@ -535,7 +534,6 @@ class Relation(Generic[ModelType]):
|
|
|
535
534
|
│ i64 ┆ str │
|
|
536
535
|
╞═════╪═════╡
|
|
537
536
|
│ 1 ┆ one │
|
|
538
|
-
├╌╌╌╌╌┼╌╌╌╌╌┤
|
|
539
537
|
│ 2 ┆ two │
|
|
540
538
|
└─────┴─────┘
|
|
541
539
|
|
|
@@ -547,7 +545,7 @@ class Relation(Generic[ModelType]):
|
|
|
547
545
|
... default="three",
|
|
548
546
|
... as_column="b",
|
|
549
547
|
... )
|
|
550
|
-
>>> relation.select(f"*, {case_statement}").to_df()
|
|
548
|
+
>>> relation.select(f"*, {case_statement}").order(by="a").to_df()
|
|
551
549
|
shape: (2, 2)
|
|
552
550
|
┌─────┬─────┐
|
|
553
551
|
│ a ┆ b │
|
|
@@ -555,7 +553,6 @@ class Relation(Generic[ModelType]):
|
|
|
555
553
|
│ i64 ┆ str │
|
|
556
554
|
╞═════╪═════╡
|
|
557
555
|
│ 1 ┆ one │
|
|
558
|
-
├╌╌╌╌╌┼╌╌╌╌╌┤
|
|
559
556
|
│ 2 ┆ two │
|
|
560
557
|
└─────┴─────┘
|
|
561
558
|
"""
|
|
@@ -580,15 +577,15 @@ class Relation(Generic[ModelType]):
|
|
|
580
577
|
Cast the columns of the relation to types compatible with the associated model.
|
|
581
578
|
|
|
582
579
|
The associated model must either be set by invoking
|
|
583
|
-
:ref:`Relation.set_model() <Relation.set_model>` or provided with the
|
|
584
|
-
parameter.
|
|
580
|
+
:ref:`Relation.set_model() <duckdb.Relation.set_model>` or provided with the
|
|
581
|
+
``model`` parameter.
|
|
585
582
|
|
|
586
583
|
Any columns of the relation that are not part of the given model schema will be
|
|
587
584
|
left as-is.
|
|
588
585
|
|
|
589
586
|
Args:
|
|
590
|
-
model: If :ref:`Relation.set_model() <Relation.set_model>` has not
|
|
591
|
-
invoked or is intended to be overwritten.
|
|
587
|
+
model: If :ref:`Relation.set_model() <duckdb.Relation.set_model>` has not
|
|
588
|
+
been invoked or is intended to be overwritten.
|
|
592
589
|
strict: If set to ``False``, columns which are technically compliant with
|
|
593
590
|
the specified field type, will not be casted. For example, a column
|
|
594
591
|
annotated with ``int`` is technically compliant with ``SMALLINT``, even
|
|
@@ -607,31 +604,31 @@ class Relation(Generic[ModelType]):
|
|
|
607
604
|
>>> class Schema(pt.Model):
|
|
608
605
|
... float_column: float
|
|
609
606
|
...
|
|
610
|
-
>>> relation = pt.Relation("select 1 as float_column")
|
|
607
|
+
>>> relation = pt.duckdb.Relation("select 1 as float_column")
|
|
611
608
|
>>> relation.types["float_column"]
|
|
612
|
-
|
|
609
|
+
INTEGER
|
|
613
610
|
>>> relation.cast(model=Schema).types["float_column"]
|
|
614
|
-
|
|
611
|
+
DOUBLE
|
|
615
612
|
|
|
616
|
-
>>> relation = pt.Relation("select 1::FLOAT as float_column")
|
|
613
|
+
>>> relation = pt.duckdb.Relation("select 1::FLOAT as float_column")
|
|
617
614
|
>>> relation.cast(model=Schema).types["float_column"]
|
|
618
|
-
|
|
615
|
+
FLOAT
|
|
619
616
|
>>> relation.cast(model=Schema, strict=True).types["float_column"]
|
|
620
|
-
|
|
617
|
+
DOUBLE
|
|
621
618
|
|
|
622
619
|
>>> class Schema(pt.Model):
|
|
623
620
|
... column_1: float
|
|
624
621
|
... column_2: float
|
|
625
622
|
...
|
|
626
|
-
>>> relation = pt.Relation(
|
|
627
|
-
...
|
|
628
|
-
... )
|
|
623
|
+
>>> relation = pt.duckdb.Relation(
|
|
624
|
+
... "select 1 as column_1, 2 as column_2"
|
|
625
|
+
... ).set_model(Schema)
|
|
629
626
|
>>> relation.types
|
|
630
|
-
{'column_1':
|
|
627
|
+
{'column_1': INTEGER, 'column_2': INTEGER}
|
|
631
628
|
>>> relation.cast(include=["column_1"]).types
|
|
632
|
-
{'column_1':
|
|
629
|
+
{'column_1': DOUBLE, 'column_2': INTEGER}
|
|
633
630
|
>>> relation.cast(exclude=["column_1"]).types
|
|
634
|
-
{'column_1':
|
|
631
|
+
{'column_1': INTEGER, 'column_2': DOUBLE}
|
|
635
632
|
"""
|
|
636
633
|
if model is not None:
|
|
637
634
|
relation = self.set_model(model)
|
|
@@ -650,7 +647,8 @@ class Relation(Generic[ModelType]):
|
|
|
650
647
|
|
|
651
648
|
if include is not None and exclude is not None:
|
|
652
649
|
raise ValueError(
|
|
653
|
-
|
|
650
|
+
"Both include and exclude provided to "
|
|
651
|
+
f"{self.__class__.__name__}.cast()!"
|
|
654
652
|
)
|
|
655
653
|
elif include is not None:
|
|
656
654
|
include = set(include)
|
|
@@ -701,7 +699,7 @@ class Relation(Generic[ModelType]):
|
|
|
701
699
|
... "c": [None, 8.0, 9.0],
|
|
702
700
|
... }
|
|
703
701
|
... )
|
|
704
|
-
>>> relation = pt.Relation(df)
|
|
702
|
+
>>> relation = pt.duckdb.Relation(df)
|
|
705
703
|
>>> relation.coalesce(a=2, b="six").to_df()
|
|
706
704
|
shape: (3, 3)
|
|
707
705
|
┌─────┬──────┬──────┐
|
|
@@ -710,9 +708,7 @@ class Relation(Generic[ModelType]):
|
|
|
710
708
|
│ i64 ┆ str ┆ f64 │
|
|
711
709
|
╞═════╪══════╪══════╡
|
|
712
710
|
│ 1 ┆ four ┆ null │
|
|
713
|
-
├╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌┤
|
|
714
711
|
│ 2 ┆ five ┆ 8.0 │
|
|
715
|
-
├╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌┤
|
|
716
712
|
│ 3 ┆ six ┆ 9.0 │
|
|
717
713
|
└─────┴──────┴──────┘
|
|
718
714
|
"""
|
|
@@ -732,7 +728,7 @@ class Relation(Generic[ModelType]):
|
|
|
732
728
|
|
|
733
729
|
Examples:
|
|
734
730
|
>>> import patito as pt
|
|
735
|
-
>>> pt.Relation("select 1 as a, 2 as b").columns
|
|
731
|
+
>>> pt.duckdb.Relation("select 1 as a, 2 as b").columns
|
|
736
732
|
['a', 'b']
|
|
737
733
|
"""
|
|
738
734
|
# Under certain specific circumstances columns are suffixed with
|
|
@@ -748,13 +744,13 @@ class Relation(Generic[ModelType]):
|
|
|
748
744
|
|
|
749
745
|
Examples:
|
|
750
746
|
>>> import patito as pt
|
|
751
|
-
>>> relation = pt.Relation("select 1 as a")
|
|
747
|
+
>>> relation = pt.duckdb.Relation("select 1 as a")
|
|
752
748
|
>>> relation.count()
|
|
753
749
|
1
|
|
754
750
|
>>> (relation + relation).count()
|
|
755
751
|
2
|
|
756
752
|
|
|
757
|
-
The :ref:`Relation.__len__()<Relation.__len__>` method invokes
|
|
753
|
+
The :ref:`Relation.__len__()<duckdb.Relation.__len__>` method invokes
|
|
758
754
|
``Relation.count()`` under the hood, and is equivalent:
|
|
759
755
|
|
|
760
756
|
>>> len(relation)
|
|
@@ -768,9 +764,10 @@ class Relation(Generic[ModelType]):
|
|
|
768
764
|
"""
|
|
769
765
|
Create new database table based on relation.
|
|
770
766
|
|
|
771
|
-
If ``self.model`` is set with
|
|
772
|
-
then the model is used
|
|
773
|
-
Otherwise, a permissive table schema is created based
|
|
767
|
+
If ``self.model`` is set with
|
|
768
|
+
:ref:`Relation.set_model()<duckdb.Relation.set_model>`, then the model is used
|
|
769
|
+
to infer the table schema. Otherwise, a permissive table schema is created based
|
|
770
|
+
on the relation data.
|
|
774
771
|
|
|
775
772
|
Returns:
|
|
776
773
|
Relation: A relation pointing to the newly created table.
|
|
@@ -780,15 +777,15 @@ class Relation(Generic[ModelType]):
|
|
|
780
777
|
>>> import patito as pt
|
|
781
778
|
|
|
782
779
|
>>> df = pt.DataFrame({"enum_column": ["A", "A", "B"]})
|
|
783
|
-
>>> relation = pt.Relation(df)
|
|
780
|
+
>>> relation = pt.duckdb.Relation(df)
|
|
784
781
|
>>> relation.create_table("permissive_table").types
|
|
785
|
-
{'enum_column':
|
|
782
|
+
{'enum_column': VARCHAR}
|
|
786
783
|
|
|
787
784
|
>>> class TableSchema(pt.Model):
|
|
788
785
|
... enum_column: Literal["A", "B", "C"]
|
|
789
786
|
...
|
|
790
787
|
>>> relation.set_model(TableSchema).create_table("strict_table").types
|
|
791
|
-
{'enum_column':
|
|
788
|
+
{'enum_column': enum__7ba49365cc1b0fd57e61088b3bc9aa25}
|
|
792
789
|
"""
|
|
793
790
|
if self.model is not None:
|
|
794
791
|
self.database.create_table(name=name, model=self.model)
|
|
@@ -810,7 +807,7 @@ class Relation(Generic[ModelType]):
|
|
|
810
807
|
|
|
811
808
|
Examples:
|
|
812
809
|
>>> import patito as pt
|
|
813
|
-
>>> db = pt.Database()
|
|
810
|
+
>>> db = pt.duckdb.Database()
|
|
814
811
|
>>> df = pt.DataFrame({"column": ["A", "A", "B"]})
|
|
815
812
|
>>> relation = db.to_relation(df)
|
|
816
813
|
>>> relation.create_view("my_view")
|
|
@@ -822,9 +819,7 @@ class Relation(Generic[ModelType]):
|
|
|
822
819
|
│ str │
|
|
823
820
|
╞════════╡
|
|
824
821
|
│ A │
|
|
825
|
-
├╌╌╌╌╌╌╌╌┤
|
|
826
822
|
│ A │
|
|
827
|
-
├╌╌╌╌╌╌╌╌┤
|
|
828
823
|
│ B │
|
|
829
824
|
└────────┘
|
|
830
825
|
"""
|
|
@@ -840,7 +835,7 @@ class Relation(Generic[ModelType]):
|
|
|
840
835
|
|
|
841
836
|
Examples:
|
|
842
837
|
>>> import patito as pt
|
|
843
|
-
>>> relation = pt.Relation("select 1 as a, 2 as b, 3 as c")
|
|
838
|
+
>>> relation = pt.duckdb.Relation("select 1 as a, 2 as b, 3 as c")
|
|
844
839
|
>>> relation.columns
|
|
845
840
|
['a', 'b', 'c']
|
|
846
841
|
>>> relation.drop("c").columns
|
|
@@ -861,10 +856,10 @@ class Relation(Generic[ModelType]):
|
|
|
861
856
|
>>> import patito as pt
|
|
862
857
|
>>> df = pt.DataFrame(
|
|
863
858
|
... [[1, 2, 3], [1, 2, 3], [3, 2, 1]],
|
|
864
|
-
...
|
|
859
|
+
... schema=["a", "b", "c"],
|
|
865
860
|
... orient="row",
|
|
866
861
|
... )
|
|
867
|
-
>>> relation = pt.Relation(df)
|
|
862
|
+
>>> relation = pt.duckdb.Relation(df)
|
|
868
863
|
>>> relation.to_df()
|
|
869
864
|
shape: (3, 3)
|
|
870
865
|
┌─────┬─────┬─────┐
|
|
@@ -873,9 +868,7 @@ class Relation(Generic[ModelType]):
|
|
|
873
868
|
│ i64 ┆ i64 ┆ i64 │
|
|
874
869
|
╞═════╪═════╪═════╡
|
|
875
870
|
│ 1 ┆ 2 ┆ 3 │
|
|
876
|
-
├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┤
|
|
877
871
|
│ 1 ┆ 2 ┆ 3 │
|
|
878
|
-
├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┤
|
|
879
872
|
│ 3 ┆ 2 ┆ 1 │
|
|
880
873
|
└─────┴─────┴─────┘
|
|
881
874
|
>>> relation.distinct().to_df()
|
|
@@ -886,7 +879,6 @@ class Relation(Generic[ModelType]):
|
|
|
886
879
|
│ i64 ┆ i64 ┆ i64 │
|
|
887
880
|
╞═════╪═════╪═════╡
|
|
888
881
|
│ 1 ┆ 2 ┆ 3 │
|
|
889
|
-
├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┤
|
|
890
882
|
│ 3 ┆ 2 ┆ 1 │
|
|
891
883
|
└─────┴─────┴─────┘
|
|
892
884
|
"""
|
|
@@ -904,8 +896,10 @@ class Relation(Generic[ModelType]):
|
|
|
904
896
|
|
|
905
897
|
Example:
|
|
906
898
|
>>> import patito as pt
|
|
907
|
-
>>> relation_123 = pt.Relation(
|
|
908
|
-
|
|
899
|
+
>>> relation_123 = pt.duckdb.Relation(
|
|
900
|
+
... "select 1 union select 2 union select 3"
|
|
901
|
+
... )
|
|
902
|
+
>>> relation_123.order(by="1").to_df()
|
|
909
903
|
shape: (3, 1)
|
|
910
904
|
┌─────┐
|
|
911
905
|
│ 1 │
|
|
@@ -913,12 +907,10 @@ class Relation(Generic[ModelType]):
|
|
|
913
907
|
│ i64 │
|
|
914
908
|
╞═════╡
|
|
915
909
|
│ 1 │
|
|
916
|
-
├╌╌╌╌╌┤
|
|
917
910
|
│ 2 │
|
|
918
|
-
├╌╌╌╌╌┤
|
|
919
911
|
│ 3 │
|
|
920
912
|
└─────┘
|
|
921
|
-
>>> relation_2 = pt.Relation("select 2")
|
|
913
|
+
>>> relation_2 = pt.duckdb.Relation("select 2")
|
|
922
914
|
>>> relation_2.to_df()
|
|
923
915
|
shape: (1, 1)
|
|
924
916
|
┌─────┐
|
|
@@ -928,7 +920,7 @@ class Relation(Generic[ModelType]):
|
|
|
928
920
|
╞═════╡
|
|
929
921
|
│ 2 │
|
|
930
922
|
└─────┘
|
|
931
|
-
>>> relation_123.except_(relation_2).to_df()
|
|
923
|
+
>>> relation_123.except_(relation_2).order(by="1").to_df()
|
|
932
924
|
shape: (2, 1)
|
|
933
925
|
┌─────┐
|
|
934
926
|
│ 1 │
|
|
@@ -936,7 +928,6 @@ class Relation(Generic[ModelType]):
|
|
|
936
928
|
│ i64 │
|
|
937
929
|
╞═════╡
|
|
938
930
|
│ 1 │
|
|
939
|
-
├╌╌╌╌╌┤
|
|
940
931
|
│ 3 │
|
|
941
932
|
└─────┘
|
|
942
933
|
"""
|
|
@@ -945,7 +936,7 @@ class Relation(Generic[ModelType]):
|
|
|
945
936
|
schema_change=False,
|
|
946
937
|
)
|
|
947
938
|
|
|
948
|
-
def execute(self) -> duckdb.
|
|
939
|
+
def execute(self) -> duckdb.DuckDBPyRelation:
|
|
949
940
|
"""
|
|
950
941
|
Execute built relation query and return result object.
|
|
951
942
|
|
|
@@ -954,11 +945,11 @@ class Relation(Generic[ModelType]):
|
|
|
954
945
|
|
|
955
946
|
Examples:
|
|
956
947
|
>>> import patito as pt
|
|
957
|
-
>>> relation = pt.Relation(
|
|
948
|
+
>>> relation = pt.duckdb.Relation(
|
|
958
949
|
... "select 1 as a, 2 as b union select 3 as a, 4 as b"
|
|
959
950
|
... )
|
|
960
951
|
>>> result = relation.aggregate("sum(a)", group_by="").execute()
|
|
961
|
-
>>> result.description
|
|
952
|
+
>>> result.description
|
|
962
953
|
[('sum(a)', 'NUMBER', None, None, None, None, None)]
|
|
963
954
|
>>> result.fetchall()
|
|
964
955
|
[(4,)]
|
|
@@ -989,7 +980,7 @@ class Relation(Generic[ModelType]):
|
|
|
989
980
|
>>> import patito as pt
|
|
990
981
|
>>> import polars as pl
|
|
991
982
|
>>> df = pt.DataFrame({"product_id": [1, 2, 3], "price": [10, 10, 20]})
|
|
992
|
-
>>> relation = pt.Relation(df).set_alias("my_relation")
|
|
983
|
+
>>> relation = pt.duckdb.Relation(df).set_alias("my_relation")
|
|
993
984
|
|
|
994
985
|
The ``.get()`` method will by default return a dynamically constructed
|
|
995
986
|
Patito model if no model has been associated with the given relation:
|
|
@@ -998,8 +989,8 @@ class Relation(Generic[ModelType]):
|
|
|
998
989
|
my_relation(product_id=1, price=10)
|
|
999
990
|
|
|
1000
991
|
If a Patito model has been associated with the relation, by the use of
|
|
1001
|
-
:ref:`Relation.set_model()<Relation.set_model>`, then the given model
|
|
1002
|
-
be used to represent the return type:
|
|
992
|
+
:ref:`Relation.set_model()<duckdb.Relation.set_model>`, then the given model
|
|
993
|
+
will be used to represent the return type:
|
|
1003
994
|
|
|
1004
995
|
>>> class Product(pt.Model):
|
|
1005
996
|
... product_id: int = pt.Field(unique=True)
|
|
@@ -1039,7 +1030,7 @@ class Relation(Generic[ModelType]):
|
|
|
1039
1030
|
else:
|
|
1040
1031
|
relation = self
|
|
1041
1032
|
result = relation.execute()
|
|
1042
|
-
row =
|
|
1033
|
+
row = result.fetchone()
|
|
1043
1034
|
if row is None or result.fetchone() is not None:
|
|
1044
1035
|
args = [repr(f) for f in filters]
|
|
1045
1036
|
args.extend(f"{key}={value!r}" for key, value in equalities.items())
|
|
@@ -1099,7 +1090,7 @@ class Relation(Generic[ModelType]):
|
|
|
1099
1090
|
... "string": ["A", "A", "B", "B"],
|
|
1100
1091
|
... }
|
|
1101
1092
|
... )
|
|
1102
|
-
>>> relation = pt.Relation(df)
|
|
1093
|
+
>>> relation = pt.duckdb.Relation(df)
|
|
1103
1094
|
>>> relation.filter("number % 2 = 0").to_df()
|
|
1104
1095
|
shape: (2, 2)
|
|
1105
1096
|
┌────────┬────────┐
|
|
@@ -1108,7 +1099,6 @@ class Relation(Generic[ModelType]):
|
|
|
1108
1099
|
│ i64 ┆ str │
|
|
1109
1100
|
╞════════╪════════╡
|
|
1110
1101
|
│ 2 ┆ A │
|
|
1111
|
-
├╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┤
|
|
1112
1102
|
│ 4 ┆ B │
|
|
1113
1103
|
└────────┴────────┘
|
|
1114
1104
|
|
|
@@ -1143,8 +1133,8 @@ class Relation(Generic[ModelType]):
|
|
|
1143
1133
|
"""
|
|
1144
1134
|
Join relation with other relation source based on condition.
|
|
1145
1135
|
|
|
1146
|
-
See :ref:`Relation.inner_join() <Relation.inner_join>` and
|
|
1147
|
-
:ref:`Relation.left_join() <Relation.left_join>` for alternative method
|
|
1136
|
+
See :ref:`duckdb.Relation.inner_join() <duckdb.Relation.inner_join>` and
|
|
1137
|
+
:ref:`Relation.left_join() <duckdb.Relation.left_join>` for alternative method
|
|
1148
1138
|
shortcuts instead of using ``how``.
|
|
1149
1139
|
|
|
1150
1140
|
Args:
|
|
@@ -1165,14 +1155,14 @@ class Relation(Generic[ModelType]):
|
|
|
1165
1155
|
... "supplier_id": [2, 1, 3],
|
|
1166
1156
|
... }
|
|
1167
1157
|
... )
|
|
1168
|
-
>>> products = pt.Relation(products_df)
|
|
1158
|
+
>>> products = pt.duckdb.Relation(products_df)
|
|
1169
1159
|
>>> supplier_df = pt.DataFrame(
|
|
1170
1160
|
... {
|
|
1171
1161
|
... "id": [1, 2],
|
|
1172
1162
|
... "supplier_name": ["Banana Republic", "Applies Inc."],
|
|
1173
1163
|
... }
|
|
1174
1164
|
... )
|
|
1175
|
-
>>> suppliers = pt.Relation(supplier_df)
|
|
1165
|
+
>>> suppliers = pt.duckdb.Relation(supplier_df)
|
|
1176
1166
|
>>> products.set_alias("p").join(
|
|
1177
1167
|
... suppliers.set_alias("s"),
|
|
1178
1168
|
... on="p.supplier_id = s.id",
|
|
@@ -1185,7 +1175,6 @@ class Relation(Generic[ModelType]):
|
|
|
1185
1175
|
│ str ┆ i64 ┆ i64 ┆ str │
|
|
1186
1176
|
╞══════════════╪═════════════╪═════╪═════════════════╡
|
|
1187
1177
|
│ apple ┆ 2 ┆ 2 ┆ Applies Inc. │
|
|
1188
|
-
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
|
|
1189
1178
|
│ banana ┆ 1 ┆ 1 ┆ Banana Republic │
|
|
1190
1179
|
└──────────────┴─────────────┴─────┴─────────────────┘
|
|
1191
1180
|
|
|
@@ -1201,9 +1190,7 @@ class Relation(Generic[ModelType]):
|
|
|
1201
1190
|
│ str ┆ i64 ┆ i64 ┆ str │
|
|
1202
1191
|
╞══════════════╪═════════════╪══════╪═════════════════╡
|
|
1203
1192
|
│ apple ┆ 2 ┆ 2 ┆ Applies Inc. │
|
|
1204
|
-
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
|
|
1205
1193
|
│ banana ┆ 1 ┆ 1 ┆ Banana Republic │
|
|
1206
|
-
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
|
|
1207
1194
|
│ oranges ┆ 3 ┆ null ┆ null │
|
|
1208
1195
|
└──────────────┴─────────────┴──────┴─────────────────┘
|
|
1209
1196
|
"""
|
|
@@ -1234,14 +1221,14 @@ class Relation(Generic[ModelType]):
|
|
|
1234
1221
|
... "supplier_id": [2, 1, 3],
|
|
1235
1222
|
... }
|
|
1236
1223
|
... )
|
|
1237
|
-
>>> products = pt.Relation(products_df)
|
|
1224
|
+
>>> products = pt.duckdb.Relation(products_df)
|
|
1238
1225
|
>>> supplier_df = pt.DataFrame(
|
|
1239
1226
|
... {
|
|
1240
1227
|
... "id": [1, 2],
|
|
1241
1228
|
... "supplier_name": ["Banana Republic", "Applies Inc."],
|
|
1242
1229
|
... }
|
|
1243
1230
|
... )
|
|
1244
|
-
>>> suppliers = pt.Relation(supplier_df)
|
|
1231
|
+
>>> suppliers = pt.duckdb.Relation(supplier_df)
|
|
1245
1232
|
>>> products.set_alias("p").inner_join(
|
|
1246
1233
|
... suppliers.set_alias("s"),
|
|
1247
1234
|
... on="p.supplier_id = s.id",
|
|
@@ -1253,7 +1240,6 @@ class Relation(Generic[ModelType]):
|
|
|
1253
1240
|
│ str ┆ i64 ┆ i64 ┆ str │
|
|
1254
1241
|
╞══════════════╪═════════════╪═════╪═════════════════╡
|
|
1255
1242
|
│ apple ┆ 2 ┆ 2 ┆ Applies Inc. │
|
|
1256
|
-
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
|
|
1257
1243
|
│ banana ┆ 1 ┆ 1 ┆ Banana Republic │
|
|
1258
1244
|
└──────────────┴─────────────┴─────┴─────────────────┘
|
|
1259
1245
|
"""
|
|
@@ -1286,14 +1272,14 @@ class Relation(Generic[ModelType]):
|
|
|
1286
1272
|
... "supplier_id": [2, 1, 3],
|
|
1287
1273
|
... }
|
|
1288
1274
|
... )
|
|
1289
|
-
>>> products = pt.Relation(products_df)
|
|
1275
|
+
>>> products = pt.duckdb.Relation(products_df)
|
|
1290
1276
|
>>> supplier_df = pt.DataFrame(
|
|
1291
1277
|
... {
|
|
1292
1278
|
... "id": [1, 2],
|
|
1293
1279
|
... "supplier_name": ["Banana Republic", "Applies Inc."],
|
|
1294
1280
|
... }
|
|
1295
1281
|
... )
|
|
1296
|
-
>>> suppliers = pt.Relation(supplier_df)
|
|
1282
|
+
>>> suppliers = pt.duckdb.Relation(supplier_df)
|
|
1297
1283
|
>>> products.set_alias("p").left_join(
|
|
1298
1284
|
... suppliers.set_alias("s"),
|
|
1299
1285
|
... on="p.supplier_id = s.id",
|
|
@@ -1305,9 +1291,7 @@ class Relation(Generic[ModelType]):
|
|
|
1305
1291
|
│ str ┆ i64 ┆ i64 ┆ str │
|
|
1306
1292
|
╞══════════════╪═════════════╪══════╪═════════════════╡
|
|
1307
1293
|
│ apple ┆ 2 ┆ 2 ┆ Applies Inc. │
|
|
1308
|
-
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
|
|
1309
1294
|
│ banana ┆ 1 ┆ 1 ┆ Banana Republic │
|
|
1310
|
-
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
|
|
1311
1295
|
│ oranges ┆ 3 ┆ null ┆ null │
|
|
1312
1296
|
└──────────────┴─────────────┴──────┴─────────────────┘
|
|
1313
1297
|
"""
|
|
@@ -1335,10 +1319,10 @@ class Relation(Generic[ModelType]):
|
|
|
1335
1319
|
Example:
|
|
1336
1320
|
>>> import patito as pt
|
|
1337
1321
|
>>> relation = (
|
|
1338
|
-
... pt.Relation("select 1 as column")
|
|
1339
|
-
... + pt.Relation("select 2 as column")
|
|
1340
|
-
... + pt.Relation("select 3 as column")
|
|
1341
|
-
... + pt.Relation("select 4 as column")
|
|
1322
|
+
... pt.duckdb.Relation("select 1 as column")
|
|
1323
|
+
... + pt.duckdb.Relation("select 2 as column")
|
|
1324
|
+
... + pt.duckdb.Relation("select 3 as column")
|
|
1325
|
+
... + pt.duckdb.Relation("select 4 as column")
|
|
1342
1326
|
... )
|
|
1343
1327
|
>>> relation.limit(2).to_df()
|
|
1344
1328
|
shape: (2, 1)
|
|
@@ -1348,7 +1332,6 @@ class Relation(Generic[ModelType]):
|
|
|
1348
1332
|
│ i64 │
|
|
1349
1333
|
╞════════╡
|
|
1350
1334
|
│ 1 │
|
|
1351
|
-
├╌╌╌╌╌╌╌╌┤
|
|
1352
1335
|
│ 2 │
|
|
1353
1336
|
└────────┘
|
|
1354
1337
|
>>> relation.limit(2, offset=2).to_df()
|
|
@@ -1359,7 +1342,6 @@ class Relation(Generic[ModelType]):
|
|
|
1359
1342
|
│ i64 │
|
|
1360
1343
|
╞════════╡
|
|
1361
1344
|
│ 3 │
|
|
1362
|
-
├╌╌╌╌╌╌╌╌┤
|
|
1363
1345
|
│ 4 │
|
|
1364
1346
|
└────────┘
|
|
1365
1347
|
"""
|
|
@@ -1392,14 +1374,11 @@ class Relation(Generic[ModelType]):
|
|
|
1392
1374
|
│ str ┆ i64 │
|
|
1393
1375
|
╞═════════╪═════╡
|
|
1394
1376
|
│ Alice ┆ 20 │
|
|
1395
|
-
├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┤
|
|
1396
1377
|
│ Bob ┆ 20 │
|
|
1397
|
-
├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┤
|
|
1398
1378
|
│ Charles ┆ 30 │
|
|
1399
|
-
├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┤
|
|
1400
1379
|
│ Diana ┆ 35 │
|
|
1401
1380
|
└─────────┴─────┘
|
|
1402
|
-
>>> relation = pt.Relation(df)
|
|
1381
|
+
>>> relation = pt.duckdb.Relation(df)
|
|
1403
1382
|
>>> relation.order(by="age desc").to_df()
|
|
1404
1383
|
shape: (4, 2)
|
|
1405
1384
|
┌─────────┬─────┐
|
|
@@ -1408,11 +1387,8 @@ class Relation(Generic[ModelType]):
|
|
|
1408
1387
|
│ str ┆ i64 │
|
|
1409
1388
|
╞═════════╪═════╡
|
|
1410
1389
|
│ Diana ┆ 35 │
|
|
1411
|
-
├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┤
|
|
1412
1390
|
│ Charles ┆ 30 │
|
|
1413
|
-
├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┤
|
|
1414
1391
|
│ Alice ┆ 20 │
|
|
1415
|
-
├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┤
|
|
1416
1392
|
│ Bob ┆ 20 │
|
|
1417
1393
|
└─────────┴─────┘
|
|
1418
1394
|
>>> relation.order(by=["age desc", "name desc"]).to_df()
|
|
@@ -1423,11 +1399,8 @@ class Relation(Generic[ModelType]):
|
|
|
1423
1399
|
│ str ┆ i64 │
|
|
1424
1400
|
╞═════════╪═════╡
|
|
1425
1401
|
│ Diana ┆ 35 │
|
|
1426
|
-
├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┤
|
|
1427
1402
|
│ Charles ┆ 30 │
|
|
1428
|
-
├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┤
|
|
1429
1403
|
│ Bob ┆ 20 │
|
|
1430
|
-
├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┤
|
|
1431
1404
|
│ Alice ┆ 20 │
|
|
1432
1405
|
└─────────┴─────┘
|
|
1433
1406
|
"""
|
|
@@ -1456,7 +1429,7 @@ class Relation(Generic[ModelType]):
|
|
|
1456
1429
|
|
|
1457
1430
|
Examples:
|
|
1458
1431
|
>>> import patito as pt
|
|
1459
|
-
>>> db = pt.Database()
|
|
1432
|
+
>>> db = pt.duckdb.Database()
|
|
1460
1433
|
>>> db.to_relation("select 1 as a").create_table("my_table")
|
|
1461
1434
|
>>> db.table("my_table").to_df()
|
|
1462
1435
|
shape: (1, 1)
|
|
@@ -1476,7 +1449,6 @@ class Relation(Generic[ModelType]):
|
|
|
1476
1449
|
│ i64 │
|
|
1477
1450
|
╞═════╡
|
|
1478
1451
|
│ 1 │
|
|
1479
|
-
├╌╌╌╌╌┤
|
|
1480
1452
|
│ 2 │
|
|
1481
1453
|
└─────┘
|
|
1482
1454
|
"""
|
|
@@ -1509,7 +1481,7 @@ class Relation(Generic[ModelType]):
|
|
|
1509
1481
|
>>> import patito as pt
|
|
1510
1482
|
>>> df1 = pt.DataFrame({"a": [1, 1, 2], "b": [1, 1, 2]})
|
|
1511
1483
|
>>> df2 = pt.DataFrame({"a": [1, 1, 3], "b": [1, 1, 3]})
|
|
1512
|
-
>>> pt.Relation(df1).intersect(pt.Relation(df2)).to_df()
|
|
1484
|
+
>>> pt.duckdb.Relation(df1).intersect(pt.duckdb.Relation(df2)).to_df()
|
|
1513
1485
|
shape: (1, 2)
|
|
1514
1486
|
┌─────┬─────┐
|
|
1515
1487
|
│ a ┆ b │
|
|
@@ -1546,7 +1518,7 @@ class Relation(Generic[ModelType]):
|
|
|
1546
1518
|
|
|
1547
1519
|
Examples:
|
|
1548
1520
|
>>> import patito as pt
|
|
1549
|
-
>>> db = pt.Database()
|
|
1521
|
+
>>> db = pt.duckdb.Database()
|
|
1550
1522
|
>>> relation = db.to_relation(pt.DataFrame({"original_column": [1, 2, 3]}))
|
|
1551
1523
|
>>> relation.select("*").to_df()
|
|
1552
1524
|
shape: (3, 1)
|
|
@@ -1556,9 +1528,7 @@ class Relation(Generic[ModelType]):
|
|
|
1556
1528
|
│ i64 │
|
|
1557
1529
|
╞═════════════════╡
|
|
1558
1530
|
│ 1 │
|
|
1559
|
-
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
|
|
1560
1531
|
│ 2 │
|
|
1561
|
-
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
|
|
1562
1532
|
│ 3 │
|
|
1563
1533
|
└─────────────────┘
|
|
1564
1534
|
>>> relation.select("*", multiplied_column="2 * original_column").to_df()
|
|
@@ -1569,9 +1539,7 @@ class Relation(Generic[ModelType]):
|
|
|
1569
1539
|
│ i64 ┆ i64 │
|
|
1570
1540
|
╞═════════════════╪═══════════════════╡
|
|
1571
1541
|
│ 1 ┆ 2 │
|
|
1572
|
-
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
|
|
1573
1542
|
│ 2 ┆ 4 │
|
|
1574
|
-
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
|
|
1575
1543
|
│ 3 ┆ 6 │
|
|
1576
1544
|
└─────────────────┴───────────────────┘
|
|
1577
1545
|
"""
|
|
@@ -1624,7 +1592,7 @@ class Relation(Generic[ModelType]):
|
|
|
1624
1592
|
|
|
1625
1593
|
Examples:
|
|
1626
1594
|
>>> import patito as pt
|
|
1627
|
-
>>> relation = pt.Relation("select 1 as a, 2 as b")
|
|
1595
|
+
>>> relation = pt.duckdb.Relation("select 1 as a, 2 as b")
|
|
1628
1596
|
>>> relation.rename(b="c").to_df().select(["a", "c"])
|
|
1629
1597
|
shape: (1, 2)
|
|
1630
1598
|
┌─────┬─────┐
|
|
@@ -1666,8 +1634,8 @@ class Relation(Generic[ModelType]):
|
|
|
1666
1634
|
|
|
1667
1635
|
Example:
|
|
1668
1636
|
>>> import patito as pt
|
|
1669
|
-
>>> relation_1 = pt.Relation("select 1 as a, 2 as b")
|
|
1670
|
-
>>> relation_2 = pt.Relation("select 1 as a, 3 as c")
|
|
1637
|
+
>>> relation_1 = pt.duckdb.Relation("select 1 as a, 2 as b")
|
|
1638
|
+
>>> relation_2 = pt.duckdb.Relation("select 1 as a, 3 as c")
|
|
1671
1639
|
>>> relation_1.set_alias("x").inner_join(
|
|
1672
1640
|
... relation_2.set_alias("y"),
|
|
1673
1641
|
... on="x.a = y.a",
|
|
@@ -1691,9 +1659,9 @@ class Relation(Generic[ModelType]):
|
|
|
1691
1659
|
Associate a give Patito model with the relation.
|
|
1692
1660
|
|
|
1693
1661
|
The returned relation has an associated ``.model`` attribute which can in turn
|
|
1694
|
-
be used by several methods such as :ref:`Relation.get()<Relation.get>`,
|
|
1695
|
-
:ref:`Relation.create_table()<Relation.create_table>`, and
|
|
1696
|
-
:ref:`Relation.__iter__<Relation.__iter__>`.
|
|
1662
|
+
be used by several methods such as :ref:`Relation.get()<duckdb.Relation.get>`,
|
|
1663
|
+
:ref:`Relation.create_table()<duckdb.Relation.create_table>`, and
|
|
1664
|
+
:ref:`Relation.__iter__<duckdb.Relation.__iter__>`.
|
|
1697
1665
|
|
|
1698
1666
|
Args:
|
|
1699
1667
|
model: A Patito Model class specifying the intended schema of the relation.
|
|
@@ -1708,16 +1676,18 @@ class Relation(Generic[ModelType]):
|
|
|
1708
1676
|
... float_column: float
|
|
1709
1677
|
... enum_column: Literal["A", "B", "C"]
|
|
1710
1678
|
...
|
|
1711
|
-
>>> relation = pt.Relation(
|
|
1679
|
+
>>> relation = pt.duckdb.Relation(
|
|
1680
|
+
... "select 1 as float_column, 'A' as enum_column"
|
|
1681
|
+
... )
|
|
1712
1682
|
>>> relation.get()
|
|
1713
1683
|
query_relation(float_column=1, enum_column='A')
|
|
1714
1684
|
>>> relation.set_model(MySchema).get()
|
|
1715
1685
|
MySchema(float_column=1.0, enum_column='A')
|
|
1716
1686
|
>>> relation.create_table("unmodeled_table").types
|
|
1717
|
-
{'float_column':
|
|
1687
|
+
{'float_column': INTEGER, 'enum_column': VARCHAR}
|
|
1718
1688
|
>>> relation.set_model(MySchema).create_table("modeled_table").types
|
|
1719
|
-
{'float_column':
|
|
1720
|
-
'enum_column':
|
|
1689
|
+
{'float_column': DOUBLE,
|
|
1690
|
+
'enum_column': enum__7ba49365cc1b0fd57e61088b3bc9aa25}
|
|
1721
1691
|
"""
|
|
1722
1692
|
# We are not able to annotate the generic instance of type(self)[type(model)]
|
|
1723
1693
|
# due to the lack of higher-kinded generics in python as of this writing.
|
|
@@ -1733,7 +1703,7 @@ class Relation(Generic[ModelType]):
|
|
|
1733
1703
|
)
|
|
1734
1704
|
|
|
1735
1705
|
@property
|
|
1736
|
-
def types(self)
|
|
1706
|
+
def types(self): # type: ignore[no-untyped-def] # noqa
|
|
1737
1707
|
"""
|
|
1738
1708
|
Return the SQL types of all the columns of the given relation.
|
|
1739
1709
|
|
|
@@ -1743,8 +1713,8 @@ class Relation(Generic[ModelType]):
|
|
|
1743
1713
|
|
|
1744
1714
|
Examples:
|
|
1745
1715
|
>>> import patito as pt
|
|
1746
|
-
>>> pt.Relation("select 1 as a, 'my_value' as b").types
|
|
1747
|
-
{'a':
|
|
1716
|
+
>>> pt.duckdb.Relation("select 1 as a, 'my_value' as b").types
|
|
1717
|
+
{'a': INTEGER, 'b': VARCHAR}
|
|
1748
1718
|
"""
|
|
1749
1719
|
return dict(zip(self.columns, self._relation.types))
|
|
1750
1720
|
|
|
@@ -1756,8 +1726,10 @@ class Relation(Generic[ModelType]):
|
|
|
1756
1726
|
|
|
1757
1727
|
Example:
|
|
1758
1728
|
>>> import patito as pt
|
|
1759
|
-
>>> pt.Relation("select 1 as column union select 2 as column").
|
|
1760
|
-
|
|
1729
|
+
>>> pt.duckdb.Relation("select 1 as column union select 2 as column").order(
|
|
1730
|
+
... by="1"
|
|
1731
|
+
... ).to_pandas()
|
|
1732
|
+
column
|
|
1761
1733
|
0 1
|
|
1762
1734
|
1 2
|
|
1763
1735
|
"""
|
|
@@ -1771,7 +1743,9 @@ class Relation(Generic[ModelType]):
|
|
|
1771
1743
|
|
|
1772
1744
|
Example:
|
|
1773
1745
|
>>> import patito as pt
|
|
1774
|
-
>>> pt.Relation("select 1 as column union select 2 as column").
|
|
1746
|
+
>>> pt.duckdb.Relation("select 1 as column union select 2 as column").order(
|
|
1747
|
+
... by="1"
|
|
1748
|
+
... ).to_df()
|
|
1775
1749
|
shape: (2, 1)
|
|
1776
1750
|
┌────────┐
|
|
1777
1751
|
│ column │
|
|
@@ -1779,7 +1753,6 @@ class Relation(Generic[ModelType]):
|
|
|
1779
1753
|
│ i64 │
|
|
1780
1754
|
╞════════╡
|
|
1781
1755
|
│ 1 │
|
|
1782
|
-
├╌╌╌╌╌╌╌╌┤
|
|
1783
1756
|
│ 2 │
|
|
1784
1757
|
└────────┘
|
|
1785
1758
|
"""
|
|
@@ -1791,10 +1764,10 @@ class Relation(Generic[ModelType]):
|
|
|
1791
1764
|
# because polars is much more eager to store integer Series as 64-bit
|
|
1792
1765
|
# integers. Otherwise there must be done a lot of manual casting whenever
|
|
1793
1766
|
# you cross the boundary between DuckDB and polars.
|
|
1794
|
-
return DataFrame._from_arrow(arrow_table).
|
|
1767
|
+
return DataFrame._from_arrow(arrow_table).with_columns(
|
|
1795
1768
|
pl.col(pl.Int32).cast(pl.Int64)
|
|
1796
1769
|
)
|
|
1797
|
-
except pa.ArrowInvalid: # pragma: no cover
|
|
1770
|
+
except (pa.ArrowInvalid, pl.ArrowError): # pragma: no cover
|
|
1798
1771
|
# Empty relations with enum columns can sometimes produce errors.
|
|
1799
1772
|
# As a last-ditch effort, we convert such columns to VARCHAR.
|
|
1800
1773
|
casted_columns = [
|
|
@@ -1805,7 +1778,7 @@ class Relation(Generic[ModelType]):
|
|
|
1805
1778
|
]
|
|
1806
1779
|
non_enum_relation = self._relation.project(", ".join(casted_columns))
|
|
1807
1780
|
arrow_table = non_enum_relation.to_arrow_table()
|
|
1808
|
-
return DataFrame._from_arrow(arrow_table).
|
|
1781
|
+
return DataFrame._from_arrow(arrow_table).with_columns(
|
|
1809
1782
|
pl.col(pl.Int32).cast(pl.Int64)
|
|
1810
1783
|
)
|
|
1811
1784
|
|
|
@@ -1820,8 +1793,8 @@ class Relation(Generic[ModelType]):
|
|
|
1820
1793
|
|
|
1821
1794
|
Example:
|
|
1822
1795
|
>>> import patito as pt
|
|
1823
|
-
>>> relation = pt.Relation("select 1 as a union select 2 as a")
|
|
1824
|
-
>>> relation.to_series()
|
|
1796
|
+
>>> relation = pt.duckdb.Relation("select 1 as a union select 2 as a")
|
|
1797
|
+
>>> relation.order(by="a").to_series()
|
|
1825
1798
|
shape: (2,)
|
|
1826
1799
|
Series: 'a' [i32]
|
|
1827
1800
|
[
|
|
@@ -1850,8 +1823,9 @@ class Relation(Generic[ModelType]):
|
|
|
1850
1823
|
Duplicates are `not` dropped.
|
|
1851
1824
|
|
|
1852
1825
|
Args:
|
|
1853
|
-
other: A ``patito.Relation`` object or something that can be
|
|
1854
|
-
``patito.Relation``.
|
|
1826
|
+
other: A ``patito.duckdb.Relation`` object or something that can be
|
|
1827
|
+
*casted* to ``patito.duckdb.Relation``.
|
|
1828
|
+
See :ref:`Relation<duckdb.Relation.__init__>`.
|
|
1855
1829
|
|
|
1856
1830
|
Returns:
|
|
1857
1831
|
New relation containing the rows of both ``self`` and ``other``.
|
|
@@ -1861,8 +1835,8 @@ class Relation(Generic[ModelType]):
|
|
|
1861
1835
|
|
|
1862
1836
|
Examples:
|
|
1863
1837
|
>>> import patito as pt
|
|
1864
|
-
>>> relation_1 = pt.Relation("select 1 as a")
|
|
1865
|
-
>>> relation_2 = pt.Relation("select 2 as a")
|
|
1838
|
+
>>> relation_1 = pt.duckdb.Relation("select 1 as a")
|
|
1839
|
+
>>> relation_2 = pt.duckdb.Relation("select 2 as a")
|
|
1866
1840
|
>>> relation_1.union(relation_2).to_df()
|
|
1867
1841
|
shape: (2, 1)
|
|
1868
1842
|
┌─────┐
|
|
@@ -1871,7 +1845,6 @@ class Relation(Generic[ModelType]):
|
|
|
1871
1845
|
│ i64 │
|
|
1872
1846
|
╞═════╡
|
|
1873
1847
|
│ 1 │
|
|
1874
|
-
├╌╌╌╌╌┤
|
|
1875
1848
|
│ 2 │
|
|
1876
1849
|
└─────┘
|
|
1877
1850
|
|
|
@@ -1883,7 +1856,6 @@ class Relation(Generic[ModelType]):
|
|
|
1883
1856
|
│ i64 │
|
|
1884
1857
|
╞═════╡
|
|
1885
1858
|
│ 1 │
|
|
1886
|
-
├╌╌╌╌╌┤
|
|
1887
1859
|
│ 2 │
|
|
1888
1860
|
└─────┘
|
|
1889
1861
|
"""
|
|
@@ -1924,7 +1896,7 @@ class Relation(Generic[ModelType]):
|
|
|
1924
1896
|
|
|
1925
1897
|
Examples:
|
|
1926
1898
|
>>> import patito as pt
|
|
1927
|
-
>>> db = pt.Database()
|
|
1899
|
+
>>> db = pt.duckdb.Database()
|
|
1928
1900
|
>>> relation = db.to_relation("select 1 as a, 2 as b")
|
|
1929
1901
|
>>> relation.with_columns(c="a + b").to_df()
|
|
1930
1902
|
shape: (1, 3)
|
|
@@ -1946,8 +1918,8 @@ class Relation(Generic[ModelType]):
|
|
|
1946
1918
|
"""
|
|
1947
1919
|
Add missing defaultable columns filled with the default values of correct type.
|
|
1948
1920
|
|
|
1949
|
-
Make sure to invoke :ref:`Relation.set_model()<Relation.set_model>` with
|
|
1950
|
-
correct model schema before executing
|
|
1921
|
+
Make sure to invoke :ref:`Relation.set_model()<duckdb.Relation.set_model>` with
|
|
1922
|
+
the correct model schema before executing
|
|
1951
1923
|
``Relation.with_missing_default_columns()``.
|
|
1952
1924
|
|
|
1953
1925
|
Args:
|
|
@@ -1968,7 +1940,7 @@ class Relation(Generic[ModelType]):
|
|
|
1968
1940
|
... default_column: int = 42
|
|
1969
1941
|
... another_default_column: int = 42
|
|
1970
1942
|
...
|
|
1971
|
-
>>> relation = pt.Relation(
|
|
1943
|
+
>>> relation = pt.duckdb.Relation(
|
|
1972
1944
|
... "select 1 as non_default_column, 2 as default_column"
|
|
1973
1945
|
... )
|
|
1974
1946
|
>>> relation.to_df()
|
|
@@ -2036,8 +2008,8 @@ class Relation(Generic[ModelType]):
|
|
|
2036
2008
|
"""
|
|
2037
2009
|
Add missing nullable columns filled with correctly typed nulls.
|
|
2038
2010
|
|
|
2039
|
-
Make sure to invoke :ref:`Relation.set_model()<Relation.set_model>` with
|
|
2040
|
-
correct model schema before executing
|
|
2011
|
+
Make sure to invoke :ref:`Relation.set_model()<duckdb.Relation.set_model>` with
|
|
2012
|
+
the correct model schema before executing
|
|
2041
2013
|
``Relation.with_missing_nullable_columns()``.
|
|
2042
2014
|
|
|
2043
2015
|
Args:
|
|
@@ -2058,7 +2030,7 @@ class Relation(Generic[ModelType]):
|
|
|
2058
2030
|
... nullable_column: Optional[int]
|
|
2059
2031
|
... another_nullable_column: Optional[int]
|
|
2060
2032
|
...
|
|
2061
|
-
>>> relation = pt.Relation("select 1 as nullable_column")
|
|
2033
|
+
>>> relation = pt.duckdb.Relation("select 1 as nullable_column")
|
|
2062
2034
|
>>> relation.to_df()
|
|
2063
2035
|
shape: (1, 1)
|
|
2064
2036
|
┌─────────────────┐
|
|
@@ -2118,7 +2090,7 @@ class Relation(Generic[ModelType]):
|
|
|
2118
2090
|
"""
|
|
2119
2091
|
Execute ``self.union(other)``.
|
|
2120
2092
|
|
|
2121
|
-
See :ref:`Relation.union()<Relation.union>` for full documentation.
|
|
2093
|
+
See :ref:`Relation.union()<duckdb.Relation.union>` for full documentation.
|
|
2122
2094
|
"""
|
|
2123
2095
|
return self.union(other)
|
|
2124
2096
|
|
|
@@ -2135,7 +2107,7 @@ class Relation(Generic[ModelType]):
|
|
|
2135
2107
|
"""
|
|
2136
2108
|
Return Relation with selected columns.
|
|
2137
2109
|
|
|
2138
|
-
Uses :ref:`Relation.select()<Relation.select>` under-the-hood in order to
|
|
2110
|
+
Uses :ref:`Relation.select()<duckdb.Relation.select>` under-the-hood in order to
|
|
2139
2111
|
perform the selection. Can technically be used to rename columns,
|
|
2140
2112
|
define derived columns, and so on, but prefer the use of Relation.select() for
|
|
2141
2113
|
such use cases.
|
|
@@ -2149,7 +2121,7 @@ class Relation(Generic[ModelType]):
|
|
|
2149
2121
|
|
|
2150
2122
|
Example:
|
|
2151
2123
|
>>> import patito as pt
|
|
2152
|
-
>>> relation = pt.Relation("select 1 as a, 2 as b, 3 as c")
|
|
2124
|
+
>>> relation = pt.duckdb.Relation("select 1 as a, 2 as b, 3 as c")
|
|
2153
2125
|
>>> relation.to_df()
|
|
2154
2126
|
shape: (1, 3)
|
|
2155
2127
|
┌─────┬─────┬─────┐
|
|
@@ -2188,9 +2160,9 @@ class Relation(Generic[ModelType]):
|
|
|
2188
2160
|
"""
|
|
2189
2161
|
Iterate over rows in relation.
|
|
2190
2162
|
|
|
2191
|
-
If :ref:`Relation.set_model()<Relation.set_model>` has been invoked
|
|
2192
|
-
given model will be used to deserialize each row. Otherwise a Patito
|
|
2193
|
-
is dynamically constructed which fits the schema of the relation.
|
|
2163
|
+
If :ref:`Relation.set_model()<duckdb.Relation.set_model>` has been invoked
|
|
2164
|
+
first, the given model will be used to deserialize each row. Otherwise a Patito
|
|
2165
|
+
model is dynamically constructed which fits the schema of the relation.
|
|
2194
2166
|
|
|
2195
2167
|
Returns:
|
|
2196
2168
|
Iterator[Model]: An iterator of patito Model objects representing each row.
|
|
@@ -2199,7 +2171,7 @@ class Relation(Generic[ModelType]):
|
|
|
2199
2171
|
>>> from typing import Literal
|
|
2200
2172
|
>>> import patito as pt
|
|
2201
2173
|
>>> df = pt.DataFrame({"float_column": [1, 2], "enum_column": ["A", "B"]})
|
|
2202
|
-
>>> relation = pt.Relation(df).set_alias("my_relation")
|
|
2174
|
+
>>> relation = pt.duckdb.Relation(df).set_alias("my_relation")
|
|
2203
2175
|
>>> for row in relation:
|
|
2204
2176
|
... print(row)
|
|
2205
2177
|
...
|
|
@@ -2235,7 +2207,7 @@ class Relation(Generic[ModelType]):
|
|
|
2235
2207
|
"""
|
|
2236
2208
|
Return the number of rows in the relation.
|
|
2237
2209
|
|
|
2238
|
-
See :ref:`Relation.count()<Relation.count>` for full documentation.
|
|
2210
|
+
See :ref:`Relation.count()<duckdb.Relation.count>` for full documentation.
|
|
2239
2211
|
"""
|
|
2240
2212
|
return self.count()
|
|
2241
2213
|
|
|
@@ -2247,7 +2219,7 @@ class Relation(Generic[ModelType]):
|
|
|
2247
2219
|
|
|
2248
2220
|
Example:
|
|
2249
2221
|
>>> import patito as pt
|
|
2250
|
-
>>> products = pt.Relation(
|
|
2222
|
+
>>> products = pt.duckdb.Relation(
|
|
2251
2223
|
... pt.DataFrame(
|
|
2252
2224
|
... {
|
|
2253
2225
|
... "product_name": ["apple", "red_apple", "banana", "oranges"],
|
|
@@ -2278,7 +2250,7 @@ class Relation(Generic[ModelType]):
|
|
|
2278
2250
|
banana 1
|
|
2279
2251
|
oranges 3
|
|
2280
2252
|
|
|
2281
|
-
>>> suppliers = pt.Relation(
|
|
2253
|
+
>>> suppliers = pt.duckdb.Relation(
|
|
2282
2254
|
... pt.DataFrame(
|
|
2283
2255
|
... {
|
|
2284
2256
|
... "id": [1, 2],
|
|
@@ -2366,7 +2338,7 @@ class Database:
|
|
|
2366
2338
|
|
|
2367
2339
|
Examples:
|
|
2368
2340
|
>>> import patito as pt
|
|
2369
|
-
>>> db = pt.Database()
|
|
2341
|
+
>>> db = pt.duckdb.Database()
|
|
2370
2342
|
>>> db.to_relation("select 1 as a, 2 as b").create_table("my_table")
|
|
2371
2343
|
>>> db.query("select * from my_table").to_df()
|
|
2372
2344
|
shape: (1, 2)
|
|
@@ -2394,12 +2366,12 @@ class Database:
|
|
|
2394
2366
|
Return the default DuckDB database.
|
|
2395
2367
|
|
|
2396
2368
|
Returns:
|
|
2397
|
-
A patito :ref:`Database<Database>` object wrapping around the given
|
|
2369
|
+
A patito :ref:`Database<duckdb.Database>` object wrapping around the given
|
|
2398
2370
|
connection.
|
|
2399
2371
|
|
|
2400
2372
|
Example:
|
|
2401
2373
|
>>> import patito as pt
|
|
2402
|
-
>>> db = pt.Database.default()
|
|
2374
|
+
>>> db = pt.duckdb.Database.default()
|
|
2403
2375
|
>>> db.query("select 1 as a, 2 as b").to_df()
|
|
2404
2376
|
shape: (1, 2)
|
|
2405
2377
|
┌─────┬─────┐
|
|
@@ -2424,13 +2396,14 @@ class Database:
|
|
|
2424
2396
|
``duckdb.connect()``.
|
|
2425
2397
|
|
|
2426
2398
|
Returns:
|
|
2427
|
-
A :ref:`Database<Database>` object wrapping around the given
|
|
2399
|
+
A :ref:`Database<duckdb.Database>` object wrapping around the given
|
|
2400
|
+
connection.
|
|
2428
2401
|
|
|
2429
2402
|
Example:
|
|
2430
2403
|
>>> import duckdb
|
|
2431
2404
|
>>> import patito as pt
|
|
2432
2405
|
>>> connection = duckdb.connect()
|
|
2433
|
-
>>> database = pt.Database.from_connection(connection)
|
|
2406
|
+
>>> database = pt.duckdb.Database.from_connection(connection)
|
|
2434
2407
|
"""
|
|
2435
2408
|
obj = cls.__new__(cls)
|
|
2436
2409
|
obj.connection = connection
|
|
@@ -2455,7 +2428,7 @@ class Database:
|
|
|
2455
2428
|
|
|
2456
2429
|
Example:
|
|
2457
2430
|
>>> import patito as pt
|
|
2458
|
-
>>> db = pt.Database()
|
|
2431
|
+
>>> db = pt.duckdb.Database()
|
|
2459
2432
|
>>> db.to_relation("select 1 as a, 2 as b").to_df()
|
|
2460
2433
|
shape: (1, 2)
|
|
2461
2434
|
┌─────┬─────┐
|
|
@@ -2473,7 +2446,6 @@ class Database:
|
|
|
2473
2446
|
│ i64 ┆ str │
|
|
2474
2447
|
╞═════╪═════╡
|
|
2475
2448
|
│ 3 ┆ 5 │
|
|
2476
|
-
├╌╌╌╌╌┼╌╌╌╌╌┤
|
|
2477
2449
|
│ 4 ┆ 6 │
|
|
2478
2450
|
└─────┴─────┘
|
|
2479
2451
|
"""
|
|
@@ -2499,7 +2471,7 @@ class Database:
|
|
|
2499
2471
|
|
|
2500
2472
|
Example:
|
|
2501
2473
|
>>> import patito as pt
|
|
2502
|
-
>>> db = pt.Database()
|
|
2474
|
+
>>> db = pt.duckdb.Database()
|
|
2503
2475
|
>>> db.execute("create table my_table (x bigint);")
|
|
2504
2476
|
>>> db.execute("insert into my_table values (1), (2), (3)")
|
|
2505
2477
|
>>> db.table("my_table").to_df()
|
|
@@ -2510,9 +2482,7 @@ class Database:
|
|
|
2510
2482
|
│ i64 │
|
|
2511
2483
|
╞═════╡
|
|
2512
2484
|
│ 1 │
|
|
2513
|
-
├╌╌╌╌╌┤
|
|
2514
2485
|
│ 2 │
|
|
2515
|
-
├╌╌╌╌╌┤
|
|
2516
2486
|
│ 3 │
|
|
2517
2487
|
└─────┘
|
|
2518
2488
|
|
|
@@ -2527,7 +2497,6 @@ class Database:
|
|
|
2527
2497
|
│ i64 │
|
|
2528
2498
|
╞═════╡
|
|
2529
2499
|
│ 1 │
|
|
2530
|
-
├╌╌╌╌╌┤
|
|
2531
2500
|
│ 3 │
|
|
2532
2501
|
└─────┘
|
|
2533
2502
|
|
|
@@ -2582,7 +2551,7 @@ class Database:
|
|
|
2582
2551
|
|
|
2583
2552
|
Example:
|
|
2584
2553
|
>>> import patito as pt
|
|
2585
|
-
>>> db = pt.Database()
|
|
2554
|
+
>>> db = pt.duckdb.Database()
|
|
2586
2555
|
>>> relation = db.query("select 1 as a, 2 as b, 3 as c")
|
|
2587
2556
|
>>> relation.to_df()
|
|
2588
2557
|
shape: (1, 3)
|
|
@@ -2624,7 +2593,7 @@ class Database:
|
|
|
2624
2593
|
... string_column: str
|
|
2625
2594
|
... bool_column: bool
|
|
2626
2595
|
...
|
|
2627
|
-
>>> db = pt.Database()
|
|
2596
|
+
>>> db = pt.duckdb.Database()
|
|
2628
2597
|
>>> empty_relation = db.empty_relation(Schema)
|
|
2629
2598
|
>>> empty_relation.to_df()
|
|
2630
2599
|
shape: (0, 2)
|
|
@@ -2659,7 +2628,7 @@ class Database:
|
|
|
2659
2628
|
Example:
|
|
2660
2629
|
>>> import patito as pt
|
|
2661
2630
|
>>> df = pt.DataFrame({"a": [1, 2], "b": [3, 4]})
|
|
2662
|
-
>>> db = pt.Database()
|
|
2631
|
+
>>> db = pt.duckdb.Database()
|
|
2663
2632
|
>>> relation = db.to_relation(df)
|
|
2664
2633
|
>>> relation.create_table(name="my_table")
|
|
2665
2634
|
>>> db.table("my_table").to_df()
|
|
@@ -2670,7 +2639,6 @@ class Database:
|
|
|
2670
2639
|
│ i64 ┆ i64 │
|
|
2671
2640
|
╞═════╪═════╡
|
|
2672
2641
|
│ 1 ┆ 3 │
|
|
2673
|
-
├╌╌╌╌╌┼╌╌╌╌╌┤
|
|
2674
2642
|
│ 2 ┆ 4 │
|
|
2675
2643
|
└─────┴─────┘
|
|
2676
2644
|
"""
|
|
@@ -2689,7 +2657,7 @@ class Database:
|
|
|
2689
2657
|
Example:
|
|
2690
2658
|
>>> import patito as pt
|
|
2691
2659
|
>>> df = pt.DataFrame({"a": [1, 2], "b": [3, 4]})
|
|
2692
|
-
>>> db = pt.Database()
|
|
2660
|
+
>>> db = pt.duckdb.Database()
|
|
2693
2661
|
>>> relation = db.to_relation(df)
|
|
2694
2662
|
>>> relation.create_view(name="my_view")
|
|
2695
2663
|
>>> db.view("my_view").to_df()
|
|
@@ -2700,7 +2668,6 @@ class Database:
|
|
|
2700
2668
|
│ i64 ┆ i64 │
|
|
2701
2669
|
╞═════╪═════╡
|
|
2702
2670
|
│ 1 ┆ 3 │
|
|
2703
|
-
├╌╌╌╌╌┼╌╌╌╌╌┤
|
|
2704
2671
|
│ 2 ┆ 4 │
|
|
2705
2672
|
└─────┴─────┘
|
|
2706
2673
|
"""
|
|
@@ -2717,11 +2684,11 @@ class Database:
|
|
|
2717
2684
|
"""
|
|
2718
2685
|
Create table with schema matching the provided Patito model.
|
|
2719
2686
|
|
|
2720
|
-
See :ref:`Relation.insert_into()<Relation.insert_into>` for how to insert
|
|
2721
|
-
into the table after creation.
|
|
2722
|
-
The :ref:`Relation.create_table()<Relation.create_table>` method can also
|
|
2723
|
-
used to create a table from a given relation `and` insert the data at the
|
|
2724
|
-
time.
|
|
2687
|
+
See :ref:`Relation.insert_into()<duckdb.Relation.insert_into>` for how to insert
|
|
2688
|
+
data into the table after creation.
|
|
2689
|
+
The :ref:`Relation.create_table()<duckdb.Relation.create_table>` method can also
|
|
2690
|
+
be used to create a table from a given relation `and` insert the data at the
|
|
2691
|
+
same time.
|
|
2725
2692
|
|
|
2726
2693
|
Args:
|
|
2727
2694
|
name: Name of new database table.
|
|
@@ -2737,10 +2704,10 @@ class Database:
|
|
|
2737
2704
|
... str_column: str
|
|
2738
2705
|
... nullable_string_column: Optional[str]
|
|
2739
2706
|
...
|
|
2740
|
-
>>> db = pt.Database()
|
|
2707
|
+
>>> db = pt.duckdb.Database()
|
|
2741
2708
|
>>> db.create_table(name="my_table", model=MyModel)
|
|
2742
2709
|
>>> db.table("my_table").types
|
|
2743
|
-
{'str_column':
|
|
2710
|
+
{'str_column': VARCHAR, 'nullable_string_column': VARCHAR}
|
|
2744
2711
|
"""
|
|
2745
2712
|
self.create_enum_types(model=model)
|
|
2746
2713
|
schema = model.schema()
|
|
@@ -2768,7 +2735,7 @@ class Database:
|
|
|
2768
2735
|
>>> class EnumModel(pt.Model):
|
|
2769
2736
|
... enum_column: Literal["A", "B", "C"]
|
|
2770
2737
|
...
|
|
2771
|
-
>>> db = pt.Database()
|
|
2738
|
+
>>> db = pt.duckdb.Database()
|
|
2772
2739
|
>>> db.create_enum_types(EnumModel)
|
|
2773
2740
|
>>> db.enum_types
|
|
2774
2741
|
{'enum__7ba49365cc1b0fd57e61088b3bc9aa25'}
|
|
@@ -2812,7 +2779,7 @@ class Database:
|
|
|
2812
2779
|
|
|
2813
2780
|
Examples:
|
|
2814
2781
|
>>> import patito as pt
|
|
2815
|
-
>>> db = pt.Database()
|
|
2782
|
+
>>> db = pt.duckdb.Database()
|
|
2816
2783
|
>>> "my_table" in db
|
|
2817
2784
|
False
|
|
2818
2785
|
>>> db.to_relation("select 1 as a, 2 as b").create_table(name="my_table")
|