{ "metadata": { "kernelspec": { "name": "python", "display_name": "Python (Pyodide)", "language": "python" }, "language_info": { "codemirror_mode": { "name": "python", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8" } }, "nbformat_minor": 4, "nbformat": 4, "cells": [ { "cell_type": "code", "source": "import duckdb", "metadata": { "trusted": true }, "outputs": [], "execution_count": 1 }, { "cell_type": "code", "source": "duckdb.sql(\"select 42\")", "metadata": { "trusted": true }, "outputs": [ { "execution_count": 2, "output_type": "execute_result", "data": { "text/plain": "┌───────┐\n│ 42 │\n│ int32 │\n├───────┤\n│ 42 │\n└───────┘" }, "metadata": {} } ], "execution_count": 2 }, { "cell_type": "code", "source": "%pip install --quiet magic-duckdb\n%load_ext magic_duckdb", "metadata": { "trusted": true }, "outputs": [], "execution_count": 1 }, { "cell_type": "code", "source": "%%dql\nSELECT 42;", "metadata": { "trusted": true }, "outputs": [ { "execution_count": 2, "output_type": "execute_result", "data": { "text/plain": " 42\n0 42", "text/html": "
\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n
42
042
\n
" }, "metadata": {} } ], "execution_count": 2 }, { "cell_type": "code", "source": "%pip install jupysql\n%env PLOOMBER_STATS_ENABLED=\"false\"\n%env PLOOMBER_VERSION_CHECK_DISABLED=\"false\"\n%load_ext sql\n\n#https://jupysql.ploomber.io/en/latest/integrations/duckdb-native.html", "metadata": { "trusted": true }, "outputs": [ { "name": "stdout", "text": "env: PLOOMBER_STATS_ENABLED=\"false\"\nenv: PLOOMBER_VERSION_CHECK_DISABLED=\"false\"\n", "output_type": "stream" } ], "execution_count": 1 }, { "cell_type": "code", "source": "import pandas as pd\nimport duckdb\n\nconn = duckdb.connect()\ndf = pd.DataFrame({\"x\": range(10)})", "metadata": { "trusted": true }, "outputs": [], "execution_count": 4 }, { "cell_type": "code", "source": "%sql conn", "metadata": { "trusted": true }, "outputs": [], "execution_count": 5 }, { "cell_type": "code", "source": "%%sql\nSELECT *\nFROM df\nWHERE x > 4", "metadata": { "trusted": true }, "outputs": [ { "output_type": "display_data", "data": { "text/plain": "Running query in 'DuckDBPyConnection'", "text/html": "Running query in 'DuckDBPyConnection'" }, "metadata": {} }, { "execution_count": 6, "output_type": "execute_result", "data": { "text/plain": "+---+\n| x |\n+---+\n| 5 |\n| 6 |\n| 7 |\n| 8 |\n| 9 |\n+---+", "text/html": "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
x
5
6
7
8
9
" }, "metadata": {} } ], "execution_count": 6 }, { "cell_type": "code", "source": "df.to_csv(\"test.csv\")", "metadata": { "trusted": true }, "outputs": [], "execution_count": 8 }, { "cell_type": "code", "source": "%%sql\nCREATE TABLE readcsvtest AS SELECT * FROM test.csv", "metadata": { "trusted": true }, "outputs": [ { "output_type": "display_data", "data": { "text/plain": "Running query in 'DuckDBPyConnection'", "text/html": "Running query in 'DuckDBPyConnection'" }, "metadata": {} }, { "execution_count": 9, "output_type": "execute_result", "data": { "text/plain": "+-------+\n| Count |\n+-------+\n| 10 |\n+-------+", "text/html": "\n \n \n \n \n \n \n \n \n \n \n
Count
10
" }, "metadata": {} } ], "execution_count": 9 }, { "cell_type": "code", "source": "%%sql\nSELECT *\nFROM readcsvtest\nLIMIT 3", "metadata": { "trusted": true }, "outputs": [ { "output_type": "display_data", "data": { "text/plain": "Running query in 'DuckDBPyConnection'", "text/html": "Running query in 'DuckDBPyConnection'" }, "metadata": {} }, { "execution_count": 10, "output_type": "execute_result", "data": { "text/plain": "+---------+---+\n| column0 | x |\n+---------+---+\n| 0 | 0 |\n| 1 | 1 |\n| 2 | 2 |\n+---------+---+", "text/html": "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
column0x
00
11
22
" }, "metadata": {} } ], "execution_count": 10 }, { "cell_type": "code", "source": " ", "metadata": { "trusted": true }, "outputs": [], "execution_count": null } ] }