Code Monkey home page Code Monkey logo

Comments (2)

Julian-J-S avatar Julian-J-S commented on September 21, 2024

hmm 🤔 not sure about the details. In any case you should not convert floats to decimal if you need this high precision/scale. You should try to use string -> decimal

Some alternatives that "work"

Same as your but cast inside with_columns instead of constructor

pl.DataFrame(
    data={"a": [12345678.987654321, 112233445566778899.456, 0.1234567890123456789]}
).with_columns(
    d1=pl.col("a").cast(pl.Decimal),
    d2=pl.col("a").cast(pl.Decimal(scale=19)),
)
# shape: (3, 3)
# ┌───────────â”Ŧ────────────────────â”Ŧ────────────────────────────────────────┐
# │ a         ┆ d1                 ┆ d2                                     │
# │ ---       ┆ ---                ┆ ---                                    │
# │ f64       ┆ decimal[38,0]      ┆ decimal[38,19]                         │
# ╞═══════════â•Ē════════════════════â•Ē════════════════════════════════════════╡
# │ 1.2346e7  ┆ 12345678           ┆ 12345678.9876543211786207232           │
# │ 1.1223e17 ┆ 112233445566778896 ┆ 112233445566778890.8777382082762506240 │
# │ 0.123457  ┆ 0                  ┆ 0.1234567890123456768                  │
# └───────────┴────────────────────┴────────────────────────────────────────┘

Use String

pl.DataFrame(
    data={
        "a": ["12345678.987654321", "112233445566778899.456", "0.1234567890123456789"]
    }
).with_columns(d=pl.col("a").cast(pl.Decimal))

# shape: (3, 2)
# ┌────────────────────────â”Ŧ────────────────────────────────────────┐
# │ a                      ┆ d                                      │
# │ ---                    ┆ ---                                    │
# │ str                    ┆ decimal[*,19]                          │
# ╞════════════════════════â•Ē════════════════════════════════════════╡
# │ 12345678.987654321     ┆ 12345678.9876543210000000000           │
# │ 112233445566778899.456 ┆ 112233445566778899.4560000000000000000 │
# │ 0.1234567890123456789  ┆ 0.1234567890123456789                  │
# └────────────────────────┴────────────────────────────────────────┘

from polars.

alexander-beedie avatar alexander-beedie commented on September 21, 2024

This seems reasonable to me - you can't create exact precision data from inexact precision data. Python allows it and take a look at the results, using your example values; you will have simply encoded the inexact precision such that each one of the initialised Decimal values is arguably "wrong" (and the second value in't just arguably wrong, it is completely wrong ;):

(Decimal('12345678.987654320895671844482421875'),
 Decimal('112233445566778896'),
 Decimal('0.12345678901234567736988623209981597028672695159912109375'))

ℹī¸ You should really init Decimals from exact data, such as string or int
(and I'd be cautious about casting, for the same reason).

from polars.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤ī¸ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.