duckdb

  1. Base:
    1. import duckdb
      1. duckdb.sql(« SELECT 42 »).show()
    2. r1 = duckdb.sql(« SELECT 42 AS i »)
      1. duckdb.sql(« SELECT i * 2 AS k FROM r1 »).show()
  2. Data Input
    1. import duckdb
    2. duckdb.read_csv(« example.csv ») # read a CSV file into a Relation
    3. duckdb.read_parquet(« example.parquet ») # read a Parquet file into a Relation
    4. duckdb.read_json(« example.json ») # read a JSON file into a Relation
    5. duckdb.sql(« SELECT * FROM ‘example.csv' ») # directly query a CSV file
    6. duckdb.sql(« SELECT * FROM ‘example.parquet' ») # directly query a Parquet file
    7. duckdb.sql(« SELECT * FROM ‘example.json' ») # directly query a JSON file
  3. Pandas
    1. pandas_df = pd.DataFrame({« a »: [42]})
    2. duckdb.sql(« SELECT * FROM pandas_df »)
  4. Polars
    1. polars_df = pl.DataFrame({« a »: [42]})
    2. duckdb.sql(« SELECT * FROM polars_df »)
  5. PyArrow
    1. arrow_table = pa.Table.from_pydict({« a »: [42]})
    2. duckdb.sql(« SELECT * FROM arrow_table »)
  6. Result Conversion
    1. duckdb.sql(« SELECT 42 »).fetchall() # Python objects
    2. duckdb.sql(« SELECT 42 »).df() # Pandas DataFrame
    3. duckdb.sql(« SELECT 42 »).pl() # Polars DataFrame
    4. duckdb.sql(« SELECT 42 »).arrow() # Arrow Table
    5. duckdb.sql(« SELECT 42 »).fetchnumpy() # NumPy Arrays
  7. writing data to disk
    1. duckdb.sql(« SELECT 42 »).write_parquet(« out.parquet ») # Write to a Parquet file
    2. duckdb.sql(« SELECT 42 »).write_csv(« out.csv ») # Write to a CSV file
    3. duckdb.sql(« COPY (SELECT 42) TO ‘out.parquet' ») # Copy to a Parquet file
  8. Using In Memory
    1. con = duckdb.connect()
    2. con.sql(« SELECT 42 AS x »).show()
  9. persistent Storage
    1. # create a connection to a file called ‘file.db’
    2. con = duckdb.connect(« file.db »)
    3. # create a table and load data into it
    4. con.sql(« CREATE TABLE test (i INTEGER) »)
    5. con.sql(« INSERT INTO test VALUES (42) »)
    6. # query the table
    7. con.table(« test »).show()
    8. # explicitly close the connection
    9. con.close()
    10. # Note: connections also closed implicitly when they go out of scope
  10. Configuration
    1. con = duckdb.connect(config = {‘threads’: 1})

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Panier