Built with Tutify.io

    AIStudyLabs

    Polars Series slice() – Usage & Examples

    Polars Series slice() – Usage & Examples

    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 –

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

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

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

    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,

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

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

    <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

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

    References