Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support round for Decimal type #15151

Closed
Julian-J-S opened this issue Mar 19, 2024 · 3 comments · Fixed by #20720
Closed

Support round for Decimal type #15151

Julian-J-S opened this issue Mar 19, 2024 · 3 comments · Fixed by #20720
Labels
accepted Ready for implementation enhancement New feature or an improvement of an existing feature

Comments

@Julian-J-S
Copy link
Contributor

Description

Currently the decimal type does not suport round. Imo this makes sense to implement for many financial calculation where you require the precision of a decimal but in the end need to round to some fixed value.

from decimal import Decimal as D
import polars as pl

pl.DataFrame({"x": [D("0.246")]}).with_columns(r=pl.col("x").round(decimals=2))

# InvalidOperationError: `round` operation not supported for dtype `decimal[*,3]`

Currently I can only truncate the decimal for example with .cast(pl.Decimal(scale=2)) but this ofc does NOT round.

@Julian-J-S Julian-J-S added the enhancement New feature or an improvement of an existing feature label Mar 19, 2024
@ritchie46 ritchie46 added the accepted Ready for implementation label Jul 6, 2024
@github-project-automation github-project-automation bot moved this to Ready in Backlog Jul 6, 2024
@DeflateAwning
Copy link
Contributor

This seems like an essential behaviour function to have when working with pl.Decimal type. Going to have to use int and keep track of the decimal place myself, I think.

@lostmygithubaccount
Copy link

lostmygithubaccount commented Aug 23, 2024

just chiming in that this is still an issue in 1.5.0, though it looks like the error has slightly changed:

[ins] In [3]: customer.limit(5).collect()
Out[3]:
shape: (5, 8)
┌───────────┬────────────────────┬─────────────────────────────┬─────────────┬─────────────────┬───────────────┬──────────────┬────────────────────────────┐
│ c_custkeyc_namec_addressc_nationkeyc_phonec_acctbalc_mktsegmentc_comment                  │
│ ------------------------                        │
│ i64strstri32strdecimal[15,2] ┆ strstr                        │
╞═══════════╪════════════════════╪═════════════════════════════╪═════════════╪═════════════════╪═══════════════╪══════════════╪════════════════════════════╡
│ 1Customer#000000001 ┆ j5JsirBM9PsCy0O1m           ┆ 15          ┆ 25-989-741-2988 ┆ 711.56        ┆ BUILDING     ┆ y final requests wake      │
│           ┆                    ┆                             ┆             ┆                 ┆               ┆              ┆ slyly qu…                  │
│ 2Customer#000000002 ┆ 487LW1dovn6Q4dMVymKwwLE9OKf ┆ 13          ┆ 23-768-687-3665 ┆ 121.65        ┆ AUTOMOBILE   ┆ y carefully regular foxes. │
│           ┆                    ┆ 3QG                         ┆             ┆                 ┆               ┆              ┆ sly…                       │
│ 3Customer#000000003 ┆ fkRGN8nY4pkE                ┆ 1           ┆ 11-719-748-3364 ┆ 7498.12       ┆ AUTOMOBILE   ┆ fully. carefully silent    │
│           ┆                    ┆                             ┆             ┆                 ┆               ┆              ┆ instru…                    │
│ 4Customer#000000004 ┆ 4u58h fqkyE                 ┆ 4           ┆ 14-128-190-5944 ┆ 2866.83       ┆ MACHINERY    ┆ sublate. fluffily even     │
│           ┆                    ┆                             ┆             ┆                 ┆               ┆              ┆ instru…                    │
│ 5Customer#000000005 ┆ hwBtxkoBF qSW4KrIk5U        ┆ 3           ┆ 13-750-942-6364 ┆ 794.47        ┆ HOUSEHOLD    ┆ equests haggle furiously   │
│           ┆                    ┆ 2B1AU7H                     ┆             ┆                 ┆               ┆              ┆ again…                     │
└───────────┴────────────────────┴─────────────────────────────┴─────────────┴─────────────────┴───────────────┴──────────────┴────────────────────────────┘

[ins] In [4]: customer.select("c_acctbal").collect()["c_acctbal"].dtype
Out[4]: Decimal(precision=15, scale=2)

[ins] In [5]: customer.select("c_acctbal").collect()["c_acctbal"].dtype.is_numeric()
Out[5]: True

[ins] In [6]: customer.select(pl.col("c_acctbal").round(1)).limit(5).collect()
---------------------------------------------------------------------------
InvalidOperationError                     Traceback (most recent call last)
Cell In[6], line 1
----> 1 customer.select(pl.col("c_acctbal").round(1)).limit(5).collect()

File ~/repos/ibis-bench/.venv/lib/python3.11/site-packages/polars/lazyframe/frame.py:2027, in LazyFrame.collect(self, type_coercion, predicate_pushdown, projection_pushdown, simplify_expression, slice_pushdown, comm_subplan_elim, comm_subexpr_elim, cluster_with_columns, no_optimization, streaming, engine, background, _eager, **_kwargs)
   2025 # Only for testing purposes
   2026 callback = _kwargs.get("post_opt_callback", callback)
-> 2027 return wrap_df(ldf.collect(callback))

InvalidOperationError: round can only be used on numeric types

@lukemanley
Copy link
Contributor

#19760 added rounding support for Decimal. I think this can be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted Ready for implementation enhancement New feature or an improvement of an existing feature
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

5 participants