In polars, the Series.slice() function is used to extract a portion (or slice) of a Series, similar to slicing a list in Python. It returns a new Series that begins at a specified offset and contains a defined number of elements.
In this article, I will explain the slice() method of the Polars Series, covering its syntax, parameters, and how it works. This method returns a new Series that represents a subset of the original.
Key Points –
- The
slice()method allows you to extract a subset of values from a Series based on position. - It takes two parameters:
offset(start index) andlength(number of elements to return). - If
lengthis omitted or set toNone, the slice will extend fromoffsetto the end of the Series. - The
offsetmust be an integer and represents the starting index (0-based). - A negative
offsetcounts positions backward from the end of the Series. - If
length=None, the slice includes all elements from theoffsetto the end of the Series. - If the
offsetexceeds the Series length, the result is an empty Series. - Slicing does not modify the original Series; it returns a new sliced Series.
<a id="h-polars-series-slice-introduction"></a>
Polars Series slice() Introduction
Let’s know the syntax of the series slice() method.
# Syntax of Series slice()
Series.slice(offset: int, length: int | None = None) → Series
<a id="h-parameters-of-the-polars-series-slice"></a>
Parameters of the Polars series slice()
Following are the parameters of series slice() method.
offset(int) - Starting index of the slice. Can be negative to count from the end.length(intorNone, optional) - Number of elements to include in the slice. IfNone, slice continues to the end.
<a id="h-return-value"></a>
Return Value
This function returns a new Series containing the sliced elements.
<a id="h-usage-of-polars-series-slice-method"></a>
Usage of Polars Series slice() Method
The Series.slice() function in Polars returns a subset of values from the Series, starting at a given index and taking a specific number of elements.
First, let’s create a Polars Series.
import polars as pl
# Sample Series with nulls
ser = pl.Series("values", [10, 20, 30, 40, 50])
print("Original Series:\n", ser)
Yields below output.
To perform a basic slice on a Polars Series starting from index 2 and taking 3 elements, you can use the slice() function.
# Slice from index 2, length 3
ser2 = ser.slice(offset=2, length=3)
print("Sliced Series:\n", ser2)
# Slice from index 2, take 3 elements
ser2 = ser.slice(2, 3)
print("Sliced Series:\n", ser2)
Here,
offset=2starts at the third element (indexing starts at 0).length=3takes 3 consecutive elements from that point onward.- The resulting Series contains:
30, 40, 50.
<a id="h-slice-from-the-beginning"></a>
Slice From the Beginning
To slice a Polars Series from the start, set offset=0 in the slice() method and specify the number of elements you want to retrieve.
# Slice from beginning (index 0), take 3 elements
ser2 = ser.slice(offset=0, length=3)
print("Sliced Series:\n", ser2)
# Output:
# Sliced Series:
# shape: (3,)
# Series: 'values' [i64]
# [
# 10
# 20
# 30
# ]
Here,
slice(0, 3)gives you the first 3 values of the Series.- Great for previewing or limiting data during processing.
Alternatively, to slice from the beginning of a Polars Series and include all elements, simply set the offset to 0 and omit the length.
# Slice from beginning (index 0)
ser2 = ser.slice(0)
print("Sliced Series:\n", ser2)
# Output:
# Sliced Series:
# shape: (5,)
# Series: 'values' [i64]
# [
# 10
# 20
# 30
# 40
# 50
# ]
Here,
offset = 0: Start at the first element.length = None(default): Take all elements from that point to the end.
<a id="h-slice-to-the-end-length-none"></a>
Slice to the End (Length=None)
To slice to the end of a Polars Series, set the offset to your starting point and let length=None (which is the default behavior). This tells Polars to return all elements from that offset onward.
# Slice from index 2 to the end
ser2 = ser.slice(2)
print("Sliced Series:\n", ser2)
# Slice from index 2 to the end
ser2 = ser.slice(offset=2, length=None)
print("Sliced Series:\n", ser2)
# Output:
# Sliced Series:
# shape: (3,)
# Series: 'values' [i64]
# [
# 30
# 40
# 50
# ]
Here,
offset = 2: Start from the 3rd element (30).length = None: Take all remaining elements.
<a id="h-use-negative-offset"></a>
Use Negative Offset
Using a negative offset with slice() instructs Polars to begin slicing from the end of the Series, counting positions backward.
# Slice from 3rd last element to the end
ser2 = ser.slice(offset=-3, length=None)
print("Sliced Series:\n", ser2)
# Slice last 2 elements
ser2 = ser.slice(-2)
print("Sliced Series:\n", ser2)
# Output:
# Sliced Series:
# shape: (3,)
# Series: 'values' [i64]
# [
# 30
# 40
# 50
# ]
Here,
offset=-3: starts from the third element from the end (i.e., value30).length=None: returns elements to the end.
<a id="h-conclusion"></a>
Conclusion
In conclusion, the Series.slice() method in Polars offers a straightforward and efficient way to retrieve a portion of data based on position.
Happy Learning!!
<a id="h-related-articles"></a>
Related Articles
- Polars quantile() – Usage & Examples
- Polars extend() Usage & Examples
- Polars abs() – Explained by Examples
- Polars Shape Usage & Examples
- Polars Series mean() – Usage & Examples
- Polars Series min() – Explained by Examples
- Polars clear() – Explained by Examples
- Polars interpolate() Function with Examples
- Polars rolling_mean() – Usage & Examples
- Polars Series limit() – Explained by Examples
- Polars struct.schema() – Explained by Examples
- Polars Series fill_null() Function with Examples
<a id="h-references"></a>
