In Polars, the clear() method on a Series is used to remove all the data from that Series, essentially making it an empty Series with the same name and datatype but no elements. In other words, it clears all values and returns a Series with zero length but the same schema.
In this article, I will explain the Polars Series clear() method, including its syntax, parameters, and how to use it. I will also show how it returns a new Series with the same name and data type, but containing n null values.
Key Points –
clear()returns a new Series with the same name and data type as the original.- The resulting Series contains no actual data elements unless specified.
- You can specify the length of the cleared Series by passing an integer
n. - By default,
clear()returns an empty Series with zero length. - The original Series remains unchanged;
clear()does not modify it in place. - Works with all data types supported by Polars Series (e.g., int, float, string, bool).
- Useful for resetting or initializing Series while maintaining schema consistency.
clear()is an efficient way to empty a Series without creating a new one from scratch manually.
<a id="h-syntax-of-polars-series-clear-method"></a>
Syntax of Polars Series clear() Method
Let’s know the syntax of the series clear() function.
# Syntax of series clear()
Series.clear(n: int = 0) → Series
<a id="h-parameters-of-the-polars-series-clear"></a>
Parameters of the Polars series clear()
It allows only one parameter.
n(optional, int) - The number ofnullvalues to include in the returned Series. Default is0.
<a id="h-return-value"></a>
Return Value
This function returns a new Series with the same name and data type, but with n null values.
<a id="h-usage-of-polars-series-clear-method"></a>
Usage of Polars Series clear() Method
The clear() method in Polars Series is used to return a new Series of a given length, filled with null values, while preserving the original Series name and data type.
To run some examples of the Polars Series clear() method, let’s create a Polars Series.
import polars as pl
ser = pl.Series("numbers", [2, 4, 6, 8])
print("Original Series:\n", ser)
Yields below output.
If you have a Polars Series with integer values and want to remove all its data, you can use the clear() method. This method returns a new Series that is cleared of all elements.
# Clear the Series
# Get an empty Series with the same name and dtype
ser2 = ser.clear()
print("Cleared Series:\n", ser2)
In the above example, the clear() method removes all data but keeps the Series name and dtype intact, resulting in an empty Series.
<a id="h-clear-a-series-with-string-values"></a>
Clear a Series with String Values
Alternatively, the clear() method can be used on a Polars Series with string values to remove all elements while retaining the original name and data type.
import polars as pl
ser = pl.Series("fruits", ["apple", "banana", "cherry", "date"])
ser2 = ser.clear()
print("Clear all elements:\n", ser2)
# Output:
# Clear all elements:
# shape: (0,)
#Series: 'fruits' [str]
#[
#]
Here,
- Since no argument is passed,
clear()removes all values. - The result is an empty Series, but it still has the same name (
"fruits") and type (str).
Let’s walk through an example of clearing the first 2 elements of a Polars Series.
# Clear the first 2 elements
ser2 = ser.clear(2)
print("After clearing first 2 elements:\n", ser2)
# Output:
# After clearing first 2 elements:
# shape: (2,)
# Series: 'fruits' [str]
# [
# null
# null
# ]
Here,
ser.clear(2)removes the first 2 elements:"apple"and"banana".- The returned Series will keep the same name and data type, but only contain the remaining values.
<a id="h-clear-a-series-with-floating-point-numbers"></a>
Clear a Series with Floating Point Numbers
To remove all elements from a Polars Series with floating-point numbers using the clear() method, while retaining the Series’ original name and data type.
import polars as pl
# Create a Series with float values
ser = pl.Series("weights", [56.7, 62.3, 70.5])
# Clear the Series
ser2 = ser.clear()
print("Cleared Series:\n", ser2)
# Output:
# Cleared Series:
# shape: (0,)
# Series: 'weights' [f64]
# [
# ]
Here,
pl.Series("weights", [56.7, 62.3, 70.5])creates a Series of float64 numbers with the name"weights".ser.clear()returns a new Series that has the same name, e.g.,"weights". The same data type, such asf64(float64). But contains no elements (i.e., it’s empty).- The original
seris not modified.
<a id="h-conclusion"></a>
Conclusion
In conclusion, the clear() method in Polars is a simple and effective way to remove all elements from a Series while preserving its name and data type. Whether working with integers, strings, or floating-point numbers, clear() helps maintain consistent schema structure, making it valuable for data preparation and pipeline workflows.
Happy Learning!!
<a id="h-related-articles"></a>
Related Articles
- Polars Series sum() Method with Examples
- Polars Series Unique Values
- Polars Replace String in Multiple Columns
- Polars Series sort() Usage & Examples
- Polars Series describe() – Usage & Examples
- Polars Series shift() Function with Examples
- Polars Series min() – Explained by Examples
- Polars Series sample() Function with Examples
- Polars Series list.join() Function with Examples
- Polars Series explode() – Explained by Examples
- Polars Series struct.schema() – Explained by Examples
<a id="h-references"></a>
