Built with Tutify.io

    AIStudyLabs

    Polars Series extend() Usage & Examples

    Polars Series extend() Usage & Examples

    In Polars, the extend() method is used to append values from another Series or iterable to the end of an existing Series. This operation updates the original Series in-place, so the changes affect the original object directly without creating a new Series. In other words, you can expand your Series by adding new values without generating a separate object.

    In this article, I will examine the Polars Series extend() method by explaining its syntax, parameters, and usage, while showing how it modifies the original Series in-place and returns the updated Series (self).

    Key Points –

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

    Syntax of Polars Series extend() Method

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

    
    # Syntax of series extend()
    Series.extend(other: Series) → Self
    

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

    Parameters of the Polars series extend()

    It allows only one parameter.

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

    Return Value

    This function returns the modified Series (Self) after appending the values. The operation is in-place, so the original Series is changed.

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

    Usage of Polars Series extend() Method

    The extend() method in Polars Series appends the values from another Series to the end of the current one. This operation modifies the original Series in-place by adding all elements from the other Series.

    Now, let’s create a Polars series using a list of values and use the extend() function.

    
    import polars as pl
    
    ser1 = pl.Series("ints", [2, 4, 6])
    ser2 = pl.Series("ints", [8, 10])
    print("First Series:\n", ser1)
    print("Second Series:\n", ser2)
    

    Yields below output.

    To extend a Polars Series with integers (i.e., append values from one Series to another), you can use the extend() method on the Series. This modifies the original Series by adding elements from another iterable or Series.

    
    # Extend ser1 with values from ser2
    ser1.extend(ser2)
    print("Extended Series:\n", ser1)
    

    In the above example, this shows how extend() appends the values of one Series to another in-place without creating a new Series.

    <a id="h-extending-a-series-with-an-empty-series"></a>

    Extending a Series with an Empty Series

    Extending a Polars Series with an empty Series is allowed and leaves the original Series unchanged, since there are no elements to add.

    
    import polars as pl
    
    ser1 = pl.Series("ints", [2, 4, 6])
    empty_ser = pl.Series("ints", [])
    
    ser1.extend(empty_ser)
    print("After extending with empty Series:\n", ser1)
    
    # Output:
    # After extending with empty Series:
    # shape: (3,)
    # Series: 'ints' [i64]
    # [
    #	2
    #	4
    #	6
    # ]
    

    In the above example, extending with an empty Series does not change the original Series. Since there are no new elements to add, s1 remains the same.

    <a id="h-extending-a-series-with-strings"></a>

    Extending a Series with Strings

    You can extend a Polars Series with string values using the extend() method, but only if the original Series also has a string data type. Polars enforces strict typing, so you cannot append strings to a Series with an integer dtype.

    
    import polars as pl
    
    ser1 = pl.Series("Courses", ["Spark", "Pyspark"])
    ser2 = pl.Series("Courses", ["Hadoop", "Polars"])
    
    ser1.extend(ser2)
    print(ser1)
    
    # Output:
    # shape: (4,)
    # Series: 'Courses' [str]
    # [
    #	"Spark"
    #	"Pyspark"
    #	"Hadoop"
    #	"Polars"
    # ]
    

    In the above example, this shows how extend() works with string data, appending elements in-place to the original Series.

    <a id="h-extend-with-float-series"></a>

    Extend with Float Series

    Using the extend() method, you can append float values to a Polars Series. Just like with other data types, the data types must match; only a Series with a float dtype can be extended with another float Series.

    
    import polars as pl
    
    ser1 = pl.Series("floats", [1.1, 2.2])
    ser2 = pl.Series("floats", [3.3, 4.4])
    
    ser1.extend(ser2)
    print("Extended float Series:\n", ser1)
    
    # Output:
    # Extended float Series:
    # shape: (4,)
    # Series: 'floats' [f64]
    # [
    #	1.1
    #	2.2
    #	3.3
    #	4.4
    # ]
    

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

    Conclusion

    In conclusion, the extend() method in Polars Series is a simple yet powerful way to append values from one Series to another. It works in-place, meaning the original Series is directly modified. However, since Polars enforces strict data types, the Series being extended must have the same dtype as the one being appended.

    Happy Learning!!

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

    Related Articles

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

    References