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 error type when doing arithmetics on a non-numeric series #6617

Closed
2 tasks done
abalkin opened this issue Feb 1, 2023 · 1 comment · Fixed by #6677
Closed
2 tasks done

Inconsistent error type when doing arithmetics on a non-numeric series #6617

abalkin opened this issue Feb 1, 2023 · 1 comment · Fixed by #6677
Labels
bug Something isn't working python Related to Python Polars

Comments

@abalkin
Copy link
Contributor

abalkin commented Feb 1, 2023

Polars version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of Polars.

Issue description

The type of the error raised when adding a number to a non-numeric series depends on the dtype of the series:

Reproducible example

>>> from datetime import date
>>> import polars as pl
>>> pl.Series("x", [date.today()]) + 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/a/Work/abalkin/polars/py-polars/polars/internals/series/series.py", line 534, in __add__
    return self._arithmetic(other, "add", "add_<>")
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/a/Work/abalkin/polars/py-polars/polars/internals/series/series.py", line 512, in _arithmetic
    other = maybe_cast(other, self.dtype, self.time_unit)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/a/Work/abalkin/polars/py-polars/polars/datatypes.py", line 802, in maybe_cast
    el = py_type(el)  # type: ignore[call-arg]
         ^^^^^^^^^^^
TypeError: function missing required argument 'month' (pos 2)
>>> pl.Series("x", [""]) + 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/a/Work/abalkin/polars/py-polars/polars/internals/series/series.py", line 534, in __add__
    return self._arithmetic(other, "add", "add_<>")
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/a/Work/abalkin/polars/py-polars/polars/internals/series/series.py", line 515, in _arithmetic
    raise ValueError(
ValueError: cannot do arithmetic with series of dtype: Utf8 and argument of type: <class 'str'>

Expected behavior

Expect the same error type in both cases. Probably ValueError error is correct because from Python perspective, dtype is part of the value, not type.

Installed versions

>>> pl.show_versions()
---Version info---
Polars: 0.16.1
Index type: UInt32
Platform: macOS-12.6.3-x86_64-i386-64bit
Python: 3.11.1 (main, Dec 23 2022, 09:40:27) [Clang 14.0.0 (clang-1400.0.29.202)]
---Optional dependencies---
pyarrow: 11.0.0
pandas: 1.5.3
numpy: 1.24.1
fsspec: <not installed>
connectorx: <not installed>
xlsx2csv: 0.8.1
deltalake: <version not detected>
matplotlib: <not installed>
@abalkin abalkin added bug Something isn't working python Related to Python Polars labels Feb 1, 2023
@zundertj
Copy link
Collaborator

zundertj commented Feb 4, 2023

Changed this to ValueError in #6677. The message will not be exactly the same as the other example you give, given that this is a different code path.

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

Successfully merging a pull request may close this issue.

2 participants