<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Stock Data Archive]]></title><description><![CDATA[Stock Data Archive]]></description><link>https://stockdataarchive.hashnode.dev</link><image><url>https://cdn.hashnode.com/uploads/logos/6b05edb889de134c2099df9a/6f5d39ef-5a41-47bc-8da6-ef919fc045d0.png</url><title>Stock Data Archive</title><link>https://stockdataarchive.hashnode.dev</link></image><generator>RSS for Node</generator><lastBuildDate>Thu, 26 Nov 2026 12:23:41 GMT</lastBuildDate><atom:link href="https://stockdataarchive.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[DuckDB read_csv, with stock data examples]]></title><description><![CDATA[Read CSV files with DuckDB using free AAPL stock data. Set column types, convert timestamps, combine files and save Parquet with SQL and Python.]]></description><link>https://stockdataarchive.hashnode.dev/duckdb-read-csv-with-stock-data-examples</link><guid isPermaLink="true">https://stockdataarchive.hashnode.dev/duckdb-read-csv-with-stock-data-examples</guid><category><![CDATA[duckDB]]></category><category><![CDATA[SQL]]></category><category><![CDATA[Python]]></category><category><![CDATA[data-engineering]]></category><dc:creator><![CDATA[Steve Feng]]></dc:creator><pubDate>Tue, 24 Nov 2026 20:12:52 GMT</pubDate><content:encoded><![CDATA[<p>Originally published on <a href="https://stockdataarchive.com/guides/duckdb-read-csv/">Stock Data Archive</a>.</p>
<p>read_csv loads a CSV file into DuckDB and works out the delimiter, the header and the column types by itself. Every example here runs on a free file of real AAPL trades and 1-minute bars.</p>
<h2>Get a CSV file to practise on</h2>
<p>DuckDB reads files over HTTPS. These two statements turn the free sample files into a CSV of 1-minute bars and a gzipped CSV of trades.</p>
<pre><code class="language-sql">COPY (FROM 'https://stockdataarchive.com/samples/AAPL_2026-11-09_1min.parquet')
  TO 'aapl_1min.csv';
COPY (FROM 'https://stockdataarchive.com/samples/AAPL_2026-11-09_trades.parquet')
  TO 'aapl_trades.csv.gz';
</code></pre>
<p>The first file has 846 rows. The second has 750k+ rows, one for every AAPL trade of the day.</p>
<h2>Read a CSV file with read_csv</h2>
<pre><code class="language-sql">SELECT * FROM read_csv('aapl_1min.csv') LIMIT 3;
</code></pre>
<p><code>FROM 'aapl_1min.csv'</code> reads the same file without the function name. Older examples call <code>read_csv_auto</code>, which is the same function.</p>
<h2>Check the types DuckDB detected</h2>
<p>DuckDB reads the first 20,480 rows and picks a type for each column. <code>DESCRIBE</code> shows what it picked.</p>
<pre><code class="language-sql">DESCRIBE FROM read_csv('aapl_trades.csv.gz');
</code></pre>
<table>
<thead>
<tr>
<th>Column</th>
<th>Type</th>
</tr>
</thead>
<tbody><tr>
<td><code>symbol</code></td>
<td>VARCHAR</td>
</tr>
<tr>
<td><code>ts_event</code></td>
<td>BIGINT</td>
</tr>
<tr>
<td><code>ts_sip</code></td>
<td>BIGINT</td>
</tr>
<tr>
<td><code>price</code></td>
<td>DOUBLE</td>
</tr>
<tr>
<td><code>size</code></td>
<td>DOUBLE</td>
</tr>
<tr>
<td><code>exchange</code></td>
<td>BIGINT</td>
</tr>
<tr>
<td><code>sequence_number</code></td>
<td>BIGINT</td>
</tr>
<tr>
<td><code>trade_id</code></td>
<td>BIGINT</td>
</tr>
<tr>
<td><code>conditions</code></td>
<td>VARCHAR</td>
</tr>
<tr>
<td><code>correction</code></td>
<td>BIGINT</td>
</tr>
<tr>
<td><code>tape</code></td>
<td>BIGINT</td>
</tr>
<tr>
<td><code>trf_id</code></td>
<td>BIGINT</td>
</tr>
<tr>
<td><code>ts_trf</code></td>
<td>BIGINT</td>
</tr>
</tbody></table>
<p>Two picks need a second look. <code>trade_id</code> is an identifier and became a number. <code>ts_event</code> is a time, written as nanoseconds since 1970, and stays a number until you convert it.</p>
<h2>Set column types yourself</h2>
<p><code>types</code> changes the columns you name and leaves the others to detection. Prices read as DOUBLE unless you ask for DECIMAL, which keeps them exact.</p>
<pre><code class="language-sql">SELECT *
FROM read_csv('aapl_trades.csv.gz',
    types = {'trade_id': 'VARCHAR', 'price': 'DECIMAL(18,6)'});
</code></pre>
<p><code>columns</code> names every column and its type, in file order. DuckDB then detects no types at all.</p>
<pre><code class="language-sql">SELECT *
FROM read_csv('aapl_1min.csv',
    columns = {
        'symbol': 'VARCHAR', 'timestamp': 'BIGINT',
        'open': 'DECIMAL(18,6)', 'high': 'DECIMAL(18,6)',
        'low': 'DECIMAL(18,6)', 'close': 'DECIMAL(18,6)',
        'volume': 'DOUBLE', 'trade_count': 'INTEGER'
    });
