{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Load And Dump Arrays\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The LArray library provides methods and functions to load and dump Array, Session, Axis Group objects to several formats such as Excel, CSV and HDF5. The HDF5 file format is designed to store and organize large amounts of data. It allows to read and write data much faster than when working with CSV and Excel files. \n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# first of all, import the LArray library\n", "from larray import *" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Loading Arrays - Basic Usage (CSV, Excel, HDF5)\n", "\n", "To read an array from a CSV file, you must use the ``read_csv`` function:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "csv_dir = get_example_filepath('examples')\n", "\n", "# read the array population from the file 'population.csv'.\n", "# The data of the array below is derived from a subset of the demo_pjan table from Eurostat\n", "population = read_csv(csv_dir / 'population.csv')\n", "population" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To read an array from a sheet of an Excel file, you can use the ``read_excel`` function:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "filepath_excel = get_example_filepath('examples.xlsx')\n", "\n", "# read the array from the sheet 'births' of the Excel file 'examples.xlsx'\n", "# The data of the array below is derived from a subset of the demo_fasec table from Eurostat\n", "births = read_excel(filepath_excel, 'births')\n", "births" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The ``open_excel`` function in combination with the ``load`` method allows you to load several arrays from the same Workbook without opening and closing it several times:\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```python\n", "# open the Excel file 'population.xlsx' and let it opened as long as you keep the indent.\n", "# The Python keyword ``with`` ensures that the Excel file is properly closed even if an error occurs\n", "with open_excel(filepath_excel) as wb:\n", " # load the array 'population' from the sheet 'population' \n", " population = wb['population'].load()\n", " # load the array 'births' from the sheet 'births'\n", " births = wb['births'].load()\n", " # load the array 'deaths' from the sheet 'deaths'\n", " deaths = wb['deaths'].load()\n", "\n", "# the Workbook is automatically closed when getting out the block defined by the with statement\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "