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

check_sorted ignored for DataFrame.rolling when group_by is not given #15225

Closed
2 tasks done
MarcoGorelli opened this issue Mar 22, 2024 · 2 comments · Fixed by #15227
Closed
2 tasks done

check_sorted ignored for DataFrame.rolling when group_by is not given #15225

MarcoGorelli opened this issue Mar 22, 2024 · 2 comments · Fixed by #15227
Labels
A-timeseries Area: date/time functionality bug Something isn't working P-low Priority: low python Related to Python Polars

Comments

@MarcoGorelli
Copy link
Collaborator

MarcoGorelli commented Mar 22, 2024

Checks

  • I have checked that this issue has not already been reported.
  • I have confirmed this bug exists on the latest version of Polars.

Reproducible example

In [15]: df = pl.DataFrame({'a': [1,2,3], 'b': [date(2020, 1, 1), date(2020, 1, 2), date(2020, 1, 3)], 'c': [1, 1, 2]})

In [16]: df.rolling('b', period='2d', check_sorted=False).agg(pl.sum('a'))

Log output

---------------------------------------------------------------------------
InvalidOperationError                     Traceback (most recent call last)
Cell In[16], line 1
----> 1 df.rolling('b', period='2d').agg(pl.sum('a'), check_sorted=False)

File ~/scratch/.venv/lib/python3.11/site-packages/polars/dataframe/group_by.py:881, in RollingGroupBy.agg(self, *aggs, **named_aggs)
    852 def agg(
    853     self,
    854     *aggs: IntoExpr | Iterable[IntoExpr],
    855     **named_aggs: IntoExpr,
    856 ) -> DataFrame:
    857     """
    858     Compute aggregations for each group of a group by operation.
    859
   (...)
    868         The resulting columns will be renamed to the keyword used.
    869     """
    870     return (
    871         self.df.lazy()
    872         .rolling(
    873             index_column=self.time_column,
    874             period=self.period,
    875             offset=self.offset,
    876             closed=self.closed,
    877             by=self.by,
    878             check_sorted=self.check_sorted,
    879         )
    880         .agg(*aggs, **named_aggs)
--> 881         .collect(no_optimization=True)
    882     )

File ~/scratch/.venv/lib/python3.11/site-packages/polars/lazyframe/frame.py:1943, in LazyFrame.collect(self, type_coercion, predicate_pushdown, projection_pushdown, simplify_expression, slice_pushdown, comm_subplan_elim, comm_subexpr_elim, no_optimization, streaming, background, _eager)
   1940 if background:
   1941     return InProcessQuery(ldf.collect_concurrently())
-> 1943 return wrap_df(ldf.collect())

InvalidOperationError: argument in operation 'group_by_rolling' is not explicitly sorted

- If your data is ALREADY sorted, set the sorted flag with: '.set_sorted()'.
- If your data is NOT sorted, sort the 'expr/series/column' first.

Issue description

check_sorted should silence the error message

Expected behavior

shape: (3, 2)
┌────────────┬─────┐
│ b          ┆ a   │
│ ---        ┆ --- │
│ date       ┆ i64 │
╞════════════╪═════╡
│ 2020-01-01 ┆ 1   │
│ 2020-01-02 ┆ 3   │
│ 2020-01-03 ┆ 5   │
└────────────┴─────┘

Installed versions

--------Version info---------
Polars:               0.20.16
Index type:           UInt32
Platform:             Linux-5.15.146.1-microsoft-standard-WSL2-x86_64-with-glibc2.35
Python:               3.11.8 (main, Feb 25 2024, 16:39:33) [GCC 11.4.0]

----Optional dependencies----
adbc_driver_manager:  <not installed>
cloudpickle:          <not installed>
connectorx:           <not installed>
deltalake:            <not installed>
fastexcel:            <not installed>
fsspec:               <not installed>
gevent:               <not installed>
hvplot:               0.9.2
matplotlib:           3.8.3
numpy:                1.26.4
openpyxl:             <not installed>
pandas:               2.0.0
pyarrow:              15.0.1
pydantic:             2.6.1
pyiceberg:            <not installed>
pyxlsb:               <not installed>
sqlalchemy:           <not installed>
xlsx2csv:             <not installed>
xlsxwriter:           <not installed>

@MarcoGorelli MarcoGorelli added bug Something isn't working python Related to Python Polars needs triage Awaiting prioritization by a maintainer A-timeseries Area: date/time functionality P-low Priority: low and removed needs triage Awaiting prioritization by a maintainer labels Mar 22, 2024
@MarcoGorelli
Copy link
Collaborator Author

Related, https://docs.pola.rs/py-polars/html/reference/expressions/api/polars.Expr.rolling.html mentions by in the check_sorted description, even though it takes no such argument

@MarcoGorelli
Copy link
Collaborator Author

Same with group_by_dynamic:

df.group_by_dynamic('b', every='2d', check_sorted=False).agg(pl.sum('a'))

(same error)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-timeseries Area: date/time functionality bug Something isn't working P-low Priority: low python Related to Python Polars
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

1 participant