From 114a38feb3277a7fd89986e2d3a12b46ad91df88 Mon Sep 17 00:00:00 2001 From: zvun <41363258+zvun@users.noreply.github.com> Date: Sun, 11 May 2025 21:41:50 +0000 Subject: [PATCH 1/5] DOC: Clarify nbytes docstring --- numpy/_core/_add_newdocs.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/numpy/_core/_add_newdocs.py b/numpy/_core/_add_newdocs.py index 9f452931fcf1..b16faeb02005 100644 --- a/numpy/_core/_add_newdocs.py +++ b/numpy/_core/_add_newdocs.py @@ -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. @@ -2698,6 +2702,17 @@ >>> np.prod(x.shape) * x.itemsize 480 + >>> import numpy as np + >>> 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 + """)) From 3ffc6b51eb2c1e62675fc9e934299247c31d6707 Mon Sep 17 00:00:00 2001 From: zvun <41363258+zvun@users.noreply.github.com> Date: Mon, 12 May 2025 06:23:45 +0000 Subject: [PATCH 2/5] Remove whitespace --- numpy/_core/_add_newdocs.py | 1 - 1 file changed, 1 deletion(-) diff --git a/numpy/_core/_add_newdocs.py b/numpy/_core/_add_newdocs.py index b16faeb02005..564c1adbb3e5 100644 --- a/numpy/_core/_add_newdocs.py +++ b/numpy/_core/_add_newdocs.py @@ -2683,7 +2683,6 @@ ----- 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. From f828aa7faa49955534340a2fb64cc8bcca8504f3 Mon Sep 17 00:00:00 2001 From: zvun <41363258+zvun@users.noreply.github.com> Date: Mon, 12 May 2025 16:17:47 +0000 Subject: [PATCH 3/5] Apply PR suggestions --- numpy/_core/_add_newdocs.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/numpy/_core/_add_newdocs.py b/numpy/_core/_add_newdocs.py index 564c1adbb3e5..a154324340da 100644 --- a/numpy/_core/_add_newdocs.py +++ b/numpy/_core/_add_newdocs.py @@ -2683,8 +2683,10 @@ ----- 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. + The number of bytes does not include: + - Memory consumed by non-element attributes or the array object. + - Memory indirectly held by the elements, e.g. in arrays storing + python objects or `StringDType`. See Also -------- @@ -2701,16 +2703,13 @@ >>> np.prod(x.shape) * x.itemsize 480 - >>> import numpy as np - >>> 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) + Although an array and its view share the same data buffer, their `nbytes` are different: + >>> arr = np.arange(10) + >>> view = arr[::2] >>> arr.nbytes - 800 - >>> arr_1.nbytes - 800000 - >>> arr_2.nbytes - 2400 + 80 + >>> view.nbytes + 40 """)) From 7ebe43b18368b35aba4c5f1060a1b0c570b188f7 Mon Sep 17 00:00:00 2001 From: zvun <41363258+zvun@users.noreply.github.com> Date: Mon, 12 May 2025 16:20:29 +0000 Subject: [PATCH 4/5] Typo --- numpy/_core/_add_newdocs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/numpy/_core/_add_newdocs.py b/numpy/_core/_add_newdocs.py index a154324340da..ce0488527fc4 100644 --- a/numpy/_core/_add_newdocs.py +++ b/numpy/_core/_add_newdocs.py @@ -2684,7 +2684,7 @@ If the array is a view, this shows how much memory it *would* use if it were copied into a separate array. The number of bytes does not include: - - Memory consumed by non-element attributes or the array object. + - Memory consumed by non-element attributes of the array object. - Memory indirectly held by the elements, e.g. in arrays storing python objects or `StringDType`. From 06b23daeb4e3ad3455dc25966bd68404a28cd2b2 Mon Sep 17 00:00:00 2001 From: zvun <41363258+zvun@users.noreply.github.com> Date: Mon, 12 May 2025 16:54:19 +0000 Subject: [PATCH 5/5] Rephrase --- numpy/_core/_add_newdocs.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/numpy/_core/_add_newdocs.py b/numpy/_core/_add_newdocs.py index ce0488527fc4..ab7d94a4be9c 100644 --- a/numpy/_core/_add_newdocs.py +++ b/numpy/_core/_add_newdocs.py @@ -2683,10 +2683,8 @@ ----- If the array is a view, this shows how much memory it *would* use if it were copied into a separate array. - The number of bytes does not include: - - Memory consumed by non-element attributes of the array object. - - Memory indirectly held by the elements, e.g. in arrays storing - python objects or `StringDType`. + The number of bytes does not include overhead from non-element attributes and memory + indirectly held by elements such as Python objects or `StringDType`. See Also --------