Built with Tutify.io

    AIStudyLabs

    Polars Series clear() – Explained by Examples

    Polars Series clear() – Explained by Examples

    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 –

    <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.

    <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,

    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,

    <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,

    <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

    <a id="h-references"></a>

    References