• Post author:
  • Post category:Polars
  • Post last modified:April 25, 2025
  • Reading time:12 mins read
You are currently viewing Polars Series rename() – by Examples

In Polars, the rename() method is used to change the name (or label) of a Series. A Series is a one-dimensional data array in Polars, typically representing a column within a DataFrame. This method is helpful when you need to change the label of a Series, especially after performing transformations or extracting it from a DataFrame.

Advertisements

In this article, I will explain the rename() function in Polars Series, including its syntax, parameters, usage, and how it returns a new Series object with the updated name (label). While the underlying data and structure of the Series remain unchanged, the Series is now associated with the new name you assign.

Key Points –

  • The rename() method is used to change the name (label) of a Polars Series.
  • It returns a new Series with the updated name; the original Series remains unchanged.
  • The method takes a single string argument, which becomes the new name.
  • Useful for improving readability and clarity, especially after transformations or filtering.
  • The method does not alter the underlying data or structure of the Series.
  • The rename() method accepts a single parameter: a string representing the new name.
  • The rename() method works with all data types, including integers, floats, strings, and booleans.

Polars Series rename() Introduction

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


# Syntax of rename()
Series.rename(name: str) → Series

Parameters of the Series rename()

It allows only one parameter.

  • name – The name parameter is a string that specifies the new label for the Series.

Return Value

This function returns a new Series with the updated name.

Usage of Polars Series rename() Method

The rename() method is used to change the name (or label) of a Polars Series. It doesn’t affect the data within the Series, only the name associated with it.

First, let’s create a Polars Series.


import polars as pl

# Sample Series
ser = pl.Series( 'Courses', ["Spark","Hadoop","Python","Pandas"])
print("Original Series:\n", ser)

Yields below output.

polars series rename

To rename a Polars Series, you can use the rename() method to assign a new name to the Series. For instance, the Series name "Courses" is updated to "New_Courses", while the data within the Series remains unaffected.


# Renaming the Series
ser_renamed = ser.rename('New_Courses')
print("Renamed Series:\n", ser_renamed)

Here,

  • We created a Series named "Courses" with some string values.
  • We renamed the Series using the rename() method to "New_Courses".
  • The result is a new Series with the updated name.
polars series rename

Renaming a Series with Numeric Data

Renaming a Polars Series with numeric data follows the same approach as renaming a Series with any other data type. Here’s how to rename a Series that contains numeric values.


import polars as pl

# Create a Series with numeric data
ser = pl.Series('Scores', [95, 88, 78, 92])

# Rename the Series to a more descriptive name
ser_renamed = ser.rename("Student_Scores")
print("Renamed Series:\n", ser_renamed)

# Output:
# Renamed Series:
# shape: (4,)
# Series: 'Student_Scores' [i64]
# [
#	95
#	88
#	78
#	92
# ]

Here,

  • The original Series has the name "Scores" and contains numeric values (e.g., 95, 88, etc.).
  • We renamed the Series to "Student_Scores" using the rename() method.
  • The result is a new Series with the updated name, but the numeric data remains the same.

Renaming a Series with Boolean Data

The rename() function is used to update the name of a Polars Series, and it can be applied to any data type, including boolean values. Below is a detailed explanation of how to use the rename() method on a Series that contains boolean data.


import polars as pl

# Create a Series with Boolean data
ser = pl.Series('Passed', [True, False, True, True])

# Rename the Series to a more descriptive name
ser_renamed = ser.rename("Exam_Passed_Status")
print("Renamed Series:\n", ser_renamed)

# Output:
# Renamed Series:
# shape: (4,)
# Series: 'Exam_Passed_Status' [bool]
# [
#	true
#	false
#	true
#	true
# ]

Here,

  • The original Series has the name "Passed" and contains Boolean values (True and False).
  • We renamed the Series to "Exam_Passed_Status" using the rename() method.
  • The data within the Series remains unchanged, and the only modification is the name of the Series.

Renaming a Series After Filtering

To rename a Polars Series after applying a filter, you can first filter the data based on a condition and then use the rename() method to assign a new, more meaningful name to the resulting Series.


import polars as pl

# Create a Series with numeric data
ser = pl.Series('Scores', [95, 88, 78, 92, 85])

# Filter the Series: Keep only values greater than 80
filtered_ser = ser.filter(ser > 80)

# Rename the filtered Series
ser_renamed = filtered_ser.rename("High_Scores")
print("Renamed Filtered Series:\n", ser_renamed)

# Output:
# Renamed Filtered Series:
# shape: (4,)
# Series: 'High_Scores' [i64]
# [
#	95
#	88
#	92
#	85
# ]

Here,

  • We created a Series, ser, containing numeric data (scores).
  • The filter() method was applied to keep only the values greater than 80, resulting in a filtered Series.
  • We renamed the filtered Series using the rename() method to "High_Scores".
  • Finally, we printed the renamed Series, which contains the scores greater than 80.

Renaming After Transformation

Renaming a Polars Series after a transformation is a clear and effective way to reflect the updated purpose or context of the data. For instance, after performing operations like scaling, rounding, or value adjustments, giving the Series a new name helps indicate its transformed role.


import polars as pl

# Original Series
ser = pl.Series("Scores", [70, 85, 90, 60])

# Add 5 bonus points to each score
ser_renamed = (ser + 5).rename("Adjusted_Scores")
print("Renamed Transformed Series:\n", ser_renamed)

# Output:
# 
# Renamed Transformed Series:
# shape: (4,)
# Series: 'Adjusted_Scores' [i64]
# [
#	75
#	90
#	95
#	65
# ]

Renaming After Applying a 10% Increase

You can rename a Polars Series after applying a percentage increase, a common transformation when adjusting values like prices, scores, or quantities.


import polars as pl

# Original Series
ser = pl.Series("Scores", [70, 85, 90, 60])

# Apply a 10% increase and rename the result
ser_renamed = (ser * 1.10).round(0).rename("Increased_By_10_Percent")
print("Renamed Transformed Series:\n", ser_renamed)

# Output:
# Renamed Transformed Series:
# shape: (4,)
# Series: 'Increased_By_10_Percent' [f64]
# [
#	77.0
#	94.0
#	99.0
#	66.0
# ]

Here,

  • The original Scores Series is scaled up by 10% using ser * 1.10.
  • We use round(0) to round to the nearest whole number (optional).
  • Finally, rename("Increased_By_10_Percent") gives the new Series a meaningful name.

Conclusion

In summary, the rename() method in Polars Series is a simple yet powerful way to update the label of a Series without modifying its underlying data. Whether you’re cleaning up column names after transformations or making your data more readable, rename() helps keep your Series well-organized and descriptive.

Happy Learning!!

References