]> git.proxmox.com Git - ceph.git/blame - ceph/src/arrow/docs/source/python/numpy.rst
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / docs / source / python / numpy.rst
CommitLineData
1d09f67e
TL
1.. Licensed to the Apache Software Foundation (ASF) under one
2.. or more contributor license agreements. See the NOTICE file
3.. distributed with this work for additional information
4.. regarding copyright ownership. The ASF licenses this file
5.. to you under the Apache License, Version 2.0 (the
6.. "License"); you may not use this file except in compliance
7.. with the License. You may obtain a copy of the License at
8
9.. http://www.apache.org/licenses/LICENSE-2.0
10
11.. Unless required by applicable law or agreed to in writing,
12.. software distributed under the License is distributed on an
13.. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14.. KIND, either express or implied. See the License for the
15.. specific language governing permissions and limitations
16.. under the License.
17
18.. _numpy_interop:
19
20NumPy Integration
21=================
22
23PyArrow allows converting back and forth from
24`NumPy <https://www.numpy.org/>`_ arrays to Arrow :ref:`Arrays <data.array>`.
25
26NumPy to Arrow
27--------------
28
29To convert a NumPy array to Arrow, one can simply call the :func:`pyarrow.array`
30factory function.
31
32.. code-block:: pycon
33
34 >>> import numpy as np
35 >>> import pyarrow as pa
36 >>> data = np.arange(10, dtype='int16')
37 >>> arr = pa.array(data)
38 >>> arr
39 <pyarrow.lib.Int16Array object at 0x7fb1d1e6ae58>
40 [
41 0,
42 1,
43 2,
44 3,
45 4,
46 5,
47 6,
48 7,
49 8,
50 9
51 ]
52
53Converting from NumPy supports a wide range of input dtypes, including
54structured dtypes or strings.
55
56Arrow to NumPy
57--------------
58
59In the reverse direction, it is possible to produce a view of an Arrow Array
60for use with NumPy using the :meth:`~pyarrow.Array.to_numpy` method.
61This is limited to primitive types for which NumPy has the same physical
62representation as Arrow, and assuming the Arrow data has no nulls.
63
64.. code-block:: pycon
65
66 >>> import numpy as np
67 >>> import pyarrow as pa
68 >>> arr = pa.array([4, 5, 6], type=pa.int32())
69 >>> view = arr.to_numpy()
70 >>> view
71 array([4, 5, 6], dtype=int32)
72
73For more complex data types, you have to use the :meth:`~pyarrow.Array.to_pandas`
74method (which will construct a Numpy array with Pandas semantics for, e.g.,
75representation of null values).