Skip to content

ENH: Add dtype argument to np.append - #22429

Closed
maidaly wants to merge 5 commits into
numpy:mainfrom
maidaly:main
Closed

maidaly wants to merge 5 commits into
numpy:mainfrom
maidaly:main

Conversation

@maidaly

@maidaly maidaly commented Oct 12, 2022

Copy link
Copy Markdown

This pull request is for solving the #22427 issue. I solved it by adding a new parameter dtype to the append function so it can overwrite the default dtype in the generated copy from the array which has not the same type as the original one .

Comment thread numpy/lib/function_base.py Outdated

@array_function_dispatch(_append_dispatcher)
def append(arr, values, axis=None):
def append(arr, values, dtype=None, axis=None):

@MatteoRaso MatteoRaso Oct 12, 2022 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation for append needs to be updated to explain what the dtype parameter is.

@maidaly maidaly Oct 12, 2022 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MatteoRaso I added the parameter to the function documentation.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dtype must be the last parameter, to not break users who currently wrote: np.append(arr, 1, 0)

@seberg seberg changed the title This pull request is for solving the #22427 issue. ENH: Add dtype argument to np.append Oct 13, 2022

@seberg seberg left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea is good, although I am a bit unsure if we should go down this road, because I do agree with the initial reporter that it is confusing that append uses the "common dtype" and promotes both arrays (like concatenate).

EDIT: Just editing. But it also needs some basic tests.

if arr.ndim != 1:
arr = arr.ravel()
values = ravel(values)
axis = arr.ndim-1

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could delete all of this code I think. concatenate supports axis=None in exactly this way already? I am wondering why it is there, but it may well out-date NumPy itself...

Mainly, casting arr to dtype ahead of time is also unnecessary.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@seberg I delete the checking dtype if None code. I also found it unnecessary and only depended on passing the dtype to the concatenate function.

Comment thread numpy/lib/function_base.py Outdated

@array_function_dispatch(_append_dispatcher)
def append(arr, values, axis=None):
def append(arr, values, dtype=None, axis=None):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dtype must be the last parameter, to not break users who currently wrote: np.append(arr, 1, 0)

@mattip mattip added the 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes label Feb 15, 2023
@mattip

mattip commented Feb 15, 2023

Copy link
Copy Markdown
Member

This needs tests and a release note.

@jorenham

jorenham commented Jan 8, 2026

Copy link
Copy Markdown
Member

This seems to have stranded, so I'm going to close it. We can re-open if you still want to work on this @maidaly

@jorenham jorenham closed this Jan 8, 2026
@github-project-automation github-project-automation Bot moved this from Pending authors' response to Completed in NumPy first-time contributor PRs Jan 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

01 - Enhancement 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes

Projects

Development

Successfully merging this pull request may close these issues.

5 participants