]>
Commit | Line | Data |
---|---|---|
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 | ||
20 | NumPy Integration | |
21 | ================= | |
22 | ||
23 | PyArrow allows converting back and forth from | |
24 | `NumPy <https://www.numpy.org/>`_ arrays to Arrow :ref:`Arrays <data.array>`. | |
25 | ||
26 | NumPy to Arrow | |
27 | -------------- | |
28 | ||
29 | To convert a NumPy array to Arrow, one can simply call the :func:`pyarrow.array` | |
30 | factory 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 | ||
53 | Converting from NumPy supports a wide range of input dtypes, including | |
54 | structured dtypes or strings. | |
55 | ||
56 | Arrow to NumPy | |
57 | -------------- | |
58 | ||
59 | In the reverse direction, it is possible to produce a view of an Arrow Array | |
60 | for use with NumPy using the :meth:`~pyarrow.Array.to_numpy` method. | |
61 | This is limited to primitive types for which NumPy has the same physical | |
62 | representation 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 | ||
73 | For more complex data types, you have to use the :meth:`~pyarrow.Array.to_pandas` | |
74 | method (which will construct a Numpy array with Pandas semantics for, e.g., | |
75 | representation of null values). |