← All notes

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

Sources

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

Permanent ID: iil:note:dgs3n63y

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.},
}

Markdown

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.