Skip to content

NUM: inequality with complex dtypes #28050

Closed
@jbrockmendel

Description

@jbrockmendel

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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions