Skip to content

BUG: Float Output Type When Combining uint64 and int64 Arrays in intersect1d #25386

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
ArthurTolley opened this issue Dec 13, 2023 · 3 comments
Closed
Labels

Comments

@ArthurTolley
Copy link

Describe the issue:

When using intersect1d where the input arrays are of differing integer types, for example, int64 and uint64. The dtype of the resulting numpy array is float64. This feels like a bug where the correct dtype for the output should be the most general integer type (probably the signed integer).

Reproduce the code example:

import numpy
a = numpy.arange(100, dtype=numpy.int64)
b = numpy.arange(100, dtype=numpy.uint64)
intersect_aa = numpy.intersect1d(a,a)
intersect_bb = numpy.intersect1d(b,b)
intersect_ab = numpy.intersect1d(a,b)

print(intersect_aa.dtype)
print(intersect_bb.dtype)
print(intersect_ab.dtype)

Error message:

No response

Python and NumPy Versions:

1.26.2
3.12.0 | packaged by Anaconda, Inc. | (main, Oct 2 2023, 12:22:05) [Clang 14.0.6 ]

Runtime Environment:

[{'numpy_version': '1.26.2',
'python': '3.12.0 | packaged by Anaconda, Inc. | (main, Oct 2 2023, '
'12:22:05) [Clang 14.0.6 ]',
'uname': uname_result(system='Darwin', node='Arthurs-MacBook-Air.local', release='23.1.0', version='Darwin Kernel Version 23.1.0: Mon Oct 9 21:28:31 PDT 2023; root:xnu-10002.41.9~6/RELEASE_ARM64_T8112', machine='arm64')},
{'simd_extensions': {'baseline': ['NEON', 'NEON_FP16', 'NEON_VFPV4', 'ASIMD'],
'found': ['ASIMDHP'],
'not_found': ['ASIMDFHM']}},
{'architecture': 'armv8',
'filepath': '/Users/arthurtolley/miniconda3/envs/test_env/lib/libopenblasp-r0.3.21.dylib',
'internal_api': 'openblas',
'num_threads': 8,
'prefix': 'libopenblas',
'threading_layer': 'pthreads',
'user_api': 'blas',
'version': '0.3.21'}]
None

Context for the issue:

This bug was preventing the usage of the output of intersect1d from being used to index an array further along our script.

We have enforced explicit types for the inputs to intersect1d in a recent PR: gwastro/pycbc#4586

@jakevdp
Copy link
Contributor

jakevdp commented Dec 13, 2023

I believe this is working as intended, because the type promotion result for int64 and uint64 is float64:

In [1]: import numpy as np

In [2]: np.result_type('int64', 'uint64')
Out[2]: dtype('float64')

Granted this is surprising behavior. For some discussion of this, you might refer to #5745 and #20905, where this issue is tracked.

@seberg
Copy link
Member

seberg commented Dec 13, 2023

Agreed, closing as duplicate of gh-20905. Granted, comparisons now get it right, but intersection uses sorting, so it's a bit of a weird thing...
It's weird, maybe we should change the behavior at some point, but it is "expected" and discussed in many other places we can add to.

@seberg seberg closed this as not planned Won't fix, can't repro, duplicate, stale Dec 13, 2023
@ArthurTolley
Copy link
Author

We will leave in our explicit type enforcement then, thank you for your quick responses! 😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants