In Polars, the len()
function returns the number of elements contained in a Series. A Polars Series is a single column of data, a named list of values, much like a column in a Pandas DataFrame.
In this article, I will explain the syntax of the Polars Series len()
function, its parameters, and how it returns the number of elements in the Series as an integer.
Key Points –
- The
len()
function returns the number of elements (rows) contained in a Polars Series. - It is equivalent to the
len()
method and can be used interchangeably. - The return value is always an integer (
int
) representing the count of elements. len()
counts all elements, includingnull
(missing) values.- You can use Python’s built-in
len(series)
syntax or the Polars methodseries.len()
. Series.len()
is functionally equivalent tolen(series)
andseries.shape[0]
.
Polars Series len() Introduction
Let’s know the syntax of the series len() function.
# Syntax of len()
Series.len() → int
Parameters of the Polars Series len()
No arguments are needed, just empty parentheses ()
.
Return Value
This function returns an integer (int) representing the number of elements in the Series.
Usage of Polars Series len() Function
The len()
function retrieves the total number of elements (or rows) present in a Polars Series. It returns this count as an integer value.
Now, let’s create a Polars series using a list of values and use the len()
function.
import polars as pl
ser = pl.Series("numbers", [2, 4, 6, 8])
print("Original Series:\n", ser)
Yields below output.
The basic usage of the Polars Series len()
function involves calling it in the simplest way to retrieve the count of elements in a Series.
# Get the length
ser2 = ser.len()
print("Length of Series:", ser2)
Here,
ser.len()
returns the number of elements (4).- This is the simplest way to check how many values your Series contains.
Series with Null Values
The Series.len()
function counts every element in the Series, including any null values. It returns the total number of rows, not just those with valid (non-null) data. In Polars, a null represents missing or undefined information. In Python, this is written as None
, and it appears as null
within Polars.
import polars as pl
# Series with null values
ser = pl.Series("values", [2, None, 4, None, 6])
# Series with null values
ser2 = ser.len()
print("Length of Series:", ser2)
# Output:
# Length of Series: 5
Here,
- There are 5 elements in the Series.
- 2 of them are
null
, butlen()
still returns5
.
Series with Strings
A Series containing strings is essentially a column-like data structure that holds text values. Below is an example of a Polars Series with string entries and how to determine its length.
import polars as pl
# Create a Series of strings
ser = pl.Series("fruits", ["apple", "banana", "mango", "grape"])
# Get the length
print("Length of series:", ser.len())
# Output:
# Length of series: 4
Here,
- The
len()
function still counts all elements, regardless of whether the values are strings, numbers, or nulls.
Empty Series in Polars
An empty Series is one that contains no elements at all. Below is an example of an empty Polars Series and how the len()
function responds to it.
import polars as pl
# Create an empty Series
ser = pl.Series("empty", [])
print("Length of series:", ser.len())
# output:
# Length of series: 0
Here,
- When a Series has no elements,
len()
returns 0. - This is useful to check for empty data before processing.
Conclusion
In conclusion, the len()
function in Polars is a simple yet essential tool for quickly determining the number of elements in a Series, including any null values. Whether your Series contains numbers, strings, or is empty, len()
provides an accurate count to help you better understand and work with your data.
Happy Learning!!
Related Articles
- Polars Series count() Function with Examples
- Polars Series mode() – Explained by Examples
- Polars Series product() Method with Examples
- Polars Series var() Usage & Examples
- Polars Series median() Function with Examples
- Polars Series std() – Explained by Examples
- Polars Series slice() – Usage & Examples
- Polars series quantile() – Usage & Examples
- Polars Series mean() – Usage & Examples
- Polars Series min() – Explained by Examples
- Polars Series limit() – Explained by Examples
- Polars Series fill_null() Function with Examples