Skip to content

Commit

Permalink
fix(rust, python): fix logical type of nested take (#5271)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Oct 20, 2022
1 parent 235b4bb commit 698adf4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
3 changes: 2 additions & 1 deletion polars/polars-core/src/chunked_array/kernels/take.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ pub(crate) unsafe fn take_list_unchecked(
} else {
None
};
let dtype = ListArray::<i64>::default_datatype(taken.data_type().clone());
// Safety:
// offsets are monotonically increasing
ListArray::new_unchecked(values.data_type().clone(), offsets.into(), taken, validity)
ListArray::new_unchecked(dtype, offsets.into(), taken, validity)
}
30 changes: 30 additions & 0 deletions py-polars/tests/unit/test_datelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -1723,3 +1723,33 @@ def test_timezone_aware_date_range() -> None:
"Given: 'UTC', got: 'Asia/Shanghai'.",
):
pl.date_range(low, high, interval=timedelta(days=5), time_zone="UTC")


def test_logical_nested_take() -> None:
frame = pl.DataFrame(
{
"ix": [2, 1],
"dt": [[datetime(2001, 1, 1)], [datetime(2001, 1, 2)]],
"d": [[date(2001, 1, 1)], [date(2001, 1, 1)]],
"t": [[time(10)], [time(10)]],
"del": [[timedelta(10)], [timedelta(10)]],
"str": [[{"a": time(10)}], [{"a": time(10)}]],
}
)
out = frame.sort(by="ix")

assert out.dtypes[:-1] == [
pl.Int64,
pl.List(pl.Datetime("us")),
pl.List(pl.Date),
pl.List(pl.Time),
pl.List(pl.Duration("us")),
]
assert out.to_dict(False) == {
"ix": [1, 2],
"dt": [[datetime(2001, 1, 2, 0, 0)], [datetime(2001, 1, 1, 0, 0)]],
"d": [[date(2001, 1, 1)], [date(2001, 1, 1)]],
"t": [[time(10, 0)], [time(10, 0)]],
"del": [[timedelta(days=10)], [timedelta(days=10)]],
"str": [[{"a": time(10, 0)}], [{"a": time(10, 0)}]],
}

0 comments on commit 698adf4

Please sign in to comment.