Python API#

Not comfortable coding in Python?

Then prefer using the interactive session!

The Python API is named seastersdb. In a Python script, you can import it with, e.g., the following snippet:

import seastersdb as sdb

The package actually provides one single function named connect() which returns a DuckDB connection to the SEASTERS database (calling the function creates the connection and defines the various macros and functions listed here). The API thus simply consists of using the connection and its methods.

Retrieve the connection with the following command:

con = sdb.connect()

Note

Since connect() is the only function of seastersdb, an alternative workflow could be written this way:

from seastersdb import connect

con = connect()

From here, executing an SQL query can be done with the execute() method:

# Example query
query = """
SELECT *
FROM ghcnd_stations()
"""

con.execute(query)

Then, calling the df() method right after executing a query allows actually extracting the query’s result as a pandas.DataFrame() object:

df = con.df()

Afterwards, pandas is quite a popular tool in our field and there are many resources on the internet to deal with it. Also, a pandas.DataFrame() can be converted into objects from other popular Python packages like xarray, and be exported to files in various formats, such as in csv. Nevertheless, if your goal is to export your query’s result in a file, using the interactive session should be a more adequate solution.