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

next() on GroupBy raises AttributeError object has no attribute _current_index #12868

Open
2 tasks done
cmdlineluser opened this issue Dec 2, 2023 · 5 comments · May be fixed by #16499
Open
2 tasks done

next() on GroupBy raises AttributeError object has no attribute _current_index #12868

cmdlineluser opened this issue Dec 2, 2023 · 5 comments · May be fixed by #16499
Labels
bug Something isn't working good first issue Good for newcomers P-low Priority: low python Related to Python Polars

Comments

@cmdlineluser
Copy link
Contributor

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

import polars as pl

next(pl.DataFrame().group_by(1))
# AttributeError: 'GroupBy' object has no attribute '_current_index'

Log output

No response

Issue description

Not sure if next() is intended to work or not, it seems like it should raise a TypeError instead if it isn't.

Expected behavior

"Work" or return a TypeError?

next([])
# TypeError: 'list' object is not an iterator

Installed versions

--------Version info---------
Polars:               0.19.19
Index type:           UInt32
Platform:             macOS-13.6.1-arm64-arm-64bit
Python:               3.11.6 (main, Nov  2 2023, 04:39:40) [Clang 14.0.0 (clang-1400.0.29.202)]

----Optional dependencies----
adbc_driver_manager:  <not installed>
cloudpickle:          <not installed>
connectorx:           <not installed>
deltalake:            <not installed>
fsspec:               2023.6.0
gevent:               <not installed>
matplotlib:           <not installed>
numpy:                1.26.2
openpyxl:             <not installed>
pandas:               2.0.3
pyarrow:              12.0.1
pydantic:             <not installed>
pyiceberg:            <not installed>
pyxlsb:               <not installed>
sqlalchemy:           <not installed>
xlsx2csv:             <not installed>
xlsxwriter:           <not installed>
@cmdlineluser cmdlineluser added bug Something isn't working python Related to Python Polars labels Dec 2, 2023
@deanm0000 deanm0000 added the good first issue Good for newcomers label Jan 10, 2024
@deanm0000
Copy link
Collaborator

@cmdlineluser
Copy link
Contributor Author

Yeah, the machinery is there I think.

_current_index is created inside __iter__

self._current_index = 0

So it works if you manually call iter()

df = pl.DataFrame({"a": [1, 1, 2], "b": [3, 4, 5]})

next(iter(df.group_by("a")))
# (1,
#  shape: (2, 2)
#  ┌─────┬─────┐
#  │ a   ┆ b   │
#  │ --- ┆ --- │
#  │ i64 ┆ i64 │
#  ╞═════╪═════╡
#  │ 1   ┆ 3   │
#  │ 1   ┆ 4   │
#  └─────┴─────┘)

@deanm0000
Copy link
Collaborator

I think it needs to be in __init__ or else if in __next__ it needs to see if it exists and if not create it

@stinodego stinodego added the needs triage Awaiting prioritization by a maintainer label Jan 13, 2024
@s-bidowaniec
Copy link

I can try to fix it

@stinodego
Copy link
Member

This should raise a TypeError.

@stinodego stinodego added P-low Priority: low and removed needs triage Awaiting prioritization by a maintainer labels May 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers P-low Priority: low python Related to Python Polars
Projects
Status: Ready
Development

Successfully merging a pull request may close this issue.

4 participants