You can convert a Polars Series to a Python list using the to_list() method. This process involves changing the data from Polars' optimized, high-performance format into a standard Python list, which is a native data type in Python.
A Polars Series is a one-dimensional labeled array, similar to a column in a DataFrame. It's part of the Polars DataFrame library, which is optimized for speed and memory efficiency, especially with large datasets.
In this article, I’ll walk you through the syntax for converting a Polars Series to a list, explain its parameters, and show how to use it to return a Python list containing the values from the Series. The data types of the list elements will match those of the original Series.
Key Points –
- A Polars Series is a one-dimensional array designed for high-performance data operations.
- Use the
to_list()method to convert a Polars Series to a standard Python list. - Supports all data types in Polars, including integers, floats, strings, booleans, and datetime values.
- Null values in a Series are converted to None in the resulting list.
- The list will retain the original data types of the Series elements.
- Converting to a list changes the data structure from a Polars object to a native Python list.
- Polars Series are optimized for speed with large datasets, whereas lists are more flexible but less efficient for such operations.
- The
to_list()method is available for all data types supported by Polars (numerics, strings, booleans, etc.).
<a id="h-syntax-of-nbsp-polars-series-to-list"></a>
Syntax of Polars Series to List
Let’s know the syntax of the Series.to_list
# Syntax of Series.to_list
Series.to_list() → list[Any]
<a id="h-parameters-of-the-polars-series-to-list"></a>
Parameters of the polars.Series.to_list
The following are the parameters of the polars.Series.to_list
- This method does not take any arguments.
<a id="h-return-value"></a>
Return Value
This function returns a Python list containing the values of the Series. The data types of the list elements correspond to the data types of the Series elements.
<a id="h-usage-of-nbsp-polars-series-to-list"></a>
Usage of Polars Series to List
To convert a Polars Series to a Python list, you can use the to_list() method, which is the most straightforward and recommended approach.
To run some examples of converting a Polars Series to a list, let’s create a Polars Series.
import polars as pl
# Create a Polars Series
ser = pl.Series("Fees", [22000,25000,30000,25000,28000])
print("Original Polars Series:\n", ser)
Yields below output.
To convert a Polars Series containing integers into a Python list, you can use the to_list() method. This method allows you to transform the Polars Series into a standard Python list.
# Convert the Polars Series to a Python list
result = ser.to_list()
print("Converted to Python list:\n", result)
Here,
seris a Polars Series with integer values.to_list()converts it into a regular Python list of integers.
<a id="h-convert-the-polars-series-list-to-floating-point-numbers"></a>
Convert the Polars Series List to Floating-Point Numbers
To convert a Polars Series to a list of floating-point numbers, you'll first convert the Series to floating-point values using the cast() method, and then convert the resulting Series to a Python list using to_list().
import polars as pl
# Create a Polars Series with integer values
polars_series = pl.Series("Scores", [50, 60, 70, 80])
# Convert the Polars Series to float
float_series = polars_series.cast(pl.Float64)
# Convert the floating-point Series to a Python list
result = float_series.to_list()
print("Converted list of floats:\n", result)
# Output:
# Converted list of floats:
# [50.0, 60.0, 70.0, 80.0]
Here,
cast(pl.Float64)converts the Series elements to 64-bit floating-point numbers (f64).to_list()converts the Series into a native Python list, now with floating-point numbers.
<a id="h-convert-the-polars-series-list-using-boolean-values"></a>
Convert the Polars Series List Using Boolean Values
You can convert a Polars Series with Boolean values (True or False) into a regular Python list by using the to_list() method. Whether the Series contains Booleans or other data types, the to_list() method remains the same for the conversion.
import polars as pl
ser = pl.Series("flags", [True, False, True, True])
# Using boolean values
result = ser.to_list()
print("Converted to list boolean:\n", result)
# Output:
# Converted to list boolean:
# [True, False, True, True]
Here,
- The Series should contain only Boolean values.
- The
to_list()method returns a list ofTrueandFalsevalues in Python. - This is useful for filtering, condition checks, or exporting logic flags.
- Return type,
list[bool]
<a id="h-convert-the-polars-series-list-using-series-with-null-values"></a>
Convert the Polars Series List Using Series with Null Values
To convert a Polars Series with null (missing) values to a Python list, you can still use the to_list() method. Polars will preserve the null values as Python None in the resulting list.
import polars as pl
# Create a Polars Series with some null (None) values
polars_series = pl.Series("Scores", [85, None, 90, None, 75])
# Convert the Series to a Python list
result = polars_series.to_list()
print("Converted List with Nulls:\n", result)
# Output:
# Converted List with Nulls:
# [85, None, 90, None, 75]
Here,
Nonein Python is equivalent tonullin Polars.- The
to_list()method preserves these null values, making it easy to work with them in native Python logic. - Return type:
list[Any](may containint,None, etc.)
<a id="h-convert-the-polars-series-list-using-date-values"></a>
Convert the Polars Series List Using Date Values
To convert a Polars Series with date values to a Python list, you can use the to_list() method. Polars will convert its internal date types to Python's datetime.date or datetime.datetime objects.
import polars as pl
from datetime import date
# Create a Polars Series with date values
ser = pl.Series("dates", [date(2024, 1, 1), date(2024, 2, 14), date(2024, 12, 31)])
# Convert to a Python list
result = ser.to_list()
print("Converted List of Dates:\n", result)
# Output:
# Converted List of Dates:
# [datetime.date(2024, 1, 1), datetime.date(2024, 2, 14), datetime.date(2024, 12, 31)]
Here,
- Polars stores date/datetime efficiently, but
to_list()converts them into standard Pythondatetime.dateordatetime.datetime. - Resulting list is of type
list[date]orlist[datetime]depending on Series type. - This is helpful when working with external Python libraries like
matplotlib,pandas, or for custom processing.
<a id="h-conclusion"></a>
Conclusion
In conclusion, converting a Polars Series to a Python list with the to_list() method is an effective and versatile operation that enhances flexibility. It allows you to work with the Series data outside of the Polars framework, making it compatible with other Python libraries, functions, and native Python operations. The conversion retains the original data types, ensuring the integrity of the data. This approach is especially useful for working with smaller datasets, debugging, or exporting data to various formats.
Happy Learning!!
<a id="h-related-articles"></a>
Related Articles
- Polars Series max() Function
- Polars Series Unique Values
- Convert Polars Series to List
- Polars Series rename() – by Examples
- Polars Series cast() – Usage & Examples
- Polars Series head() Method with Examples
- Polars Series min() – Explained by Examples
- How to Convert Struct to Series in Polars?
- How to Replace Certain Values in a Polars Series?
- How to Transform a Series of a Polars DataFrame?
- Polars Series filter() Method with Examples
- How to Convert Polars Series to Pandas Series in Python?
<a id="h-references"></a>
