Built with Tutify.io

    AIStudyLabs

    Convert Polars Series to List

    Convert Polars Series to List

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

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

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

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

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

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

    <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

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

    References