Built with Tutify.io

    AIStudyLabs

    Polars Series limit() – Explained by Examples

    Polars Series limit() – Explained by Examples

    In Polars, the Series.limit() is used to return a new Series that contains at most the first n elements of the original Series. It is a simple and effective way to preview or reduce the size of a Series for analysis, testing, or further processing. The method works with any Series data type (integers, floats, strings, booleans, etc.) and does not modify the original Series.

    In this article, I will explain the Polars Series limit() method, including its syntax, parameters, and functionality. This method produces a new Series that includes up to the first n elements from the original Series.

    Key Points –

    <a id="h-syntax-of-polars-series-limit-method"></a>

    Syntax of Polars Series limit() Method

    Let’s know the syntax of the series limit() method.

    
    # Syntax of series limit()
    Series.limit(n: int = 10) → Series
    

    <a id="h-parameters-of-the-polars-series-limit"></a>

    Parameters of the Polars series limit()

    It allows only one parameter.

    <a id="h-return-value"></a>

    Return Value

    This function returns a new Series containing up to the first n values from the original Series.

    <a id="h-usage-of-polars-series-limit-method"></a>

    Usage of Polars Series limit() Method

    Series.limit(n) creates a new Series containing just the first n elements of the original Series. It effectively restricts the Series to its top n entries. If the Series has fewer than n elements, it returns all of them.

    Now, let’s create a Polars series.

    
    import polars as pl
    
    ser = pl.Series("numbers", [5, 10, 15, 20, 25])
    print("Original Series:\n", ser)
    

    Yields below output.

    To keep only the first 3 elements of a Series in Polars, you can use the limit() method. This method allows you to restrict an integer Series to its first 3 values. Below is a straightforward example demonstrating how to apply limit() on an integer Series in Polars.

    
    # Limit first 3 elements of integer Series
    ser2 = ser.limit(3)
    print("Limited Series (first 3 elements):\n", ser2)
    

    In the above example, ser.limit(3) returns a new Series containing only the first 3 elements from the original Series.

    <a id="h-limit-first-0-elements-returns-empty-series"></a>

    Limit First 0 Elements (Returns Empty Series)

    If you apply limit(0) to a Polars Series, it returns an empty Series. The result retains the same name and data type as the original Series but contains no elements.

    
    import polars as pl
    
    ser = pl.Series("numbers", [5, 10, 15, 20, 25])
    
    # Limit to first 0 elements
    ser2 = ser.limit(0)
    print("Limited Series (first 0 elements):\n", ser2)
    
    # Output:
    # Limited Series (first 0 elements):
    # shape: (0,)
    # Series: 'numbers' [i64]
    # [
    # ]
    

    In the above example, so limit(0) gives you a Series with no elements but retains the original Series' name and data type.

    <a id="h-limit-on-float-series"></a>

    Limit on Float Series

    You can also use limit() on a float Series in Polars, it works the same way as with integers. Here’s an example of using limit() on a float Series in Polars.

    
    import polars as pl
    
    ser = pl.Series("float_numbers", [1.1, 2.2, 3.3, 4.4, 5.5])
    
    # Limit to first 3 elements
    ser2 = ser.limit(3)
    print("Limited Series (first 3 elements):\n", ser2)
    
    # Output:
    # Limited Series (first 3 elements):
    # shape: (3,)
    # Series: 'float_numbers' [f64]
    # [
    #	1.1
    #	2.2
    #	3.3
    # ]
    

    In the above example, you can apply limit(n) to a Series of any data type, integers, floats, strings, booleans, and more. In this example, it returns the first 3 float values from the Series. The Series keeps its original name and float data type (f64, which stands for 64-bit float).

    <a id="h-limit-on-boolean-series"></a>

    Limit on Boolean Series

    You can also use limit() on a Boolean Series. Here’s an example showing how the limit() method functions on a Boolean Series in Polars.

    
    import polars as pl
    
    ser = pl.Series("bool_values", [True, False, True, True, False])
    
    # Limit to first 3 elements
    ser2 = ser.limit(3)
    print("Limited Series (first 3 elements):\n", ser2)
    
    # Output:
    # Limited Series (first 3 elements):
    # shape: (3,)
    #Series: 'bool_values' [bool]
    #[
    #	true
    #	false
    #	true
    #]
    

    In the above example, the original Series contains 5 boolean values. Using limit(3) retains only the first three values: True, False, and the next one. This method is handy when you want to quickly extract a portion of a Boolean Series for analysis or filtering.

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

    Conclusion

    In conclusion, the limit() method in Polars is a simple and efficient way to select the first n elements from a Series, regardless of its data type. It’s useful for previewing data, sampling, or slicing Series quickly during analysis.

    Happy Learning!!

    <a id="h-related-articles"></a>

    Related Articles

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

    References