blob: cc0ce0873e06bd463029661dd482045ad56e615d [file] [log] [blame]
<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Memory and IO Interfaces &mdash; Apache Arrow v3.0.0</title>
<link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="../_static/theme_overrides.css" type="text/css" />
<!--[if lt IE 9]>
<script src="../_static/js/html5shiv.min.js"></script>
<![endif]-->
<script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
<script src="../_static/jquery.js"></script>
<script src="../_static/underscore.js"></script>
<script src="../_static/doctools.js"></script>
<script src="../_static/language_data.js"></script>
<script type="text/javascript" src="../_static/js/theme.js"></script>
<link rel="canonical" href="https://arrow.apache.org/docs/python/memory.html" />
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="next" title="Data Types and In-Memory Data Model" href="data.html" />
<link rel="prev" title="Installing PyArrow" href="install.html" />
<!-- Matomo -->
<script>
var _paq = window._paq = window._paq || [];
/* tracker methods like "setCustomDimension" should be called before "trackPageView" */
_paq.push(["setDoNotTrack", true]);
_paq.push(["disableCookies"]);
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
var u="https://analytics.apache.org/";
_paq.push(['setTrackerUrl', u+'matomo.php']);
_paq.push(['setSiteId', '20']);
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
})();
</script>
<!-- End Matomo Code -->
</head>
<body class="wy-body-for-nav">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search" >
<a href="../index.html" class="icon icon-home"> Apache Arrow
</a>
<div class="version">
3.0.0
</div>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
<input type="text" name="q" placeholder="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
<p class="caption"><span class="caption-text">Specifications and Protocols</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../format/Versioning.html">Format Versioning and Stability</a></li>
<li class="toctree-l1"><a class="reference internal" href="../format/Columnar.html">Arrow Columnar Format</a></li>
<li class="toctree-l1"><a class="reference internal" href="../format/Flight.html">Arrow Flight RPC</a></li>
<li class="toctree-l1"><a class="reference internal" href="../format/Integration.html">Integration Testing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../format/CDataInterface.html">The Arrow C data interface</a></li>
<li class="toctree-l1"><a class="reference internal" href="../format/CStreamInterface.html">The Arrow C stream interface</a></li>
<li class="toctree-l1"><a class="reference internal" href="../format/Other.html">Other Data Structures</a></li>
</ul>
<p class="caption"><span class="caption-text">Libraries</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../status.html">Implementation Status</a></li>
<li class="toctree-l1"><a class="reference external" href="https://arrow.apache.org/docs/c_glib/">C/GLib</a></li>
<li class="toctree-l1"><a class="reference internal" href="../cpp/index.html">C++</a></li>
<li class="toctree-l1"><a class="reference external" href="https://github.com/apache/arrow/blob/master/csharp/README.md">C#</a></li>
<li class="toctree-l1"><a class="reference external" href="https://godoc.org/github.com/apache/arrow/go/arrow">Go</a></li>
<li class="toctree-l1"><a class="reference internal" href="../java/index.html">Java</a></li>
<li class="toctree-l1"><a class="reference external" href="https://arrow.apache.org/docs/js/">JavaScript</a></li>
<li class="toctree-l1"><a class="reference external" href="https://github.com/apache/arrow/blob/master/julia/Arrow/README.md">Julia</a></li>
<li class="toctree-l1"><a class="reference external" href="https://github.com/apache/arrow/blob/master/matlab/README.md">MATLAB</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="index.html">Python</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="install.html">Installing PyArrow</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">Memory and IO Interfaces</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#referencing-and-allocating-memory">Referencing and Allocating Memory</a><ul>
<li class="toctree-l4"><a class="reference internal" href="#pyarrow-buffer">pyarrow.Buffer</a></li>
<li class="toctree-l4"><a class="reference internal" href="#memory-pools">Memory Pools</a></li>
</ul>
</li>
<li class="toctree-l3"><a class="reference internal" href="#input-and-output">Input and Output</a><ul>
<li class="toctree-l4"><a class="reference internal" href="#high-level-api">High-Level API</a></li>
<li class="toctree-l4"><a class="reference internal" href="#on-disk-and-memory-mapped-files">On-Disk and Memory Mapped Files</a></li>
<li class="toctree-l4"><a class="reference internal" href="#in-memory-reading-and-writing">In-Memory Reading and Writing</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="data.html">Data Types and In-Memory Data Model</a></li>
<li class="toctree-l2"><a class="reference internal" href="compute.html">Compute Functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="ipc.html">Streaming, Serialization, and IPC</a></li>
<li class="toctree-l2"><a class="reference internal" href="filesystems.html">Filesystem Interface</a></li>
<li class="toctree-l2"><a class="reference internal" href="filesystems_deprecated.html">Filesystem Interface (legacy)</a></li>
<li class="toctree-l2"><a class="reference internal" href="plasma.html">The Plasma In-Memory Object Store</a></li>
<li class="toctree-l2"><a class="reference internal" href="numpy.html">NumPy Integration</a></li>
<li class="toctree-l2"><a class="reference internal" href="pandas.html">Pandas Integration</a></li>
<li class="toctree-l2"><a class="reference internal" href="timestamps.html">Timestamps</a></li>
<li class="toctree-l2"><a class="reference internal" href="csv.html">Reading CSV files</a></li>
<li class="toctree-l2"><a class="reference internal" href="feather.html">Feather File Format</a></li>
<li class="toctree-l2"><a class="reference internal" href="json.html">Reading JSON files</a></li>
<li class="toctree-l2"><a class="reference internal" href="parquet.html">Reading and Writing the Apache Parquet Format</a></li>
<li class="toctree-l2"><a class="reference internal" href="dataset.html">Tabular Datasets</a></li>
<li class="toctree-l2"><a class="reference internal" href="cuda.html">CUDA Integration</a></li>
<li class="toctree-l2"><a class="reference internal" href="extending_types.html">Extending pyarrow</a></li>
<li class="toctree-l2"><a class="reference internal" href="extending.html">Using pyarrow from C++ and Cython Code</a></li>
<li class="toctree-l2"><a class="reference internal" href="api.html">API Reference</a></li>
<li class="toctree-l2"><a class="reference internal" href="getting_involved.html">Getting Involved</a></li>
<li class="toctree-l2"><a class="reference internal" href="benchmarks.html">Benchmarks</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference external" href="https://arrow.apache.org/docs/r/">R</a></li>
<li class="toctree-l1"><a class="reference external" href="https://github.com/apache/arrow/blob/master/ruby/README.md">Ruby</a></li>
<li class="toctree-l1"><a class="reference external" href="https://docs.rs/crate/arrow/">Rust</a></li>
</ul>
<p class="caption"><span class="caption-text">Development</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../developers/contributing.html">Contributing to Apache Arrow</a></li>
<li class="toctree-l1"><a class="reference internal" href="../developers/cpp/index.html">C++ Development</a></li>
<li class="toctree-l1"><a class="reference internal" href="../developers/python.html">Python Development</a></li>
<li class="toctree-l1"><a class="reference internal" href="../developers/archery.html">Daily Development using Archery</a></li>
<li class="toctree-l1"><a class="reference internal" href="../developers/crossbow.html">Packaging and Testing with Crossbow</a></li>
<li class="toctree-l1"><a class="reference internal" href="../developers/docker.html">Running Docker Builds</a></li>
<li class="toctree-l1"><a class="reference internal" href="../developers/benchmarks.html">Benchmarks</a></li>
<li class="toctree-l1"><a class="reference internal" href="../developers/documentation.html">Building the Documentation</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" aria-label="top navigation">
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="../index.html">Apache Arrow</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href="../index.html" class="icon icon-home"></a> &raquo;</li>
<li><a href="index.html">Python bindings</a> &raquo;</li>
<li>Memory and IO Interfaces</li>
<li class="wy-breadcrumbs-aside">
<a href="../_sources/python/memory.rst.txt" rel="nofollow"> View page source</a>
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<div class="section" id="memory-and-io-interfaces">
<span id="io"></span><h1>Memory and IO Interfaces<a class="headerlink" href="#memory-and-io-interfaces" title="Permalink to this headline"></a></h1>
<p>This section will introduce you to the major concepts in PyArrow’s memory
management and IO systems:</p>
<ul class="simple">
<li><p>Buffers</p></li>
<li><p>Memory pools</p></li>
<li><p>File-like and stream-like objects</p></li>
</ul>
<div class="section" id="referencing-and-allocating-memory">
<h2>Referencing and Allocating Memory<a class="headerlink" href="#referencing-and-allocating-memory" title="Permalink to this headline"></a></h2>
<div class="section" id="pyarrow-buffer">
<h3>pyarrow.Buffer<a class="headerlink" href="#pyarrow-buffer" title="Permalink to this headline"></a></h3>
<p>The <a class="reference internal" href="generated/pyarrow.Buffer.html#pyarrow.Buffer" title="pyarrow.Buffer"><code class="xref py py-class docutils literal notranslate"><span class="pre">Buffer</span></code></a> object wraps the C++ <a class="reference internal" href="../cpp/api/memory.html#_CPPv4N5arrow6BufferE" title="arrow::Buffer"><code class="xref cpp cpp-class docutils literal notranslate"><span class="pre">arrow::Buffer</span></code></a> type
which is the primary tool for memory management in Apache Arrow in C++. It permits
higher-level array classes to safely interact with memory which they may or may
not own. <code class="docutils literal notranslate"><span class="pre">arrow::Buffer</span></code> can be zero-copy sliced to permit Buffers to cheaply
reference other Buffers, while preserving memory lifetime and clean
parent-child relationships.</p>
<p>There are many implementations of <code class="docutils literal notranslate"><span class="pre">arrow::Buffer</span></code>, but they all provide a
standard interface: a data pointer and length. This is similar to Python’s
built-in <cite>buffer protocol</cite> and <code class="docutils literal notranslate"><span class="pre">memoryview</span></code> objects.</p>
<p>A <a class="reference internal" href="generated/pyarrow.Buffer.html#pyarrow.Buffer" title="pyarrow.Buffer"><code class="xref py py-class docutils literal notranslate"><span class="pre">Buffer</span></code></a> can be created from any Python object implementing
the buffer protocol by calling the <a class="reference internal" href="generated/pyarrow.py_buffer.html#pyarrow.py_buffer" title="pyarrow.py_buffer"><code class="xref py py-func docutils literal notranslate"><span class="pre">py_buffer()</span></code></a> function. Let’s consider
a bytes object:</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [1]: </span><span class="kn">import</span> <span class="nn">pyarrow</span> <span class="kn">as</span> <span class="nn">pa</span>
<span class="gp">In [2]: </span><span class="n">data</span> <span class="o">=</span> <span class="sa">b</span><span class="s1">&#39;abcdefghijklmnopqrstuvwxyz&#39;</span>
<span class="gp">In [3]: </span><span class="n">buf</span> <span class="o">=</span> <span class="n">pa</span><span class="o">.</span><span class="n">py_buffer</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="gp">In [4]: </span><span class="n">buf</span>
<span class="gh">Out[4]: </span><span class="go">&lt;pyarrow.lib.Buffer at 0x7efd799b9870&gt;</span>
<span class="gp">In [5]: </span><span class="n">buf</span><span class="o">.</span><span class="n">size</span>
<span class="gh">Out[5]: </span><span class="go">26</span>
</pre></div>
</div>
<p>Creating a Buffer in this way does not allocate any memory; it is a zero-copy
view on the memory exported from the <code class="docutils literal notranslate"><span class="pre">data</span></code> bytes object.</p>
<p>External memory, under the form of a raw pointer and size, can also be
referenced using the <a class="reference internal" href="generated/pyarrow.foreign_buffer.html#pyarrow.foreign_buffer" title="pyarrow.foreign_buffer"><code class="xref py py-func docutils literal notranslate"><span class="pre">foreign_buffer()</span></code></a> function.</p>
<p>Buffers can be used in circumstances where a Python buffer or memoryview is
required, and such conversions are zero-copy:</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [6]: </span><span class="n">memoryview</span><span class="p">(</span><span class="n">buf</span><span class="p">)</span>
<span class="gh">Out[6]: </span><span class="go">&lt;memory at 0x7efd799ce408&gt;</span>
</pre></div>
</div>
<p>The Buffer’s <a class="reference internal" href="generated/pyarrow.Buffer.html#pyarrow.Buffer.to_pybytes" title="pyarrow.Buffer.to_pybytes"><code class="xref py py-meth docutils literal notranslate"><span class="pre">to_pybytes()</span></code></a> method converts the Buffer’s data to a
Python bytestring (thus making a copy of the data):</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [7]: </span><span class="n">buf</span><span class="o">.</span><span class="n">to_pybytes</span><span class="p">()</span>
<span class="gh">Out[7]: </span><span class="go">b&#39;abcdefghijklmnopqrstuvwxyz&#39;</span>
</pre></div>
</div>
</div>
<div class="section" id="memory-pools">
<h3>Memory Pools<a class="headerlink" href="#memory-pools" title="Permalink to this headline"></a></h3>
<p>All memory allocations and deallocations (like <code class="docutils literal notranslate"><span class="pre">malloc</span></code> and <code class="docutils literal notranslate"><span class="pre">free</span></code> in C)
are tracked in an instance of <a class="reference internal" href="generated/pyarrow.MemoryPool.html#pyarrow.MemoryPool" title="pyarrow.MemoryPool"><code class="xref py py-class docutils literal notranslate"><span class="pre">MemoryPool</span></code></a>. This means that we can
then precisely track amount of memory that has been allocated:</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [8]: </span><span class="n">pa</span><span class="o">.</span><span class="n">total_allocated_bytes</span><span class="p">()</span>
<span class="gh">Out[8]: </span><span class="go">200045632</span>
</pre></div>
</div>
<p>Let’s allocate a resizable <a class="reference internal" href="generated/pyarrow.Buffer.html#pyarrow.Buffer" title="pyarrow.Buffer"><code class="xref py py-class docutils literal notranslate"><span class="pre">Buffer</span></code></a> from the default pool:</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [9]: </span><span class="n">buf</span> <span class="o">=</span> <span class="n">pa</span><span class="o">.</span><span class="n">allocate_buffer</span><span class="p">(</span><span class="mi">1024</span><span class="p">,</span> <span class="n">resizable</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="gp">In [10]: </span><span class="n">pa</span><span class="o">.</span><span class="n">total_allocated_bytes</span><span class="p">()</span>
<span class="gh">Out[10]: </span><span class="go">200046656</span>
<span class="gp">In [11]: </span><span class="n">buf</span><span class="o">.</span><span class="n">resize</span><span class="p">(</span><span class="mi">2048</span><span class="p">)</span>
<span class="gp">In [12]: </span><span class="n">pa</span><span class="o">.</span><span class="n">total_allocated_bytes</span><span class="p">()</span>
<span class="gh">Out[12]: </span><span class="go">200047680</span>
</pre></div>
</div>
<p>The default allocator requests memory in a minimum increment of 64 bytes. If
the buffer is garbaged-collected, all of the memory is freed:</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [13]: </span><span class="n">buf</span> <span class="o">=</span> <span class="bp">None</span>
<span class="gp">In [14]: </span><span class="n">pa</span><span class="o">.</span><span class="n">total_allocated_bytes</span><span class="p">()</span>
<span class="gh">Out[14]: </span><span class="go">200045632</span>
</pre></div>
</div>
<p>Besides the default built-in memory pool, there may be additional memory pools
to choose (such as <a class="reference external" href="https://github.com/microsoft/mimalloc">mimalloc</a>)
from depending on how Arrow was built. One can get the backend
name for a memory pool:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">pa</span><span class="o">.</span><span class="n">default_memory_pool</span><span class="p">()</span><span class="o">.</span><span class="n">backend_name</span>
<span class="go">&#39;jemalloc&#39;</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="api/memory.html#api-memory-pool"><span class="std std-ref">API documentation for memory pools</span></a>.</p>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p>On-GPU buffers using Arrow’s optional <a class="reference internal" href="cuda.html"><span class="doc">CUDA integration</span></a>.</p>
</div>
</div>
</div>
<div class="section" id="input-and-output">
<h2>Input and Output<a class="headerlink" href="#input-and-output" title="Permalink to this headline"></a></h2>
<p id="io-native-file">The Arrow C++ libraries have several abstract interfaces for different kinds of
IO objects:</p>
<ul class="simple">
<li><p>Read-only streams</p></li>
<li><p>Read-only files supporting random access</p></li>
<li><p>Write-only streams</p></li>
<li><p>Write-only files supporting random access</p></li>
<li><p>File supporting reads, writes, and random access</p></li>
</ul>
<p>In the interest of making these objects behave more like Python’s built-in
<code class="docutils literal notranslate"><span class="pre">file</span></code> objects, we have defined a <a class="reference internal" href="generated/pyarrow.NativeFile.html#pyarrow.NativeFile" title="pyarrow.NativeFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">NativeFile</span></code></a> base class
which implements the same API as regular Python file objects.</p>
<p><a class="reference internal" href="generated/pyarrow.NativeFile.html#pyarrow.NativeFile" title="pyarrow.NativeFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">NativeFile</span></code></a> has some important features which make it
preferable to using Python files with PyArrow where possible:</p>
<ul class="simple">
<li><p>Other Arrow classes can access the internal C++ IO objects natively, and do
not need to acquire the Python GIL</p></li>
<li><p>Native C++ IO may be able to do zero-copy IO, such as with memory maps</p></li>
</ul>
<p>There are several kinds of <a class="reference internal" href="generated/pyarrow.NativeFile.html#pyarrow.NativeFile" title="pyarrow.NativeFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">NativeFile</span></code></a> options available:</p>
<ul class="simple">
<li><p><a class="reference internal" href="generated/pyarrow.OSFile.html#pyarrow.OSFile" title="pyarrow.OSFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">OSFile</span></code></a>, a native file that uses your operating system’s
file descriptors</p></li>
<li><p><a class="reference internal" href="generated/pyarrow.MemoryMappedFile.html#pyarrow.MemoryMappedFile" title="pyarrow.MemoryMappedFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">MemoryMappedFile</span></code></a>, for reading (zero-copy) and writing with
memory maps</p></li>
<li><p><a class="reference internal" href="generated/pyarrow.BufferReader.html#pyarrow.BufferReader" title="pyarrow.BufferReader"><code class="xref py py-class docutils literal notranslate"><span class="pre">BufferReader</span></code></a>, for reading <a class="reference internal" href="generated/pyarrow.Buffer.html#pyarrow.Buffer" title="pyarrow.Buffer"><code class="xref py py-class docutils literal notranslate"><span class="pre">Buffer</span></code></a> objects
as a file</p></li>
<li><p><a class="reference internal" href="generated/pyarrow.BufferOutputStream.html#pyarrow.BufferOutputStream" title="pyarrow.BufferOutputStream"><code class="xref py py-class docutils literal notranslate"><span class="pre">BufferOutputStream</span></code></a>, for writing data in-memory, producing a
Buffer at the end</p></li>
<li><p><a class="reference internal" href="generated/pyarrow.FixedSizeBufferWriter.html#pyarrow.FixedSizeBufferWriter" title="pyarrow.FixedSizeBufferWriter"><code class="xref py py-class docutils literal notranslate"><span class="pre">FixedSizeBufferWriter</span></code></a>, for writing data into an already
allocated Buffer</p></li>
<li><p><a class="reference internal" href="generated/pyarrow.HdfsFile.html#pyarrow.HdfsFile" title="pyarrow.HdfsFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">HdfsFile</span></code></a>, for reading and writing data to the Hadoop Filesystem</p></li>
<li><p><a class="reference internal" href="generated/pyarrow.PythonFile.html#pyarrow.PythonFile" title="pyarrow.PythonFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">PythonFile</span></code></a>, for interfacing with Python file objects in C++</p></li>
<li><p><a class="reference internal" href="generated/pyarrow.CompressedInputStream.html#pyarrow.CompressedInputStream" title="pyarrow.CompressedInputStream"><code class="xref py py-class docutils literal notranslate"><span class="pre">CompressedInputStream</span></code></a> and
<a class="reference internal" href="generated/pyarrow.CompressedOutputStream.html#pyarrow.CompressedOutputStream" title="pyarrow.CompressedOutputStream"><code class="xref py py-class docutils literal notranslate"><span class="pre">CompressedOutputStream</span></code></a>, for on-the-fly compression or
decompression to/from another stream</p></li>
</ul>
<p>There are also high-level APIs to make instantiating common kinds of streams
easier.</p>
<div class="section" id="high-level-api">
<h3>High-Level API<a class="headerlink" href="#high-level-api" title="Permalink to this headline"></a></h3>
<div class="section" id="input-streams">
<h4>Input Streams<a class="headerlink" href="#input-streams" title="Permalink to this headline"></a></h4>
<p>The <a class="reference internal" href="generated/pyarrow.input_stream.html#pyarrow.input_stream" title="pyarrow.input_stream"><code class="xref py py-func docutils literal notranslate"><span class="pre">input_stream()</span></code></a> function allows creating a readable
<a class="reference internal" href="generated/pyarrow.NativeFile.html#pyarrow.NativeFile" title="pyarrow.NativeFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">NativeFile</span></code></a> from various kinds of sources.</p>
<ul>
<li><p>If passed a <a class="reference internal" href="generated/pyarrow.Buffer.html#pyarrow.Buffer" title="pyarrow.Buffer"><code class="xref py py-class docutils literal notranslate"><span class="pre">Buffer</span></code></a> or a <code class="docutils literal notranslate"><span class="pre">memoryview</span></code> object, a
<a class="reference internal" href="generated/pyarrow.BufferReader.html#pyarrow.BufferReader" title="pyarrow.BufferReader"><code class="xref py py-class docutils literal notranslate"><span class="pre">BufferReader</span></code></a> will be returned:</p>
<blockquote>
<div><div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [15]: </span><span class="n">buf</span> <span class="o">=</span> <span class="n">memoryview</span><span class="p">(</span><span class="sa">b</span><span class="s2">&quot;some data&quot;</span><span class="p">)</span>
<span class="gp">In [16]: </span><span class="n">stream</span> <span class="o">=</span> <span class="n">pa</span><span class="o">.</span><span class="n">input_stream</span><span class="p">(</span><span class="n">buf</span><span class="p">)</span>
<span class="gp">In [17]: </span><span class="n">stream</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span>
<span class="gh">Out[17]: </span><span class="go">b&#39;some&#39;</span>
</pre></div>
</div>
</div></blockquote>
</li>
<li><p>If passed a string or file path, it will open the given file on disk
for reading, creating a <a class="reference internal" href="generated/pyarrow.OSFile.html#pyarrow.OSFile" title="pyarrow.OSFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">OSFile</span></code></a>. Optionally, the file
can be compressed: if its filename ends with a recognized extension
such as <code class="docutils literal notranslate"><span class="pre">.gz</span></code>, its contents will automatically be decompressed on
reading.</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [18]: </span><span class="kn">import</span> <span class="nn">gzip</span>
<span class="gp">In [19]: </span><span class="k">with</span> <span class="n">gzip</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s1">&#39;example.gz&#39;</span><span class="p">,</span> <span class="s1">&#39;wb&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
<span class="gp"> ....: </span> <span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39;some data</span><span class="se">\n</span><span class="s1">&#39;</span> <span class="o">*</span> <span class="mi">3</span><span class="p">)</span>
<span class="gp"> ....: </span>
<span class="gp">In [20]: </span><span class="n">stream</span> <span class="o">=</span> <span class="n">pa</span><span class="o">.</span><span class="n">input_stream</span><span class="p">(</span><span class="s1">&#39;example.gz&#39;</span><span class="p">)</span>
<span class="gp">In [21]: </span><span class="n">stream</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
<span class="gh">Out[21]: </span><span class="go">b&#39;some data\nsome data\nsome data\n&#39;</span>
</pre></div>
</div>
</li>
<li><p>If passed a Python file object, it will wrapped in a <a class="reference internal" href="generated/pyarrow.PythonFile.html#pyarrow.PythonFile" title="pyarrow.PythonFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">PythonFile</span></code></a>
such that the Arrow C++ libraries can read data from it (at the expense
of a slight overhead).</p></li>
</ul>
</div>
<div class="section" id="output-streams">
<h4>Output Streams<a class="headerlink" href="#output-streams" title="Permalink to this headline"></a></h4>
<p><a class="reference internal" href="generated/pyarrow.output_stream.html#pyarrow.output_stream" title="pyarrow.output_stream"><code class="xref py py-func docutils literal notranslate"><span class="pre">output_stream()</span></code></a> is the equivalent function for output streams
and allows creating a writable <a class="reference internal" href="generated/pyarrow.NativeFile.html#pyarrow.NativeFile" title="pyarrow.NativeFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">NativeFile</span></code></a>. It has the same
features as explained above for <a class="reference internal" href="generated/pyarrow.input_stream.html#pyarrow.input_stream" title="pyarrow.input_stream"><code class="xref py py-func docutils literal notranslate"><span class="pre">input_stream()</span></code></a>, such as being
able to write to buffers or do on-the-fly compression.</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [22]: </span><span class="k">with</span> <span class="n">pa</span><span class="o">.</span><span class="n">output_stream</span><span class="p">(</span><span class="s1">&#39;example1.dat&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">stream</span><span class="p">:</span>
<span class="gp"> ....: </span> <span class="n">stream</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39;some data&#39;</span><span class="p">)</span>
<span class="gp"> ....: </span>
<span class="gp">In [23]: </span><span class="n">f</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s1">&#39;example1.dat&#39;</span><span class="p">,</span> <span class="s1">&#39;rb&#39;</span><span class="p">)</span>
<span class="gp">In [24]: </span><span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
<span class="gh">Out[24]: </span><span class="go">b&#39;some data&#39;</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="on-disk-and-memory-mapped-files">
<h3>On-Disk and Memory Mapped Files<a class="headerlink" href="#on-disk-and-memory-mapped-files" title="Permalink to this headline"></a></h3>
<p>PyArrow includes two ways to interact with data on disk: standard operating
system-level file APIs, and memory-mapped files. In regular Python we can
write:</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [25]: </span><span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s1">&#39;example2.dat&#39;</span><span class="p">,</span> <span class="s1">&#39;wb&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
<span class="gp"> ....: </span> <span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39;some example data&#39;</span><span class="p">)</span>
<span class="gp"> ....: </span>
</pre></div>
</div>
<p>Using pyarrow’s <a class="reference internal" href="generated/pyarrow.OSFile.html#pyarrow.OSFile" title="pyarrow.OSFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">OSFile</span></code></a> class, you can write:</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [26]: </span><span class="k">with</span> <span class="n">pa</span><span class="o">.</span><span class="n">OSFile</span><span class="p">(</span><span class="s1">&#39;example3.dat&#39;</span><span class="p">,</span> <span class="s1">&#39;wb&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
<span class="gp"> ....: </span> <span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39;some example data&#39;</span><span class="p">)</span>
<span class="gp"> ....: </span>
</pre></div>
</div>
<p>For reading files, you can use <a class="reference internal" href="generated/pyarrow.OSFile.html#pyarrow.OSFile" title="pyarrow.OSFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">OSFile</span></code></a> or
<a class="reference internal" href="generated/pyarrow.MemoryMappedFile.html#pyarrow.MemoryMappedFile" title="pyarrow.MemoryMappedFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">MemoryMappedFile</span></code></a>. The difference between these is that
<a class="reference internal" href="generated/pyarrow.OSFile.html#pyarrow.OSFile" title="pyarrow.OSFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">OSFile</span></code></a> allocates new memory on each read, like Python file
objects. In reads from memory maps, the library constructs a buffer referencing
the mapped memory without any memory allocation or copying:</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [27]: </span><span class="n">file_obj</span> <span class="o">=</span> <span class="n">pa</span><span class="o">.</span><span class="n">OSFile</span><span class="p">(</span><span class="s1">&#39;example2.dat&#39;</span><span class="p">)</span>
<span class="gp">In [28]: </span><span class="n">mmap</span> <span class="o">=</span> <span class="n">pa</span><span class="o">.</span><span class="n">memory_map</span><span class="p">(</span><span class="s1">&#39;example3.dat&#39;</span><span class="p">)</span>
<span class="gp">In [29]: </span><span class="n">file_obj</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span>
<span class="gh">Out[29]: </span><span class="go">b&#39;some&#39;</span>
<span class="gp">In [30]: </span><span class="n">mmap</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span>
<span class="gh">Out[30]: </span><span class="go">b&#39;some&#39;</span>
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">read</span></code> method implements the standard Python file <code class="docutils literal notranslate"><span class="pre">read</span></code> API. To read
into Arrow Buffer objects, use <code class="docutils literal notranslate"><span class="pre">read_buffer</span></code>:</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [31]: </span><span class="n">mmap</span><span class="o">.</span><span class="n">seek</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="gh">Out[31]: </span><span class="go">0</span>
<span class="gp">In [32]: </span><span class="n">buf</span> <span class="o">=</span> <span class="n">mmap</span><span class="o">.</span><span class="n">read_buffer</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span>
<span class="gp">In [33]: </span><span class="k">print</span><span class="p">(</span><span class="n">buf</span><span class="p">)</span>
<span class="go">&lt;pyarrow.lib.Buffer object at 0x7efd79a5f470&gt;</span>
<span class="gp">In [34]: </span><span class="n">buf</span><span class="o">.</span><span class="n">to_pybytes</span><span class="p">()</span>
<span class="gh">Out[34]: </span><span class="go">b&#39;some&#39;</span>
</pre></div>
</div>
<p>Many tools in PyArrow, particular the Apache Parquet interface and the file and
stream messaging tools, are more efficient when used with these <code class="docutils literal notranslate"><span class="pre">NativeFile</span></code>
types than with normal Python file objects.</p>
</div>
<div class="section" id="in-memory-reading-and-writing">
<h3>In-Memory Reading and Writing<a class="headerlink" href="#in-memory-reading-and-writing" title="Permalink to this headline"></a></h3>
<p>To assist with serialization and deserialization of in-memory data, we have
file interfaces that can read and write to Arrow Buffers.</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [35]: </span><span class="n">writer</span> <span class="o">=</span> <span class="n">pa</span><span class="o">.</span><span class="n">BufferOutputStream</span><span class="p">()</span>
<span class="gp">In [36]: </span><span class="n">writer</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39;hello, friends&#39;</span><span class="p">)</span>
<span class="gh">Out[36]: </span><span class="go">14</span>
<span class="gp">In [37]: </span><span class="n">buf</span> <span class="o">=</span> <span class="n">writer</span><span class="o">.</span><span class="n">getvalue</span><span class="p">()</span>
<span class="gp">In [38]: </span><span class="n">buf</span>
<span class="gh">Out[38]: </span><span class="go">&lt;pyarrow.lib.Buffer at 0x7efd79a7a5b0&gt;</span>
<span class="gp">In [39]: </span><span class="n">buf</span><span class="o">.</span><span class="n">size</span>
<span class="gh">Out[39]: </span><span class="go">14</span>
<span class="gp">In [40]: </span><span class="n">reader</span> <span class="o">=</span> <span class="n">pa</span><span class="o">.</span><span class="n">BufferReader</span><span class="p">(</span><span class="n">buf</span><span class="p">)</span>
<span class="gp">In [41]: </span><span class="n">reader</span><span class="o">.</span><span class="n">seek</span><span class="p">(</span><span class="mi">7</span><span class="p">)</span>
<span class="gh">Out[41]: </span><span class="go">7</span>
<span class="gp">In [42]: </span><span class="n">reader</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="mi">7</span><span class="p">)</span>
<span class="gh">Out[42]: </span><span class="go">b&#39;friends&#39;</span>
</pre></div>
</div>
<p>These have similar semantics to Python’s built-in <code class="docutils literal notranslate"><span class="pre">io.BytesIO</span></code>.</p>
</div>
</div>
</div>
</div>
</div>
<footer>
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
<a href="data.html" class="btn btn-neutral float-right" title="Data Types and In-Memory Data Model" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
<a href="install.html" class="btn btn-neutral float-left" title="Installing PyArrow" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
</div>
<hr/>
<div role="contentinfo">
<p>
&#169; Copyright 2016-2019 Apache Software Foundation.
</p>
</div>
Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
<a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script type="text/javascript">
jQuery(function () {
SphinxRtdTheme.Navigation.enable(true);
});
</script>
<script type="text/javascript" src="/docs/_static/versionwarning.js"></script></body>
</html>