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

normal python max function doesn't work and the error message is not helpful #14062

Closed
2 tasks done
arnabanimesh opened this issue Jan 29, 2024 · 4 comments
Closed
2 tasks done
Labels
bug Something isn't working invalid A bug report that is not actually a bug python Related to Python Polars

Comments

@arnabanimesh
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

from datetime import datetime
import polars as pl

out_df = pl.read_csv(b"total_time,qty,end_time\n2009,106,2009")
out_df = out_df.select(pl.col("qty"),(pl.lit(datetime(2024,1,1,0,0,0),pl.Datetime)+pl.duration(minutes=max(0,pl.col("end_time").cast(pl.Int64)-pl.col("total_time").cast(pl.Int64)))).dt.strftime("%d-%m-%Y %H:%M").alias("start_time"))
print(out_df)

Using pl.max_horizontal instead of max works. But this should also work in a similar manner.

Log output

file < 128 rows, no statistics determined
no. of chunks: 1 processed by: 1 threads.
Traceback (most recent call last):
  File "D:\coding\postproc.py", line 7, in <module>
    out_df = out_df.select(pl.col("qty"),(pl.lit(datetime(2024,1,1,0,0,0),pl.Datetime)+pl.duration(minutes=max(0,pl.col("end_time").cast(pl.Int64)-pl.col("total_time").cast(pl.Int64)))).dt.strftime("%d-%m-%Y %H:%M").alias("start_time"))
                                                                                                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Arnab\scoop\apps\python311\current\Lib\site-packages\polars\expr\expr.py", line 149, in __bool__
    raise TypeError(msg)
TypeError: the truth value of an Expr is ambiguous

Hint: use '&' or '|' to logically combine Expr, not 'and'/'or', and use `x.is_in([y,z])` instead of `x in [y,z]` to check membership.

Issue description

Error is being generated

Expected behavior

No error, normal output as per code

Installed versions

--------Version info---------
Polars:               0.20.6
Index type:           UInt32
Platform:             Windows-10-10.0.22631-SP0
Python:               3.11.7 (tags/v3.11.7:fa7a6f2, Dec  4 2023, 19:24:49) [MSC v.1937 64 bit (AMD64)]

----Optional dependencies----
adbc_driver_manager:  <not installed>
cloudpickle:          3.0.0
connectorx:           <not installed>
deltalake:            <not installed>
fsspec:               2023.12.2
gevent:               <not installed>
hvplot:               <not installed>
matplotlib:           3.8.2
numpy:                1.26.2
openpyxl:             3.1.2
pandas:               2.1.4
pyarrow:              14.0.2
pydantic:             <not installed>
pyiceberg:            <not installed>
pyxlsb:               1.0.10
sqlalchemy:           <not installed>
xlsx2csv:             0.8.2
xlsxwriter:           3.1.9
@arnabanimesh arnabanimesh added bug Something isn't working needs triage Awaiting prioritization by a maintainer python Related to Python Polars labels Jan 29, 2024
@Wainberg
Copy link
Contributor

Here's a minimal example:

>>> max(pl.col.a, pl.col.b)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "polars/expr/expr.py", line 148, in __bool__
    raise TypeError(msg)
TypeError: the truth value of an Expr is ambiguous

Hint: use '&' or '|' to logically combine Expr, not 'and'/'or', and use `x.is_in([y,z])` instead of `x in [y,z]` to check membership.

I agree that it would be nice to make max an alias for max_horizontal here, and similarly for min, all and any.

@arnabanimesh
Copy link
Contributor Author

I think max is already kind of an alias of max_horizontal as my code outputs properly if you remove the qty column from the select statement in my code. This kind of inconsistent behaviour is unexpected.

@alexander-beedie
Copy link
Collaborator

You can't use the Python max function with a Polars expression ;)
You need to use Polars' own max or max_horizontal here.

max(0, pl.col("end_time"))
# TypeError: the truth value of an Expr is ambiguous
pl.max_horizontal(0, pl.col("end_time"))
# <polars.expr.expr.Expr at 0x29de1fc10>

@alexander-beedie alexander-beedie added invalid A bug report that is not actually a bug and removed needs triage Awaiting prioritization by a maintainer labels Jan 29, 2024
@arnabanimesh
Copy link
Contributor Author

At least change the error message. It is not helpful at all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working invalid A bug report that is not actually a bug python Related to Python Polars
Projects
None yet
Development

No branches or pull requests

3 participants