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

ak.argmax and ak.argmin: support for Boolean arrays #2934

Closed
delannoy opened this issue Jan 11, 2024 · 2 comments · Fixed by #2935
Closed

ak.argmax and ak.argmin: support for Boolean arrays #2934

delannoy opened this issue Jan 11, 2024 · 2 comments · Fixed by #2935
Assignees
Labels
feature New feature or request

Comments

@delannoy
Copy link

delannoy commented Jan 11, 2024

Description of new feature

ak.argmax and ak.argmin do not support Boolean arrays unless they are first cast to integer. Meanwhile, np.argmax and np.argmin can operate on Boolean arrays directly:

import awkward as ak
import numpy as np

assert ak.__version__ == '2.5.1'
assert np.__version__ == '1.24.4'

np_bool_array = np.array([[1, 2, 3], [3, 2, 1]]) > 2
assert np.argmax(np_bool_array, axis=1).tolist() == [2, 0]
assert np.argmin(np_bool_array, axis=1).tolist() == [0, 1]

ak_bool_array = ak.Array([[1, 2, 3], [4, 3, 2, 1]]) > 2

try:
    ak.argmax(ak_bool_array, axis=1)
except KeyError:
    print("this doesn't work :(")

try:
    ak.argmax(np_bool_array, axis=1)
except KeyError:
    print("this doesn't work either :(")

# casting to `int` works:
ak_int_array = ak.values_astype(ak_bool_array, int)
assert ak.argmax(ak_int_array, axis=1).tolist() == [2, 0]
assert ak.argmin(ak_int_array, axis=1).tolist() == [0, 2]
@delannoy delannoy added the feature New feature or request label Jan 11, 2024
@ianna
Copy link
Collaborator

ianna commented Jan 11, 2024

@delannoy - thanks for brining it up! I think, we do want to be as close as possible to Numpy. Python, from version 2.3 forward, has a bool type which is a subclass of int, the standard integer type. It has two possible values: True and False, which are special versions of 1 and 0 respectively and behave as such in arithmetic contexts.

@agoose77 agoose77 self-assigned this Jan 11, 2024
@agoose77
Copy link
Collaborator

Agreed with @ianna, we should support this. I'll make a PR! Thanks for the report :)

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

Successfully merging a pull request may close this issue.

3 participants