Python API
Kùzu provides a Python package that you can install via PyPI. A full list of the available functions and classes can be found in the Python API documentation, linked below.
Some useful features of the Python API are explained in the following sections.
DataFrames and Arrow Tables
In Python, Kùzu supports the use of Pandas and Polars DataFrames, as well as PyArrow Tables. This allows you to leverage the data manipulation capabilities of these libraries in your graph workflows.
Output query results
You can output the results of a Cypher query to a Pandas DataFrame, Polars DataFrame, or PyArrow Table. The following examples show how to output query results to each of these data structures.
You can output the results of a Cypher query to a Pandas DataFrame using the get_as_df()
method:
You can return all the columns of a node table by using the *
wildcard in the RETURN
clause.
Return specific columns by name and optionally, alias them, as follows:
This will return only the name
column.
You can output the results of a Cypher query to a Polars DataFrame using the get_as_pl()
method:
Using the get_as_pl()
method on your query result returns the result as a Polars DataFrame.
Return specific columns by name and optionally, alias them, as follows:
This will return only the name
column.
You can output the results of a Cypher query to a PyArrow Table using the get_as_arrow()
method:
Using the get_as_arrow()
method on your query result returns the result as a PyArrow Table.
LOAD FROM
You can scan a Pandas DataFrame, Polars DataFrame, or PyArrow Table in Kùzu using the LOAD FROM
clause.
Scanning a DataFrame or Table does not copy the data into Kùzu, it only reads the data.
Using the get_as_df()
method on your query result returns the result as a Pandas DataFrame.
Using the get_as_pl()
method on your query result returns the result as a Polars DataFrame.
Using the get_as_arrow()
method on your query result returns the result as a PyArrow Table.
COPY FROM
Copy from a Pandas DataFrame into a Kùzu table using the COPY FROM
command:
Using the get_as_df()
method on your query result returns the result as a Pandas DataFrame.
Copy from a Polars DataFrame into a Kùzu table using the COPY FROM
command:
Using the get_as_pl()
method on your query result returns the result as a Polars DataFrame.
Copy from a PyArrow Table into a Kùzu table using the COPY FROM
command:
Using the get_as_arrow()
method on your query result returns the result as a PyArrow Table.
Type notation
This section summarizes the type notation used in Kùzu’s Python API. Below is a table from Python
types to a Kùzu LogicalTypeID
, which will be used to infer types via Python type annotations.
Python type | Kùzu LogicalTypeID |
---|---|
bool | BOOL |
int | INT64 |
float | DOUBLE |
str | STRING |
datetime | TIMESTAMP |
date | DATE |
timedelta | INTERVAL |
uuid | UUID |
list | LIST |
dict | MAP |
UDF
Kùzu’s Python API also supports the registration of User Defined Functions (UDFs), allowing you to define custom functions in Python and use them in your Cypher queries. This can allow you to quickly extend Kùzu with new functions you need in your Python applications.
An example of using the UDF API is shown below. We will define a Python UDF that calculates the difference between two numbers, and then apply it in a Cypher query.
Register the UDF
Note that in the example above, we explicitly declared the expected types of the parameters and the return value in Kùzu, prior to registering the UDF.
Alternatively, you can simply use Python type annotations to denote the type signature of the parameters and return value.
Additional parameters
The UDF API’s create_function
provides the following additional parameters:
name: str
: The name of the function to be invoked in cypher.udf: Callable[[...], Any]
: The function to be executed.params_type: Optional[list[Type | str]]
: A list whose elements can either bekuzu.Type
orstr
.kuzu.Type
can be used to denote nonnested parameter types, whilestr
can be used to denote both nested and nonnested parameter types. Details on how to denote types are in the type notation section.return_type: Optional[Type | str]
: Either akuzu.Type
enum orstr
. Details on how to denote types are in the type notation section.default_null_handling: Optional[bool]
: True by default. When true, if any one of the inputs is null, function execution is skipped and the output is resolved to null.catch_exceptions: Optional[bool]
: False by default. When true, if the UDF raises an exception, the output is resolved to null. Otherwise the Exception is rethrown.
Apply the UDF
Once the UDF is registered, you can apply it in a Cypher query. First, let’s create some data.
We’re now ready to apply the UDF in a Cypher query:
The output should be:
Remove UDF
In case you want to remove the UDF, you can call the remove_function
method on the connection object.
Nested and complex types
When working with UDFs, you can also specify nested or complex types, though in this case, there are some differences from the examples shown above. With these additional types, a string representation should be given for the parameters which are then manually casted to the respective Kùzu type.
Some examples of where this is relevant are listed below:
- A list of
INT64
would be"INT64[]"
- A map from a
STRING
to aDOUBLE
would be"MAP(STRING, DOUBLE)"
- A Decimal value with 7 significant figures and 2 decimals would be
"DECIMAL(7, 2)"
Note that it’s also valid to define child types through Python’s type annotations, e.g. list[int]
,
or dict(str, float)
for simple types.
Below, we show an example to calculate the discounted price of an item using a Python UDF.
The second parameter is a built-in native type in Kùzu, i.e., kuzu.Type.BOOL
. For the first parameter,
we need to specify a string, i.e. "DECIMAL(7,2)"
that’s then parsed and used by the binder in Kùzu
to map to the internal Decimal representation.