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

Inconsistent behavior between slice() as an expression on a column and slice() on a Series/DataFrame #18553

Open
2 tasks done
JSteilberg opened this issue Sep 4, 2024 · 1 comment
Labels
bug Something isn't working needs decision Awaiting decision by a maintainer needs triage Awaiting prioritization by a maintainer python Related to Python Polars

Comments

@JSteilberg
Copy link

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

>>> pl.DataFrame({"a": [1,2,3,4,5]}).select(pl.col("a").slice(-6))
shape: (5, 1)
┌─────┐
│ a   │
│ --- │
│ i64 │
╞═════╡
│ 1   │
│ 2   │
│ 3   │
│ 4   │
│ 5   │
└─────┘
>>> pl.DataFrame({"a": [1,2,3,4,5]}).slice(-6)
shape: (4, 1)
┌─────┐
│ a   │
│ --- │
│ i64 │
╞═════╡
│ 1   │
│ 2   │
│ 3   │
│ 4   │
└─────┘
>>> pl.Series([1,2,3,4,5]).slice(-6)
shape: (4,)
Series: '' [i64]
[
	1
	2
	3
	4
]

I would highly prefer the first behavior, as it is what Python does for negative index out-of-bounds:

>>> [1,2,3,4,5][-6:]
[1, 2, 3, 4, 5]

Log output

No response

Issue description

Often there is a need to support capping a list to some maximum length. I know there are other ways to achieve what I want, but it seems like the above behavior should be consistent as the docs do not specify any functional difference between slicing on a DataFrame/Series/Expr.

Expected behavior

I would expect all three examples to return [1,2,3,4,5] (in the applicable datastructure), as slicing beyond the beginning of the list in Python tends to clamp to the beginning rather than loop back around to the end.

Installed versions

--------Version info---------
Polars:              1.6.0
Index type:          UInt64
Platform:            Linux-6.8.0-40-generic-x86_64-with-glibc2.35
Python:              3.12.2 | packaged by conda-forge | (main, Feb 16 2024, 20:50:58) [GCC 12.3.0]

----Optional dependencies----
adbc_driver_manager  <not installed>
altair               <not installed>
cloudpickle          <not installed>
connectorx           <not installed>
deltalake            <not installed>
fastexcel            <not installed>
fsspec               2024.6.1
gevent               <not installed>
great_tables         <not installed>
matplotlib           3.8.4
nest_asyncio         <not installed>
numpy                1.26.4
openpyxl             <not installed>
pandas               2.2.2
pyarrow              16.1.0
pydantic             <not installed>
pyiceberg            <not installed>
sqlalchemy           <not installed>
torch                <not installed>
xlsx2csv             <not installed>
xlsxwriter           <not installed>
@JSteilberg JSteilberg added bug Something isn't working needs triage Awaiting prioritization by a maintainer python Related to Python Polars labels Sep 4, 2024
@coastalwhite coastalwhite added the needs decision Awaiting decision by a maintainer label Sep 4, 2024
@lmocsi
Copy link

lmocsi commented Sep 5, 2024

And the expressions pl.DataFrame({"a": [1,2,3,4,5]}).slice(-10) and pl.Series([1,2,3,4,5]).slice(-10) return an empty df / series.
(with slice(-9) they return only the first value: 1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs decision Awaiting decision by a maintainer needs triage Awaiting prioritization by a maintainer python Related to Python Polars
Projects
None yet
Development

No branches or pull requests

3 participants