Skip to content

ENH: np.append should cast appended values to the dtype of the first argument #25640

Open

Description

@ngoldbaum

Proposed new feature or change:

Right now np.append will convert both operands to an array first, then combine the result. This produces suboptimal results for stringdtype and a scalar operand:

Pdb) np.append(stringdtype_array, "hello")
*** ValueError: Cannot find common instance for unequal dtype instances

If instead we cast the second argument to the dtype of the first argument, this would succeed.

Note that this problem is particular to StringDType, but relying on numpy coercing the arguments to built-in dtypes is probably less forward looking than casting to the dtype of the first argument.

Activity

  1. seberg commented on Jan 20, 2024

    @seberg
    Member

    See also gh-22429. While I agree that append shouldn't promote, I am not certain how well we can get away with it.

    I am surprised about not allowing the promotion here, but I guess it is the conservative thing to do (easier to relax).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions