Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

DOC: Clarify (potentially misleading) nbytes docstring #28943

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
Loading
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
DOC: Clarify nbytes docstring
  • Loading branch information
zvun committed May 11, 2025
commit 114a38feb3277a7fd89986e2d3a12b46ad91df88
17 changes: 16 additions & 1 deletion 17 numpy/_core/_add_newdocs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2676,10 +2676,14 @@

add_newdoc('numpy._core.multiarray', 'ndarray', ('nbytes',
"""
Total bytes consumed by the elements of the array.
Total number of bytes the array's data would consume if stored
contiguously in memory.

Notes
-----
If the array is a view, this shows how much memory it *would* use
if it were copied into a separate array.

Does not include memory consumed by non-element attributes of the
array object.
Copy link
Member

Choose a reason for hiding this comment

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

Maybe we ca add that it also doesn't include memory indirectly held by the elements.
(I.e. if you store Python objects or the new StringDType)


Expand All @@ -2698,6 +2702,17 @@
>>> np.prod(x.shape) * x.itemsize
480

>>> import numpy as np
Copy link
Member

Choose a reason for hiding this comment

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

No need I think, but a sentence on why the next thing comes would help.

>>> arr = np.arange(100).reshape(10, 10, 1)
>>> arr_1 = np.broadcast_to(arr, (10, 10, 1000))
>>> arr_2 = np.lib.stride_tricks.sliding_window_view(arr, 5, 0)
>>> arr.nbytes
800
>>> arr_1.nbytes
800000
>>> arr_2.nbytes
2400
Copy link
Member

Choose a reason for hiding this comment

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

This introduces way too much complexity for very little gain. If anything at all, just do some slicing like arr[::2] or so.


"""))


Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.