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

Add pl.lit(list) #6608

Closed
mcrumiller opened this issue Feb 1, 2023 · 4 comments · Fixed by #7879
Closed

Add pl.lit(list) #6608

mcrumiller opened this issue Feb 1, 2023 · 4 comments · Fixed by #7879
Labels
enhancement New feature or an improvement of an existing feature

Comments

@mcrumiller
Copy link
Contributor

Problem description

Stems from #6596. Literal lists would be very useful for series with dtype pl.List. Example:

import polars as pl

lit = pl.lit([1, 1])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\430015439\AppData\Local\Programs\Python\Python39\lib\site-packages\polars\internals\lazy_functions.py", line 1176, in lit
    return pli.wrap_expr(pylit(item, allow_object))
ValueError: could not convert value '[1, 1]' as a Literal

In a more useful context, it would be inferred, e.g:

df = pl.DataFrame({
    'a': [
        [1, 1],
        [2, 2]
    ]
})
df.filter(pl.col('a') == [1, 1])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\430015439\AppData\Local\Programs\Python\Python39\lib\site-packages\polars\internals\expr\expr.py", line 251, in __eq__
    return wrap_expr(self._pyexpr.eq(self._to_expr(other)._pyexpr))
  File "C:\Users\430015439\AppData\Local\Programs\Python\Python39\lib\site-packages\polars\internals\expr\expr.py", line 163, in _to_expr
    return pli.lit(other)
  File "C:\Users\430015439\AppData\Local\Programs\Python\Python39\lib\site-packages\polars\internals\lazy_functions.py", line 1176, in lit
    return pli.wrap_expr(pylit(item, allow_object))
@mcrumiller mcrumiller added the enhancement New feature or an improvement of an existing feature label Feb 1, 2023
@deanm0000
Copy link
Collaborator

The problem is that there aren't literal lists in dataframes

For instance, taking your example type(df[0,0]) is a polars.internals.series.series.Series not a list.

@mcrumiller
Copy link
Contributor Author

@deanm0000 that makes sense, but since a pl.lit(pl.Series) is possible, surely this could be hidden from the user and work as intended?

@deanm0000
Copy link
Collaborator

touche.

So it seems what you're after is being able to do

df.with_columns(pl.Series([1,1]).alias('c')).filter(pl.col('a')==pl.col('c')) 

but without the, seemingly, superfluous with_columns
makes sense.

@MarcoGorelli
Copy link
Collaborator

I'd have thought concat_list would work here, but it looks like it returns both rows 🤔

In [1]: df = pl.DataFrame({
   ...:     'a': [
   ...:         [1, 1],
   ...:         [2, 2]
   ...:     ]
   ...: })
   ...: df.filter(pl.col('a') == pl.concat_list([1, 1]))
Out[1]:
shape: (2, 1)
┌───────────┐
│ a         │
│ ---       │
│ list[i64] │
╞═══════════╡
│ [1, 1]    │
│ [2, 2]    │
└───────────┘

In [2]: df = pl.DataFrame({
   ...:     'a': [
   ...:         [1, 1],
   ...:         [2, 2]
   ...:     ]
   ...: })
   ...: df.filter(pl.col('a') == pl.concat_list([1, 9]))
Out[2]:
shape: (0, 1)
┌───────────┐
│ a         │
│ ---       │
│ list[i64] │
╞═══════════╡
└───────────┘

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or an improvement of an existing feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants