Skip to content

[C++/Python] Kernel for SetItem(IntegerArray, values) ("replace_with_indices") #25505

Open

Description

@asfimport

We should have a kernel that allows overriding the values of an array using an integer array as the indexer and a scalar or array of equal length as the values.

Reporter: Uwe Korn / @xhochy

Related issues:

Note: This issue was originally created as ARROW-9431. Please see the migration documentation for further details.

Activity

  1. asfimport commented on May 17, 2021

    @asfimport
    Author

    Niranda Perera / @nirandaperera:
    Identical issue

  2. asfimport commented on May 18, 2021

    @asfimport
    Author

    Joris Van den Bossche / @jorisvandenbossche:
    @nirandaperera you linked ARROW-9430 as duplicate. Both are of course closely related, but there is a difference: using integer indices vs a boolean mask to select the values you want to set

  3. asfimport commented on May 18, 2021

    @asfimport
    Author

    Niranda Perera / @nirandaperera:
    Thank you @jorisvandenbossche. That was clearly my mistake :(

  4. asfimport commented on May 18, 2021

    @asfimport
    Author

    Niranda Perera / @nirandaperera:
    I'd like to take this one. @jorisvandenbossche  @xhochy @pitrou I'd like to get your feedback on the following.

    Just to be clear, an example of this would be,

     

    // code placeholder
    arr1 = [a, b, c, d]
    arr2 = [l, m, n, o, p]
    
    to_replace = [0, 2, 3]
    
    res = replace_by_index(to_replace, arr1, arr2)
    
    # res --> [l, b, n, o]

     

    What is a good name for the kernel? I'm not sure about replace_by_index because Arrays are immutable. Any suggestions?

    Can we assume that 'to_replace' is a non-null Int64Array?

    And should we enforce 'to_replace' being sorted (IMO this would be more efficient)?

    What is the arity of this kernel? Unary or binary?

     

  5. asfimport commented on May 18, 2021

    @asfimport
    Author

    Joris Van den Bossche / @jorisvandenbossche:
    I think one difference in your example is that I expect the arr2 to be of the same length as to_replace

    Can we assume that 'to_replace' is a non-null Int64Array?

    That might be the easiest for now. Or otherwise we can say nulls in to_replace give nulls in the output. The question is then how to interpret arr2, though (should it be of the same length as to_replace, or as the non-null values of to_replace ?)

    And should we enforce 'to_replace' being sorted (IMO this would be more efficient)?

    Hmm, not sure. Also for Take we generally don't require sortedness of the indices, I think?

    What is the arity of this kernel? Unary or binary?

    Ternary?

  6. asfimport commented on May 26, 2021

    @asfimport
    Author

    Antoine Pitrou / @pitrou:
    Indeed, we don't require sortedness of the indices for Take. Also, I'm not convinced it would be more efficient (why?).

  7. asfimport commented on May 26, 2021

    @asfimport
    Author

    Antoine Pitrou / @pitrou:
    The example doesn't quite look right to me. I would expect:

    arr1 = [a, b, c, d]
    arr2 = [l, m, n, o, p]
    
    to_replace = [0, 2, 3]
    
    res = replace_by_index(to_replace, arr1, arr2)
    
    # res --> [l, b, m, n]

    @xhochy What do you think?

  8. asfimport commented on May 26, 2021

    @asfimport
    Author

    Niranda Perera / @nirandaperera:
    @pitrou my idea was something like this.

    def replace_by_index(to_replace: Array, arr1: Array, arr2: Array) -> Array:
      out = arr1 # copy array
      for i in to_replace:
        out[i] = arr2[i]
      return out 

     

    Its like an if-else mask, but the mask's set bits are encoded in an array ARROW-10640

  9. asfimport commented on May 26, 2021

    @asfimport
    Author

    Antoine Pitrou / @pitrou:
    If this is meant to emulate Numpy's advanced indexing, then I don't think this is the right semantics.

  10. asfimport commented on May 26, 2021

    @asfimport
    Author

    Niranda Perera / @nirandaperera:
    I see.. Can you give an example?

  11. asfimport commented on May 26, 2021

    @asfimport
    Author

    Antoine Pitrou / @pitrou:

    >>> a = np.array(["a", "b", "c", "d"])
    >>> b = np.array(["m", "n", "o"])
    >>> a[[0,2,3]] = b
    >>> a
    array(['m', 'b', 'n', 'o'], dtype='<U1')
  12. asfimport commented on May 26, 2021

    @asfimport
    Author

    Antoine Pitrou / @pitrou:
    @xhochy What is the intended use case?

  13. asfimport commented on May 26, 2021

    @asfimport
    Author

    Niranda Perera / @nirandaperera:
    Got it. Think that makes sense. So, this will ultimately be another ternary kernel, isnt it?

  14. asfimport commented on May 26, 2021

    @asfimport
    Author

    Antoine Pitrou / @pitrou:
    What do you mean by "another ternary kernel"?

  15. asfimport commented on May 26, 2021

    @asfimport
    Author

    Niranda Perera / @nirandaperera:
    I'm currently working on the 'if-else' function ARROW-10640. And AFAIK it is the only ternary function that is there ATM. This will be another one.

  16. 4 remaining items

  17. asfimport commented on Jun 1, 2021

    @asfimport
    Author

    Joris Van den Bossche / @jorisvandenbossche:
    Yes, I think that covers all cases. I am personally not fully sure that the third case of a scalar for the indices is necessarily important to cover for an initial implementation (for example also the "take" kernel only supports an actual array for the indices)

  18. asfimport commented on Jun 1, 2021

    @asfimport
    Author
  19. asfimport commented on Nov 16, 2021

    @asfimport
    Author

    Niranda Perera / @nirandaperera:
    I'd like to start looking at this Jira again. @jorisvandenbossche  Has there been any developments for the last couple of months (affecting this requirement) or can I still go ahead? 

  20. asfimport commented on Nov 17, 2021

    @asfimport
    Author

    Joris Van den Bossche / @jorisvandenbossche:
    I am not aware of any work that happened in the meantime. So taking a look at this issue would be very welcome!

    The related ARROW-9430 has been done by now, and there we went with a name replace_with_mask for the kernel to set values with a mask. So maybe for this kernel we can then use replace_with_indices to follow the same naming scheme?

  21. asfimport commented on Jul 12, 2022

    @asfimport
    Author

    Todd Farmer / @toddfarmer:
    This issue was last updated over 90 days ago, which may be an indication it is no longer being actively worked. To better reflect the current state, the issue is being unassigned. Please feel free to re-take assignment of the issue if it is being actively worked, or if you plan to start that work soon.

  22. changed the title [-][C++/Python] Kernel for SetItem(IntegerArray, values)[/-] [+][C++/Python] Kernel for SetItem(IntegerArray, values) ("replace_with_indices")[/+] on Mar 21, 2023
  23. thisisnic commented on Jun 20, 2025

    @thisisnic
    Member

    This issue hasn't had activity in a long time. If it's still being worked on, please leave a comment. Otherwise, it will be closed on 23rd June.

    Labelled Status: Stale-Warning for tracking.

  24. added
    Status: stale-warning
    on Jun 20, 2025
  25. added
    Status: needs champion
    and removed
    Status: stale-warning
    on Dec 18, 2025
  26. Kanishk-Dendukuri commented on Jul 31, 2026

    @Kanishk-Dendukuri

    Hi! I'd like to work on this. I'm still fairly new to Arrow but I'll put in the effort to see this through. My understanding, correct me if I'm wrong: I'd model it on the existing replace_with_mask, so it'd be a ternary kernel replace_with_indices(values, indices, replacements) that returns an array the same length as values, where replacements is either an array (length = len(indices)) or a scalar.

    And a few semantics I'd want to confirm: should I assume indices can be unsorted, contain duplicates (last write wins), be null (skipping the index and replacement pair), and out of bounds (skip or error?)?

  27. added a commit that references this issue on Aug 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Component: C++Component: PythonStatus: needs championType: enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions