Skip to content

NUM: inequality with complex dtypes #28050

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

Closed
jbrockmendel opened this issue Aug 21, 2019 · 2 comments · Fixed by #43159
Closed

NUM: inequality with complex dtypes #28050

jbrockmendel opened this issue Aug 21, 2019 · 2 comments · Fixed by #43159
Assignees
Labels
Complex Complex Numbers good first issue Needs Tests Unit test(s) needed to prevent regressions Numeric Operations Arithmetic, Comparison, and Logical operations

Comments

@jbrockmendel
Copy link
Member

TL;DR: our comparisons with complex dtypes are not internally consistent:

arr = np.arange(5).astype(np.complex128)
ser = pd.Series(arr)
df = ser.to_frame()

>>> df < df.astype(object)
[...]
TypeError: '<' not supported between instances of 'complex' and 'complex'

>>> df.lt(df.astype(object))
       0
0  False
1  False
2  False
3  False
4  False

Digging into this...

numpy has some surprising behavior for comparing complex dtypes:

arr = np.arange(5)
arr = arr + arr*1j
arr2 = arr*2 - 1

>>> arr < arr2
array([False,  True,  True,  True,  True])

I would expect these inequalities to be undefined, and in fact, if we cast to object we get a TypeError:

>>> arr < arr2.astype(object)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: '<' not supported between instances of 'complex' and 'complex'

That's the behavior I expected in the first place, but then trying it on just the scalars:

>>> arr[0] < arr2[0]
False
@mroeschke
Copy link
Member

Looks like there's some consistency now. Could use a test if there isn't one

In [28]: arr = np.arange(5).astype(np.complex128)
    ...: ser = pd.Series(arr)
    ...: df = ser.to_frame()

In [29]: df < df.astype(object)
TypeError: '<' not supported between instances of 'complex' and 'complex'

In [30]: df.lt(df.astype(object))
TypeError: '<' not supported between instances of 'complex' and 'complex'

@mroeschke mroeschke added good first issue Needs Tests Unit test(s) needed to prevent regressions and removed Bug labels Jul 11, 2021
@calvh
Copy link
Contributor

calvh commented Aug 4, 2021

take

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Complex Complex Numbers good first issue Needs Tests Unit test(s) needed to prevent regressions Numeric Operations Arithmetic, Comparison, and Logical operations
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants