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

feat: Supports explode_by_offsets for decimal #15417

Merged
merged 1 commit into from Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions crates/polars-core/src/series/implementations/decimal.rs
Expand Up @@ -153,6 +153,17 @@ impl private::PrivateSeries for SeriesWrap<DecimalChunked> {
fn group_tuples(&self, multithreaded: bool, sorted: bool) -> PolarsResult<GroupsProxy> {
self.0.group_tuples(multithreaded, sorted)
}

fn explode_by_offsets(&self, offsets: &[i64]) -> Series {
self.0
.explode_by_offsets(offsets)
.decimal()
.unwrap()
.as_ref()
.clone()
.into_decimal_unchecked(self.0.precision(), self.0.scale())
.into_series()
}
}

impl SeriesTrait for SeriesWrap<DecimalChunked> {
Expand Down
29 changes: 29 additions & 0 deletions py-polars/tests/unit/datatypes/test_decimal.py
Expand Up @@ -409,3 +409,32 @@ def test_decimal_list_get_13847() -> None:
out = df.select(pl.col("a").list.get(0))
expected = pl.DataFrame({"a": [D("1.1"), D("2.1")]})
assert_frame_equal(out, expected)


def test_decimal_explode() -> None:
with pl.Config() as cfg:
cfg.activate_decimals()

nested_decimal_df = pl.DataFrame(
{
"bar": [[D("3.4"), D("3.4")], [D("4.5")]],
}
)
df = nested_decimal_df.explode("bar")
expected_df = pl.DataFrame(
{
"bar": [D("3.4"), D("3.4"), D("4.5")],
}
)
assert_frame_equal(df, expected_df)

# test group-by head #15330
df = pl.DataFrame(
{
"foo": [1, 1, 2],
"bar": [D("3.4"), D("3.4"), D("4.5")],
}
)
head_df = df.group_by("foo", maintain_order=True).head(1)
expected_df = pl.DataFrame({"foo": [1, 2], "bar": [D("3.4"), D("4.5")]})
assert_frame_equal(head_df, expected_df)