blob: bbc8e196fd9294e8518146190eb23baa9c7147ea [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>Reading CSV files &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/csv.html" />
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="next" title="Feather File Format" href="feather.html" />
<link rel="prev" title="Timestamps" href="timestamps.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"><a class="reference internal" href="memory.html">Memory and IO Interfaces</a></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 current"><a class="current reference internal" href="#">Reading CSV files</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#usage">Usage</a></li>
<li class="toctree-l3"><a class="reference internal" href="#customized-parsing">Customized parsing</a></li>
<li class="toctree-l3"><a class="reference internal" href="#customized-conversion">Customized conversion</a></li>
<li class="toctree-l3"><a class="reference internal" href="#incremental-reading">Incremental reading</a></li>
<li class="toctree-l3"><a class="reference internal" href="#character-encoding">Character encoding</a></li>
<li class="toctree-l3"><a class="reference internal" href="#performance">Performance</a></li>
</ul>
</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>Reading CSV files</li>
<li class="wy-breadcrumbs-aside">
<a href="../_sources/python/csv.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="reading-csv-files">
<span id="csv"></span><h1>Reading CSV files<a class="headerlink" href="#reading-csv-files" title="Permalink to this headline"></a></h1>
<p>Arrow supports reading columnar data from CSV files.
The features currently offered are the following:</p>
<ul class="simple">
<li><p>multi-threaded or single-threaded reading</p></li>
<li><p>automatic decompression of input files (based on the filename extension,
such as <code class="docutils literal notranslate"><span class="pre">my_data.csv.gz</span></code>)</p></li>
<li><p>fetching column names from the first row in the CSV file</p></li>
<li><p>column-wise type inference and conversion to one of <code class="docutils literal notranslate"><span class="pre">null</span></code>, <code class="docutils literal notranslate"><span class="pre">int64</span></code>,
<code class="docutils literal notranslate"><span class="pre">float64</span></code>, <code class="docutils literal notranslate"><span class="pre">date32</span></code>, <code class="docutils literal notranslate"><span class="pre">timestamp[s]</span></code>, <code class="docutils literal notranslate"><span class="pre">string</span></code> or <code class="docutils literal notranslate"><span class="pre">binary</span></code> data</p></li>
<li><p>opportunistic dictionary encoding of <code class="docutils literal notranslate"><span class="pre">string</span></code> and <code class="docutils literal notranslate"><span class="pre">binary</span></code> columns
(disabled by default)</p></li>
<li><p>detecting various spellings of null values such as <code class="docutils literal notranslate"><span class="pre">NaN</span></code> or <code class="docutils literal notranslate"><span class="pre">#N/A</span></code></p></li>
</ul>
<div class="section" id="usage">
<h2>Usage<a class="headerlink" href="#usage" title="Permalink to this headline"></a></h2>
<p>CSV reading functionality is available through the <code class="xref py py-mod docutils literal notranslate"><span class="pre">pyarrow.csv</span></code> module.
In many cases, you will simply call the <a class="reference internal" href="generated/pyarrow.csv.read_csv.html#pyarrow.csv.read_csv" title="pyarrow.csv.read_csv"><code class="xref py py-func docutils literal notranslate"><span class="pre">read_csv()</span></code></a> function
with the file path you want to read from:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">pyarrow</span> <span class="k">import</span> <span class="n">csv</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fn</span> <span class="o">=</span> <span class="s1">&#39;tips.csv.gz&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">table</span> <span class="o">=</span> <span class="n">csv</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="n">fn</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">table</span>
<span class="go">pyarrow.Table</span>
<span class="go">total_bill: double</span>
<span class="go">tip: double</span>
<span class="go">sex: string</span>
<span class="go">smoker: string</span>
<span class="go">day: string</span>
<span class="go">time: string</span>
<span class="go">size: int64</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">len</span><span class="p">(</span><span class="n">table</span><span class="p">)</span>
<span class="go">244</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">df</span> <span class="o">=</span> <span class="n">table</span><span class="o">.</span><span class="n">to_pandas</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">df</span><span class="o">.</span><span class="n">head</span><span class="p">()</span>
<span class="go"> total_bill tip sex smoker day time size</span>
<span class="go">0 16.99 1.01 Female No Sun Dinner 2</span>
<span class="go">1 10.34 1.66 Male No Sun Dinner 3</span>
<span class="go">2 21.01 3.50 Male No Sun Dinner 3</span>
<span class="go">3 23.68 3.31 Male No Sun Dinner 2</span>
<span class="go">4 24.59 3.61 Female No Sun Dinner 4</span>
</pre></div>
</div>
</div>
<div class="section" id="customized-parsing">
<h2>Customized parsing<a class="headerlink" href="#customized-parsing" title="Permalink to this headline"></a></h2>
<p>To alter the default parsing settings in case of reading CSV files with an
unusual structure, you should create a <a class="reference internal" href="generated/pyarrow.csv.ParseOptions.html#pyarrow.csv.ParseOptions" title="pyarrow.csv.ParseOptions"><code class="xref py py-class docutils literal notranslate"><span class="pre">ParseOptions</span></code></a> instance
and pass it to <a class="reference internal" href="generated/pyarrow.csv.read_csv.html#pyarrow.csv.read_csv" title="pyarrow.csv.read_csv"><code class="xref py py-func docutils literal notranslate"><span class="pre">read_csv()</span></code></a>.</p>
</div>
<div class="section" id="customized-conversion">
<h2>Customized conversion<a class="headerlink" href="#customized-conversion" title="Permalink to this headline"></a></h2>
<p>To alter how CSV data is converted to Arrow types and data, you should create
a <a class="reference internal" href="generated/pyarrow.csv.ConvertOptions.html#pyarrow.csv.ConvertOptions" title="pyarrow.csv.ConvertOptions"><code class="xref py py-class docutils literal notranslate"><span class="pre">ConvertOptions</span></code></a> instance and pass it to <a class="reference internal" href="generated/pyarrow.csv.read_csv.html#pyarrow.csv.read_csv" title="pyarrow.csv.read_csv"><code class="xref py py-func docutils literal notranslate"><span class="pre">read_csv()</span></code></a>.</p>
</div>
<div class="section" id="incremental-reading">
<h2>Incremental reading<a class="headerlink" href="#incremental-reading" title="Permalink to this headline"></a></h2>
<p>For memory-constrained environments, it is also possible to read a CSV file
one batch at a time, using <a class="reference internal" href="generated/pyarrow.csv.open_csv.html#pyarrow.csv.open_csv" title="pyarrow.csv.open_csv"><code class="xref py py-func docutils literal notranslate"><span class="pre">open_csv()</span></code></a>. It currently doesn’t support
parallel reading.</p>
</div>
<div class="section" id="character-encoding">
<h2>Character encoding<a class="headerlink" href="#character-encoding" title="Permalink to this headline"></a></h2>
<p>By default, CSV files are expected to be encoded in UTF8. Non-UTF8 data
is accepted for <code class="docutils literal notranslate"><span class="pre">binary</span></code> columns. The encoding can be changed using
the <a class="reference internal" href="generated/pyarrow.csv.ReadOptions.html#pyarrow.csv.ReadOptions" title="pyarrow.csv.ReadOptions"><code class="xref py py-class docutils literal notranslate"><span class="pre">ReadOptions</span></code></a> class.</p>
</div>
<div class="section" id="performance">
<h2>Performance<a class="headerlink" href="#performance" title="Permalink to this headline"></a></h2>
<p>Due to the structure of CSV files, one cannot expect the same levels of
performance as when reading dedicated binary formats like
<a class="reference internal" href="parquet.html#parquet"><span class="std std-ref">Parquet</span></a>. Nevertheless, Arrow strives to reduce the
overhead of reading CSV files. A reasonable expectation is at least
100 MB/s per core on a performant desktop or laptop computer (measured
in source CSV bytes, not target Arrow data bytes).</p>
<p>Performance options can be controlled through the <a class="reference internal" href="generated/pyarrow.csv.ReadOptions.html#pyarrow.csv.ReadOptions" title="pyarrow.csv.ReadOptions"><code class="xref py py-class docutils literal notranslate"><span class="pre">ReadOptions</span></code></a> class.
Multi-threaded reading is the default for highest performance, distributing
the workload efficiently over all available cores.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>The number of concurrent threads is automatically inferred by Arrow.
You can inspect and change it using the <a class="reference internal" href="generated/pyarrow.cpu_count.html#pyarrow.cpu_count" title="pyarrow.cpu_count"><code class="xref py py-func docutils literal notranslate"><span class="pre">cpu_count()</span></code></a>
and <a class="reference internal" href="generated/pyarrow.set_cpu_count.html#pyarrow.set_cpu_count" title="pyarrow.set_cpu_count"><code class="xref py py-func docutils literal notranslate"><span class="pre">set_cpu_count()</span></code></a> functions, respectively.</p>
</div>
</div>
</div>
</div>
</div>
<footer>
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
<a href="feather.html" class="btn btn-neutral float-right" title="Feather File Format" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
<a href="timestamps.html" class="btn btn-neutral float-left" title="Timestamps" 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>