Functions#

DataFusion provides a large number of built-in functions for performing complex queries without requiring user-defined functions. In here we will cover some of the more popular use cases. If you want to view all the functions go to the Functions API Reference.

Note

For Apache Spark-compatible versions of these functions (with Spark NULL-propagation, 1-indexed substrings, HALF_UP rounding, etc.), see Spark-Compatible Functions.

We’ll use the pokemon dataset in the following examples.

from datafusion import SessionContext

ctx = SessionContext()
ctx.register_csv("pokemon", "pokemon.csv")
df = ctx.table("pokemon")

Mathematical#

DataFusion offers mathematical functions such as pow() or log()

from datafusion import col, literal
from datafusion import functions as f

df.select(
    f.pow(col('"Attack"'), literal(2)) - f.pow(col('"Defense"'), literal(2))
).limit(10)
DataFrame()
+------------------------------------------------------------------+
| power(pokemon.Attack,Int64(2)) - power(pokemon.Defense,Int64(2)) |
+------------------------------------------------------------------+
| 0.0                                                              |
| -125.0                                                           |
| -165.0                                                           |
| -5129.0                                                          |
| 855.0                                                            |
| 732.0                                                            |
| 972.0                                                            |
| 4579.0                                                           |
| 4732.0                                                           |
| -1921.0                                                          |
+------------------------------------------------------------------+

Conditional#

There 3 conditional functions in DataFusion coalesce(), nullif() and case().

df.select(
    f.coalesce(col('"Type 1"'), col('"Type 2"')).alias("dominant_type")
).limit(10)
DataFrame()
+---------------+
| dominant_type |
+---------------+
| Grass         |
| Grass         |
| Grass         |
| Grass         |
| Fire          |
| Fire          |
| Fire          |
| Fire          |
| Fire          |
| Water         |
+---------------+

Temporal#

For selecting the current time use now()

df.select(f.now())
DataFrame()
+-------------------------------+
| now()                         |
+-------------------------------+
| 2026-10-12T19:09:11.091449105 |
| 2026-10-12T19:09:11.091449105 |
| 2026-10-12T19:09:11.091449105 |
| 2026-10-12T19:09:11.091449105 |
| 2026-10-12T19:09:11.091449105 |
| 2026-10-12T19:09:11.091449105 |
| 2026-10-12T19:09:11.091449105 |
| 2026-10-12T19:09:11.091449105 |
| 2026-10-12T19:09:11.091449105 |
| 2026-10-12T19:09:11.091449105 |
+-------------------------------+
Data truncated.

Convert to timestamps using to_timestamp()

df.select(f.to_timestamp(col('"Total"')).alias("timestamp"))
DataFrame()
+---------------------+
| timestamp           |
+---------------------+
| 1970-01-01T00:05:18 |
| 1970-01-01T00:06:45 |
| 1970-01-01T00:08:45 |
| 1970-01-01T00:10:25 |
| 1970-01-01T00:05:09 |
| 1970-01-01T00:06:45 |
| 1970-01-01T00:08:54 |
| 1970-01-01T00:10:34 |
| 1970-01-01T00:10:34 |
| 1970-01-01T00:05:14 |
+---------------------+
Data truncated.

Extracting parts of a date using date_part() (alias extract())

df.select(
   f.date_part("month", f.to_timestamp(col('"Total"'))).alias("month"),
   f.extract("day", f.to_timestamp(col('"Total"'))).alias("day")
)
DataFrame()
+-------+-----+
| month | day |
+-------+-----+
| 1     | 1   |
| 1     | 1   |
| 1     | 1   |
| 1     | 1   |
| 1     | 1   |
| 1     | 1   |
| 1     | 1   |
| 1     | 1   |
| 1     | 1   |
| 1     | 1   |
+-------+-----+
Data truncated.

String#

In the field of data science, working with textual data is a common task. To make string manipulation easier, DataFusion offers a range of helpful options.

