• Post author:
  • Post category:Polars
  • Post last modified:June 3, 2025
  • Reading time:9 mins read
You are currently viewing 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.

Advertisements

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 –

  • The extend() method appends values from another Series or iterable to the end of the current Series.
  • It modifies the original Series in-place rather than creating a new Series.
  • The method returns the updated Series (self).
  • The extend() method is useful for efficiently growing a Series without extra memory allocation.
  • Extending with an empty Series or iterable leaves the original Series unchanged.
  • You can extend a Series using another Series or any iterable of compatible values.
  • The Series being appended must have the same data type as the original Series.
  • extend() is useful in scenarios where data arrives in chunks or batches.

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

Parameters of the Polars series extend()

It allows only one parameter.

  • other – A polars.Series object whose values you want to append to the current Series.

Return Value

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

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.

polars series extend

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.

polars series extend

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.

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.

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
# ]

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

References