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

Addition of np.ndarray + pl.Series #6812

Closed
2 tasks done
s-banach opened this issue Feb 11, 2023 · 2 comments · Fixed by #6983
Closed
2 tasks done

Addition of np.ndarray + pl.Series #6812

s-banach opened this issue Feb 11, 2023 · 2 comments · Fixed by #6983
Assignees
Labels
bug Something isn't working python Related to Python Polars

Comments

@s-banach
Copy link
Contributor

s-banach commented Feb 11, 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

I guess this is kind of a stupid issue, because why would you ever mix numpy arrays with polars Series? But I guess if it’s allowed, then it should be done right..

I give a few examples of strange behavior in the code below.

Example 1:
Pylance reports that result_1 will have type NDArray[bool_], but it is actually a Series.

Example 2:
Dtype depends on order of summands.

Example 3:
In this example, the array has length > 1.
Now array + Series works, but Series + array raises ValueError.

Reproducible example

import numpy as np
import polars as pl

# Example 1
sx = pl.Series(values=[1, 1])
y = np.array([1.0])
result_1 = y + sx
print("Example 1:", type(result_1))

# Example 2
sx = pl.Series(values=[1, 1])
y = np.array([1.0])
left = sx + y
right = y + sx
print("Example 2:", left.dtype, right.dtype)

# Example 3
sx = pl.Series(values=[1, 1])
y = np.array([1, 1])
result_3 = y + sx
print("Example 3: sx + y works")
try:
    result_3 = sx + y
except ValueError as e:
    print("Example 3: sx + y gives this error.", e)

"""
Example 1: <class 'polars.internals.series.series.Series'>
Example 2: Int64 Float64
Example 3: sx + y works
Example 3: sx + y gives this error. Cannot convert Python type <class 'numpy.ndarray'> to Int64
"""

Expected behavior

I'm not 100% confident about the intended behavior, but this is what I think.

Example 1:
Ideally, the type checker would know the result is a Series.

Example 2:
The result should have dtype Float64 in either case.

Example 3:
If array + Series is allowed, then Series + array should be allowed.

Installed versions

---Version info---
Polars: 0.16.4
Index type: UInt32
Platform: Windows-10-10.0.19044-SP0
Python: 3.10.8 | packaged by conda-forge | (main, Nov 24 2022, 14:07:00) [MSC v.1916 64 bit (AMD64)]
---Optional dependencies---
pyarrow: 8.0.0
pandas: 1.5.2
numpy: 1.22.3
fsspec: <not installed>
connectorx: <not installed>
xlsx2csv: <not installed>
deltalake: <not installed>
matplotlib: <not installed>
@s-banach s-banach added bug Something isn't working python Related to Python Polars labels Feb 11, 2023
@ritchie46
Copy link
Member

@zundertj could you take a look at this one? It is all numpy on the pythgon side a I believe.

@zundertj
Copy link
Collaborator

The typing issue in example 1 we cannot do anything about, as it is the typing of the Numpy array, see for example numpy/numpy#22064 .

For examples 2 & 3, what I have done in #6983 is to cast the Numpy array to a pl.Series for the common arithmetic operations. As you said, these operations are not commonly done, so expect some rough edges. But do keep reporting those, happy to cover more if it is reasonable.

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.

3 participants