| [ Web Proxy ] |
| Viewing: https://arrow.apache.org/docs/python/generated/../../cpp/../r/articles/../articles/python.html | [Back] [Original] |
The arrow package provides reticulate methods for passing data between R and Python within the same process. This article provides a brief overview.
Code in this article assumes arrow and reticulate are both loaded:
library(arrow, warn.conflicts = FALSE)
library(reticulate, warn.conflicts = FALSE)One reason you might want to use PyArrow in R is to take advantage of
functionality that is better supported in Python than in R at the
current state of development. For example, at one point in time the R
arrow package didnt support concat_arrays() but PyArrow
did, so this would have been a good use case at that time. At the time
of current writing PyArrow has more comprehensive support for Arrow Flight
than the R package but see the article on
Flight support in arrow so that would be another instance in which
PyArrow would be of benefit to R users.
A second reason that R users may want to use PyArrow is to efficiently pass data objects between R and Python. With large data sets, it can be quite costly in terms of time and CPU cycles to perform the copy and covert operations required to translate a native data structure in R (e.g., a data frame) to an analogous structure in Python (e.g., a Pandas DataFrame) and vice versa. Because Arrow data objects such as Tables have the same in-memory format in R and Python, it is possible to perform zero-copy data transfers, in which only the metadata needs to be passed between languages. As illustrated later, this drastically improves performance.
To use Arrow in Python, the pyarrow library needs to be
installed. For example, you may wish to create a Python virtual
environment containing the pyarrow library. A virtual
environment is a specific Python installation created for one project or
purpose. It is a good practice to use specific environments in Python so
that updating a package doesnt impact packages in other projects.
You can perform the set up from within R. Lets suppose you want to
call your virtual environment something like
my-pyarrow-env. Your setup code would look like this:
virtualenv_create("my-pyarrow-env")
install_pyarrow("my-pyarrow-env")If you want to install a development version of pyarrow
to the virtual environment, add nightly = TRUE to the
install_pyarrow() command:
install_pyarrow("my-pyarrow-env", nightly = TRUE)Note that you dont have to use virtual environments. If you prefer conda environments, you can use this setup code:
conda_create("my-pyarrow-env")
install_pyarrow("my-pyarrow-env")To learn more about installing and configuring Python from R, see the reticulate documentation, which discusses the topic in more detail.
Assuming that arrow and reticulate are both loaded in R, your first step is to make sure that the correct Python environment is being used. To do that with a virtual environment, use a command like this:
use_virtualenv("my-pyarrow-env")For a conda environment use the following:
use_condaenv("my-pyarrow-env")Once you have done this, the next step is to import
pyarrow into the Python session as shown below:
pa <- import("pyarrow")Executing this command in R is the equivalent of the following import in Python:
It may be a good idea to check your pyarrow version too,
as shown below:
pa$`__version__`## [1] "8.0.0"
Support for passing data to and from R is included in
pyarrow versions 0.17 and greater.
You can use the reticulate function r_to_py() to pass
objects from R to Python, and similarly you can use
py_to_r() to pull objects from the Python session into R.
To illustrate this, lets create two objects in R:
df_random is an R data frame containing 100 million rows of
random data, and tb_random is the same data stored as an
Arrow Table:
set.seed(1234)
nrows <- 10^8
df_random <- data.frame(
x = rnorm(nrows),
y = rnorm(nrows),
subset = sample(10, nrows, replace = TRUE)
)
tb_random <- arrow_table(df_random)Transferring the data from R to Python without Arrow is a time-consuming process because the underlying object has to be copied and converted to a Python data structure:
system.time({
df_py <- r_to_py(df_random)
})## user system elapsed
## 0.307 5.172 5.529
In contrast, sending the Arrow Table across happens almost instantaneously:
system.time({
tb_py <- r_to_py(tb_random)
})## user system elapsed
## 0.004 0.000 0.003
Send, however, isnt really the correct word. Internally, were passing pointers to the data between the R and Python interpreters running together in the same process, without copying anything. Nothing is being sent: were sharing and accessing the same internal Arrow memory buffers.
Its possible to send data the other direction also. For example
lets create an Array in pyarrow.
a <- pa$array(c(1, 2, 3))
a## Array
## <double>
## [
## 1,
## 2,
## 3
## ]
Notice that a is now an Array object in
your R session even though you created it in Python and you can
apply R methods on it:
a[a > 1]## Array
## <double>
## [
## 2,
## 3
## ]
Similarly, you can combine this object with Arrow objects created in
R, and you can use PyArrow methods like pa$concat_arrays()
to do so:
## Array
## <double>
## [
## 1,
## 2,
## 3,
## 5,
## 6,
## 7,
## 8,
## 9
## ]
Now you have a single Array in R.
| Web Proxy Viewer | New URL | Original Page |