[X] I have checked that this issue has not already been reported.
[X] I have confirmed this bug exists on the latest version of Polars.
Reproducible example
from datetime import datetime
import polars as pl
df = pl.DataFrame(
{
"time": [
datetime(2021, 2, 1),
datetime(2021, 1, 1), # this is 2021-04-01 in the docs, i.e. sorted
datetime(2021, 5, 1),
datetime(2021, 6, 1),
],
"groups": ["A", "B", "A", "B"],
"values": [0, 1, 2, 3],
}
)
df_upsampled = df.upsample(
time_column="time", every="1mo", group_by="groups", maintain_order=True
)
Log output
---------------------------------------------------------------------------
InvalidOperationError Traceback (most recent call last)
/var/folders/1c/6_s1_dhd2xngnxyrz3vnpqfr0000gq/T/ipykernel_51814/920196386.py in ?()
15 }
16 )#.sort("time")
17
18
---> 19 df_upsampled = df.upsample(
20 time_column="time", every="1mo", group_by="groups", maintain_order=True
21 )
22
~/Dev/performance_management_chart/.venv/lib/python3.11/site-packages/polars/_utils/deprecation.py in ?(*args, **kwargs)
87 def wrapper(*args: P.args, **kwargs: P.kwargs) -> T:
88 _rename_keyword_argument(
89 old_name, new_name, kwargs, function.__qualname__, version
90 )
---> 91 return function(*args, **kwargs)
~/Dev/performance_management_chart/.venv/lib/python3.11/site-packages/polars/dataframe/frame.py in ?(self, time_column, every, group_by, maintain_order)
6419
6420 every = parse_as_duration_string(every)
6421
6422 return self._from_pydf(
-> 6423 self._df.upsample(group_by, time_column, every, maintain_order)
6424 )
InvalidOperationError: argument in operation 'upsample' is not sorted, please sort the 'expr/series/column' first
Issue description
I'm not sure if this is a bug or desired behavior, but it was somewhat unintuitive behavior.
I would like to upsample my data, but group_by some other variable (groups).
My assumption was that if I do sort('groups', 'date') and then upsample(...., group_by='groups') that this counts as sorted because it is sorted within each group.
Checks
Reproducible example
Log output
Issue description
I'm not sure if this is a bug or desired behavior, but it was somewhat unintuitive behavior. I would like to
upsample
my data, butgroup_by
some other variable (groups
). My assumption was that if I dosort('groups', 'date')
and thenupsample(...., group_by='groups')
that this counts as sorted because it is sorted within each group.Quoting from the upsample doc:
So I would assume that this should work similarly for the input. In the MWE, although the
time
column isn't sorted, it is sorted within a group.Expected behavior
I would think that something like this should always work
Installed versions