df.select(
    f.char_length(col('"Name"')).alias("len"),
    f.lower(col('"Name"')).alias("lower"),
    f.left(col('"Name"'), literal(4)).alias("code")
)
DataFrame()
+-----+---------------------------+------+
| len | lower                     | code |
+-----+---------------------------+------+
| 9   | bulbasaur                 | Bulb |
| 7   | ivysaur                   | Ivys |
| 8   | venusaur                  | Venu |
| 21  | venusaurmega venusaur     | Venu |
| 10  | charmander                | Char |
| 10  | charmeleon                | Char |
| 9   | charizard                 | Char |
| 25  | charizardmega charizard x | Char |
| 25  | charizardmega charizard y | Char |
| 8   | squirtle                  | Squi |
+-----+---------------------------+------+
Data truncated.

This also includes the functions for regular expressions like regexp_replace() and regexp_match()

df.select(
    f.regexp_match(col('"Name"'), literal("Char")).alias("dragons"),
    f.regexp_replace(col('"Name"'), literal("saur"), literal("fleur")).alias("flowers")
)
DataFrame()
+---------+---------------------------+
| dragons | flowers                   |
+---------+---------------------------+
|         | Bulbafleur                |
|         | Ivyfleur                  |
|         | Venufleur                 |
|         | VenufleurMega Venusaur    |
| [Char]  | Charmander                |
| [Char]  | Charmeleon                |
| [Char]  | Charizard                 |
| [Char]  | CharizardMega Charizard X |
| [Char]  | CharizardMega Charizard Y |
|         | Squirtle                  |
+---------+---------------------------+
Data truncated.

Casting#

Casting expressions to different data types using arrow_cast()

df.select(
    f.arrow_cast(col('"Total"'), "Float64").alias("total_as_float"),
    f.arrow_cast(col('"Total"'), "Int32").alias("total_as_int")
)
DataFrame()
+----------------+--------------+
| total_as_float | total_as_int |
+----------------+--------------+
| 318.0          | 318          |
| 405.0          | 405          |
| 525.0          | 525          |
| 625.0          | 625          |
| 309.0          | 309          |
| 405.0          | 405          |
| 534.0          | 534          |
| 634.0          | 634          |
| 634.0          | 634          |
| 314.0          | 314          |
+----------------+--------------+
Data truncated.

Other#

The function in_list() allows to check a column for the presence of multiple values:

types = [literal("Grass"), literal("Fire"), literal("Water")]
(
    df.select(f.in_list(col('"Type 1"'), types, negated=False).alias("basic_types"))
      .limit(20)
      .to_pandas()
)
    basic_types
0          True
1          True
2          True
3          True
4          True
5          True
6          True
7          True
8          True
9          True
10         True
11         True
12         True
13        False
14        False
15        False
16        False
17        False
18        False
19        False

Handling Missing Values#

DataFusion provides methods to handle missing values in DataFrames:

fill_null#

The fill_null() method replaces NULL values in specified columns with a provided value:

# Fill all NULL values with 0 where possible
df = df.fill_null(0)

# Fill NULL values only in specific string columns
df = df.fill_null("missing", subset=["name", "category"])

The fill value will be cast to match each column’s type. If casting fails for a column, that column remains unchanged.

fill_nan#

The fill_nan() method replaces NaN values in floating-point columns. NaN is distinct from NULL, which fill_null() handles:

# Replace NaN with 0.0 in every floating-point column
df = df.fill_nan(0.0)

# Replace NaN only in specific columns
df = df.fill_nan(0.0, subset=["price"])

Column names with uppercase letters or dots#

fill_null() and fill_nan() fail on a DataFrame that has any column whose name contains an uppercase letter or a ., even when that column is not in subset (apache/datafusion#25829):

df = ctx.from_pydict({"Price": [float("nan"), 2.0], "qty": [float("nan"), 1.0]})
df.fill_nan(0.0, subset=["qty"])
# Schema error: No field named price. Did you mean '..."Price"'?

Until that is fixed, rename such columns first. Quote the old name so it is not normalized:

df.with_column_renamed('"Price"', "price").fill_nan(0.0, subset=["qty"])