</code></pre>
<h2>Convert timestamps</h2>
<p>The bars carry their start time as integer nanoseconds in UTC. <code>to_timestamp</code> takes seconds, and <code>AT TIME ZONE</code> gives the time in New York.</p>
<pre><code class="language-sql">SELECT
    to_timestamp("timestamp" / 1e9) AT TIME ZONE 'America/New_York' AS bar_start,
    close
FROM read_csv('aapl_1min.csv')
LIMIT 3;
</code></pre>
<p>For tick data, <code>make_timestamp_ns(ts_event)</code> keeps all nine decimal places.</p>
<p>Text timestamps such as 2026-11-09 09:30:00 are detected without help. A layout such as 11/09/2026 09:30 is read as text until you pass its format.</p>
<pre><code class="language-sql">SELECT * FROM read_csv('fills.csv', timestampformat = '%m/%d/%Y %H:%M');
</code></pre>
<h2>Read many CSV files at once</h2>
<p>A pattern reads every matching file as one table. <code>filename = true</code> adds a column that says which file each row came from. <code>union_by_name = true</code> lines columns up by name when the files differ.</p>
<pre><code class="language-sql">SELECT symbol, count(*) AS bars
FROM read_csv('bars/*.csv', filename = true, union_by_name = true)
GROUP BY symbol;
</code></pre>
<p>Gzipped files need nothing extra. DuckDB reads <code>.csv.gz</code> files as they are.</p>
<h2>read_csv options you will use most</h2>
<table>
<thead>
<tr>
<th>Option</th>
<th>What it does</th>
<th>Example</th>
</tr>
</thead>
<tbody><tr>
<td><code>header</code></td>
<td>Says whether the first row holds column names.</td>
<td><code>header = false</code></td>
</tr>
<tr>
<td><code>names</code></td>
<td>Names the columns of a file that has no header.</td>
<td><code>names = ['symbol', 'price']</code></td>
</tr>
<tr>
<td><code>delim</code></td>
<td>Sets the character between values.</td>
<td><code>delim = ';'</code></td>
</tr>
<tr>
<td><code>skip</code></td>
<td>Skips lines at the top of the file.</td>
<td><code>skip = 2</code></td>
</tr>
<tr>
<td><code>nullstr</code></td>
<td>Reads a marker as NULL.</td>
<td><code>nullstr = 'n/a'</code></td>
</tr>
<tr>
<td><code>decimal_separator</code></td>
<td>Reads 346,20 as a number.</td>
<td><code>decimal_separator = ','</code></td>
</tr>
<tr>
<td><code>dateformat</code></td>
<td>Parses dates written another way.</td>
<td><code>dateformat = '%d.%m.%Y'</code></td>
</tr>
<tr>
<td><code>timestampformat</code></td>
<td>Parses timestamps written another way.</td>
<td><code>timestampformat = '%m/%d/%Y %H:%M'</code></td>
</tr>
<tr>
<td><code>sample_size</code></td>
<td>Sets how many rows decide the types. -1 reads the whole file.</td>
<td><code>sample_size = -1</code></td>
</tr>
<tr>
<td><code>all_varchar</code></td>
<td>Reads every column as text.</td>
<td><code>all_varchar = true</code></td>
</tr>
<tr>
<td><code>ignore_errors</code></td>
<td>Skips rows that do not fit.</td>
<td><code>ignore_errors = true</code></td>
</tr>
<tr>
<td><code>compression</code></td>
<td>Names the compression when the file name does not show it.</td>
<td><code>compression = 'gzip'</code></td>
</tr>
</tbody></table>
<h2>Fix “Could not convert string” errors</h2>
<p>The error means a value further down the file does not fit the type picked from the first rows. There are three ways out.</p>
<ul>
<li><code>sample_size = -1</code> reads the whole file before it picks the types.</li>
<li><code>types</code> sets the right type for that column.</li>
<li><code>store_rejects = true</code> loads the rows that fit and saves the others in a table named <code>reject_errors</code>.</li>
</ul>
<pre><code class="language-sql">CREATE TABLE bars AS
FROM read_csv('bars.csv', types = {'timestamp': 'BIGINT'}, store_rejects = true);

SELECT line, column_name, csv_line FROM reject_errors;
</code></pre>
<h2>Save the CSV as Parquet</h2>
<p>Parquet keeps the column types, so the next query detects nothing and reads only the columns it needs.</p>
<pre><code class="language-sql">COPY (FROM read_csv('aapl_trades.csv.gz', types = {'trade_id': 'VARCHAR'}))
  TO 'aapl_trades.parquet' (FORMAT parquet, COMPRESSION zstd);
</code></pre>
<p>This day of AAPL trades takes 74 MB as CSV, 18 MB gzipped and 12 MB as Parquet.</p>
<h2>Use read_csv from Python</h2>
<p>The same SQL runs from the <code>duckdb</code> package, and <code>.df()</code> returns a pandas DataFrame.</p>
<pre><code class="language-python">import duckdb

bars = duckdb.sql("""
    SELECT
        to_timestamp("timestamp" / 1e9) AT TIME ZONE 'America/New_York' AS bar_start,
        close
    FROM read_csv('aapl_1min.csv')
""").df()
</code></pre>
<p>Explore the <a href="https://stockdataarchive.com/free-intraday-data/">free sample files</a> and <a href="https://stockdataarchive.com">Stock Data Archive</a>.</p>
]]></content:encoded></item></channel></rss>