DataFrame.to_numpy builds a NumPy array that torch.from_numpy can share
Answer: Use DataFrame.to_numpy to convert a DataFrame to a NumPy array. torch.from_numpy creates a Tensor from a numpy.ndarray. That tensor can share memory with the array.
From a DataFrame to NumPy
DataFrame.to_numpy converts the DataFrame to a NumPy array. The default dtype is the common NumPy dtype of all types in the DataFrame. Copying data and coercing values may be expensive. The copy parameter defaults to False, but copy=False does not ensure a no-copy result. Rather, copy=True ensure that a copy is made. The return value is a numpy.ndarray. Series.to_numpy is a similar method for Series.
From a NumPy array to a tensor
torch.from_numpy creates a Tensor from a numpy.ndarray. The tensor and the ndarray share memory. A write to the tensor shows up in the array. numpy.float64, numpy.float32, and bool are accepted dtypes. The returned tensor is not resizable. Writing to a tensor created from a read-only NumPy array is unsupported.
See also
Claims
- id: C1 claim: DataFrame.to_numpy converts a DataFrame to a NumPy array. source: S1 evidence: "Convert the DataFrame to a NumPy array."
- id: C2 claim: copy=True forces a copy of the array. source: S1 evidence: "copy=True ensure that a copy is made"
- id: C3 claim: torch.from_numpy creates a tensor from a NumPy ndarray. source: S2 evidence: "Creates a Tensor from a numpy.ndarray."
- id: C4 claim: The tensor and ndarray share memory. source: S2 evidence: "The returned tensor and ndarray share the same memory."
Sources
- [S1] pandas.DataFrame.to_numpy. pandas docs. https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_numpy.html . License: BSD-3-Clause. Accessed 2026-11-16.
- [S2] torch.from_numpy. PyTorch docs. https://pytorch.org/docs/2.15/generated/torch.from_numpy.html . License: BSD-style. Accessed 2026-11-16.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: pandas docs, DataFrame.to_numpy, BSD-3-Clause (https://raw.githubusercontent.com/pandas-dev/pandas/main/LICENSE). Copyright (c) 2008-2011, AQR Capital Management, LLC, Lambda Foundry, Inc. and PyData Development Team; Copyright (c) 2011-2026, Open source contributors. PyTorch docs, torch.from_numpy, BSD-style (https://raw.githubusercontent.com/pytorch/pytorch/main/LICENSE). Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: pandas docs, DataFrame.to_numpy, BSD-3-Clause (https://raw.githubusercontent.com/pandas-dev/pandas/main/LICENSE). Copyright (c) 2008-2011, AQR Capital Management, LLC, Lambda Foundry, Inc. and PyData Development Team; Copyright (c) 2011-2026, Open source contributors. PyTorch docs, torch.from_numpy, BSD-style (https://raw.githubusercontent.com/pytorch/pytorch/main/LICENSE).
Cite this
iinventedlight. 2026. "DataFrame.to_numpy builds a NumPy array that torch.from_numpy can share." iinventedlight Library, iil:note:dgs3n63y, version of 2026-11-16. https://iinventedlight.com/library/notes/pandas-numpy-pytorch/. License: CC-BY-4.0.
@misc{iil_note_dgs3n63y,
author = {{iinventedlight}},
title = {{DataFrame.to\_numpy builds a NumPy array that torch.from\_numpy can share}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-16},
url = {https://iinventedlight.com/library/notes/pandas-numpy-pytorch/},
note = {Permanent ID iil:note:dgs3n63y. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/pandas-numpy-pytorch.md.