{ "cells": [ { "cell_type": "markdown", "id": "c452598e", "metadata": {}, "source": [ "# Pickle examples" ] }, { "cell_type": "markdown", "id": "a1406ad8", "metadata": {}, "source": [ "## Python `pickle` module\n", "\n", "In this example we want to use the Python [pickle](https://docs.python.org/3/library/pickle.html) module to save the following dict in pickle format:" ] }, { "cell_type": "code", "execution_count": 1, "id": "db7ff3f7", "metadata": {}, "outputs": [], "source": [ "pyviz = {\n", " \"Title\": \"PyViz Tutorial\",\n", " \"Language\": \"de\",\n", " \"Authors\": \"Veit Schiele\",\n", " \"License\": \"BSD-3-Clause\",\n", " \"Publication date\": \"2020-04-13\",\n", "}" ] }, { "cell_type": "code", "execution_count": 2, "id": "a247fb7a", "metadata": {}, "outputs": [], "source": [ "import pickle" ] }, { "cell_type": "code", "execution_count": 3, "id": "c842fe04", "metadata": {}, "outputs": [], "source": [ "with open(\"pyviz.pkl\", \"wb\") as f:\n", " pickle.dump(pyviz, f, pickle.HIGHEST_PROTOCOL)" ] }, { "cell_type": "markdown", "id": "b8b2a92f", "metadata": {}, "source": [ "Now we read the pickle file again:" ] }, { "cell_type": "code", "execution_count": 4, "id": "25d7b260", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'Title': 'PyViz Tutorial', 'Language': 'de', 'Authors': 'Veit Schiele', 'License': 'BSD-3-Clause', 'Publication date': '2020-04-13'}\n" ] } ], "source": [ "with open(\"pyviz.pkl\", \"rb\") as f:\n", " pyviz = pickle.load(f)\n", "\n", "print(pyviz)" ] }, { "cell_type": "markdown", "id": "0590401c", "metadata": {}, "source": [ "This way we can easily store Python objects persistently.\n", "\n", "
\n", "\n", "**Warning:**\n", "\n", "`pickle` can only be recommended as a short-term storage format. The problem is that the format is not guaranteed to remain stable over time; an object picked today may not be unpickled with a later version of the library.\n", "
" ] }, { "cell_type": "markdown", "id": "b5d91683", "metadata": {}, "source": [ "## pandas\n", "\n", "All pandas objects have a `to_pickle` method that writes data to disk in pickle format:" ] }, { "cell_type": "code", "execution_count": 5, "id": "75de046d", "metadata": {}, "outputs": [ { "data": { "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idtitlelanguageauthorlicensedate
01Python basicsenVeit SchieleBSD-3-Clause2021-10-28
12Jupyter TutorialenVeit SchieleBSD-3-Clause2019-06-27
23Jupyter TutorialdeVeit SchieleBSD-3-Clause2020-10-26
34PyViz TutorialenVeit SchieleBSD-3-Clause2020-04-13
\n", "
" ], "text/plain": [ " id title language author license date\n", "0 1 Python basics en Veit Schiele BSD-3-Clause 2021-10-28\n", "1 2 Jupyter Tutorial en Veit Schiele BSD-3-Clause 2019-06-27\n", "2 3 Jupyter Tutorial de Veit Schiele BSD-3-Clause 2020-10-26\n", "3 4 PyViz Tutorial en Veit Schiele BSD-3-Clause 2020-04-13" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "\n", "\n", "books = pd.read_pickle(\"books.pkl\")\n", "\n", "books" ] }, { "cell_type": "markdown", "id": "29bf1b87", "metadata": {}, "source": [ "pandas objects all have a `to_pickle` method that writes the data to the hard disk in pickle format:" ] }, { "cell_type": "code", "execution_count": 6, "id": "70fad48e", "metadata": {}, "outputs": [], "source": [ "books.to_pickle(\"books.pkl\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.13 Kernel", "language": "python", "name": "python313" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.0" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 5 }