Built with Tutify.io

    AIStudyLabs

    Convert Polars Casting a Column to Decimal

    Convert Polars Casting a Column to Decimal

    In Polars, casting a column to Decimal involves converting the column’s data type to a high-precision decimal format. This is crucial in scenarios where exactness and control over decimal places are essential, such as financial calculations, measurements, or any data analysis that demands precision.

    By casting a column to Decimal, you control the total number of digits (precision) and the number of digits after the decimal point (scale), helping you maintain accuracy in your data. In this article, I will explain how to convert polars casting a column to decimal.

    Key Points –

    <a id="h-usage-of-nbsp-polars-casting-a-column-to-decimal"></a>

    Usage of Polars Casting a Column to Decimal

    Casting a column to Decimal in Polars means changing the column’s data type to Decimal, which allows for high-precision numeric operations. This is useful when working with data that requires exact decimal values, such as financial calculations or scientific measurements.

    First, let’s create a Polars DataFrame.

    
    import polars as pl
    
    # Creating a DataFrame 
    technologies = {
       'Courses': ["Spark", "PySpark", "Pandas"],
       'Fee': [22000, 25000, 26000], 
       'Discount': [100.50, 250.20, 140.40] 
    }
    
    df = pl.DataFrame(technologies)
    print("Original DataFrame:\n", df)
    

    Yields below output.

    To cast a Float64 column to a Decimal in Polars, you can use the cast() method. However, Polars doesn't directly support a Decimal type, so you can convert it to a Decimal using pl.datatypes.Decimal by specifying the precision and scale.

    
    # Casting the 'Discount' column from Float64 to Decimal
    df2 = df.with_columns(
        pl.col('Discount').cast(pl.Decimal(10, 2)).alias('Discount_Decimal'))
    print("DataFrame after casting 'Discount' to Decimal:\n", df2)
    

    Here,

    Alternatively, to cast a Float64 column to Decimal in Polars, you can use the cast() method.

    
    # Cast 'Discount' column from Float64 to Decimal
    df2 = df.with_columns(
        pl.col("Discount").cast(pl.Decimal))
    print("DataFrame with 'Discount' as Decimal:\n", df2)
    
    # Output:
    # ataFrame with 'Discount' as Decimal:
    3 shape: (3, 3)
    ┌─────────┬───────┬───────────────┐
    │ Courses ┆ Fee   ┆ Discount      │
    │ ---     ┆ ---   ┆ ---           │
    │ str     ┆ i64   ┆ decimal[38,0] │
    ╞═════════╪═══════╪═══════════════╡
    │ Spark   ┆ 22000 ┆ 100           │
    │ PySpark ┆ 25000 ┆ 250           │
    │ Pandas  ┆ 26000 ┆ 140           │
    └─────────┴───────┴───────────────┘
    

    Here,

    <a id="h-casting-an-integer-column-to-decimal"></a>

    Casting an Integer Column to Decimal

    To cast an integer column to a Decimal in Polars, you can use the same approach as you would for a Float64 column, but instead, cast the integer column to Decimal. By using the cast() method, you can convert the integer column to Decimal for greater precision. In this example, we'll cast the Fee column (currently an integer) to Decimal to ensure higher precision.

    
    # Casting the 'Fee' column from Integer to Decimal
    df2 = df.with_columns(
        pl.col('Fee').cast(pl.Decimal(10, 2)).alias('Fee_Decimal'))
    print("\nDataFrame after casting 'Fee' to Decimal:\n", df2)
    
    # Output:
    # DataFrame after casting 'Fee' to Decimal:
    # shape: (3, 4)
    ┌─────────┬───────┬──────────┬───────────────┐
    │ Courses ┆ Fee   ┆ Discount ┆ Fee_Decimal   │
    │ ---     ┆ ---   ┆ ---      ┆ ---           │
    │ str     ┆ i64   ┆ f64      ┆ decimal[10,2] │
    ╞═════════╪═══════╪══════════╪═══════════════╡
    │ Spark   ┆ 22000 ┆ 100.5    ┆ 22000.00      │
    │ PySpark ┆ 25000 ┆ 250.2    ┆ 25000.00      │
    │ Pandas  ┆ 26000 ┆ 140.4    ┆ 26000.00      │
    └─────────┴───────┴──────────┴───────────────┘
    

    Here,

    <a id="h-casting-a-column-to-decimal-with-specific-precision"></a>

    Casting a Column to Decimal with Specific Precision

    To cast a column to Decimal with specific precision in Polars, you can use the cast() method with the desired precision and scale. The precision represents the total number of significant digits, while the scale represents the number of digits after the decimal point.

    
    # Cast 'Discount' column from Float64 
    # To Decimal with precision 10 and scale 3
    df_decimal = df.with_columns(
        pl.col("Discount").cast(pl.Decimal(10, 3)))
    print("DataFrame with 'Discount' as Decimal (precision 10, scale 3):\n", df_decimal)
    
    # Output:
    # DataFrame with 'Discount' as Decimal (precision 10, scale 3):
    # shape: (3, 3)
    ┌─────────┬───────┬───────────────┐
    │ Courses ┆ Fee   ┆ Discount      │
    │ ---     ┆ ---   ┆ ---           │
    │ str     ┆ i64   ┆ decimal[10,3] │
    ╞═════════╪═══════╪═══════════════╡
    │ Spark   ┆ 22000 ┆ 100.500       │
    │ PySpark ┆ 25000 ┆ 250.200       │
    │ Pandas  ┆ 26000 ┆ 140.400       │
    └─────────┴───────┴───────────────┘
    

    Here,

    <a id="h-casting-a-column-with-null-values-to-decimal"></a>

    Casting a Column with Null Values to Decimal

    When you have a column with null values and want to cast it to Decimal, Polars will handle the null values gracefully. The null values will remain null after casting to Decimal. This behavior is consistent with Polars' handling of null values across data types.

    
    import polars as pl
    
    # Creating a DataFrame with null values
    technologies = {
       'Courses': ["Spark", "PySpark", "Pandas"],
       'Fee': [22000, 25000, 26000], 
       'Discount': ["100.50", None, "140.40"]  # Contains string numbers and a null value
    }
    df = pl.DataFrame(technologies)
    
    # Cast 'Price' column from String to Decimal
    df2 = df.with_columns(
        pl.col("Discount").cast(pl.Decimal(10, 2)))
    print("DataFrame with 'Discount' as Decimal (with nulls):\n", df2)
    
    # Output:
    # DataFrame with 'Discount' as Decimal (with nulls):
    # shape: (3, 3)
    ┌─────────┬───────┬───────────────┐
    │ Courses ┆ Fee   ┆ Discount      │
    │ ---     ┆ ---   ┆ ---           │
    │ str     ┆ i64   ┆ decimal[10,2] │
    ╞═════════╪═══════╪═══════════════╡
    │ Spark   ┆ 22000 ┆ 100.50        │
    │ PySpark ┆ 25000 ┆ null          │
    │ Pandas  ┆ 26000 ┆ 140.40        │
    └─────────┴───────┴───────────────┘
    

    Here,

    <a id="h-casting-a-string-column-to-decimal"></a>

    Casting a String Column to Decimal

    To cast a string column to Decimal in Polars, you can use the cast() method with the pl.Decimal type. When casting from a string, Polars will attempt to interpret the string as a valid number (e.g., a numeric string such as "100.50" or "250.75").

    
    import polars as pl
    
    # Creating a DataFrame with null values
    technologies = {
       'Courses': ["Spark", "PySpark", "Pandas"],
       'Fee': [22000, 25000, 26000], 
       'price_str': ["100.50", "250.20", "140.40"]  # String representation of prices
    }
    
    df = pl.DataFrame(technologies)
    
    # Cast 'price_str' from String to Decimal
    df2 = df.with_columns(
        pl.col("price_str").cast(pl.Decimal(10, 2))  # Precision 10, scale 2
    )
    print("DataFrame with 'price_str' as Decimal:\n", df2)
    
    # Output:
    # DataFrame with 'price_str' as Decimal:
    # shape: (3, 3)
    ┌─────────┬───────┬───────────────┐
    │ Courses ┆ Fee   ┆ price_str     │
    │ ---     ┆ ---   ┆ ---           │
    │ str     ┆ i64   ┆ decimal[10,2] │
    ╞═════════╪═══════╪═══════════════╡
    │ Spark   ┆ 22000 ┆ 100.50        │
    │ PySpark ┆ 25000 ┆ 250.20        │
    │ Pandas  ┆ 26000 ┆ 140.40        │
    └─────────┴───────┴───────────────┘
    

    Here,

    <a id="h-casting-a-column-with-negative-values-to-decimal"></a>

    Casting a Column with Negative Values to Decimal

    Casting a column with negative values to Decimal in Polars works similarly to casting columns with positive values. Negative values are preserved during the casting process, and the resulting Decimal will have the appropriate sign, precision, and scale.

    
    import polars as pl
    
    # Creating a DataFrame with null values
    technologies = {
       'Courses': ["Spark", "PySpark", "Pandas"],
       'Fee': [22000, 25000, 26000], 
      'Discount': ["-100.50", "250.20", "-140.40"]  # String representation of negative prices
    }
    
    df = pl.DataFrame(technologies)
    
    # Cast 'Discount' column from String to Decimal (precision 10, scale 2)
    df2 = df.with_columns(
        pl.col("Discount").cast(pl.Decimal(10, 2))  
    )
    print("DataFrame with 'Discount' as Decimal (negative values):\n", df2)
    
    # Output:
    # DataFrame with 'Price' as Decimal (negative values):
    # shape: (3, 3)
    ┌─────────┬───────┬───────────────┐
    │ Courses ┆ Fee   ┆ Discount      │
    │ ---     ┆ ---   ┆ ---           │
    │ str     ┆ i64   ┆ decimal[10,2] │
    ╞═════════╪═══════╪═══════════════╡
    │ Spark   ┆ 22000 ┆ -100.50       │
    │ PySpark ┆ 25000 ┆ 250.20        │
    │ Pandas  ┆ 26000 ┆ -140.40       │
    └─────────┴───────┴───────────────┘
    

    Here,

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

    Conclusion

    In summary, converting casting columns to Decimal in Polars is an effective and simple method to maintain precision, particularly useful for financial data or other scenarios requiring high numerical accuracy. Whether the data type is integer or float, applying the cast(pl.Decimal(precision, scale)) method provides better control over number formatting and helps prevent floating-point rounding errors.

    Happy Learning!!

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

    Related Articles

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

    References