{ "cells": [ { "cell_type": "markdown", "id": "05b12869", "metadata": {}, "source": [ "# Making a line chart in plotnine\n", "\n", "## Organizing your data\n", "\n", "When you're making a time series chart (which is mostly what a line chart will be), you need to make sure your x axis column is a datetime." ] }, { "cell_type": "code", "execution_count": null, "id": "07aa059e", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "from plotnine import *\n", "\n", "df = pd.read_csv('lumber-prices.csv')\n", "df.head(2)" ] }, { "cell_type": "markdown", "id": "48007c10", "metadata": {}, "source": [ "In this dataset, we can use `df.dtypes` to see it's an `object`, which generally means a normal boring string." ] }, { "cell_type": "code", "execution_count": 2, "id": "c0de99cb", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "open float64\n", "high float64\n", "low float64\n", "close float64\n", "date object\n", "dtype: object" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.dtypes" ] }, { "cell_type": "markdown", "id": "6472fc36", "metadata": {}, "source": [ "It's simple to convert the column to a datetime using `pd.to_datetime`." ] }, { "cell_type": "code", "execution_count": 3, "id": "e7a680b2", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | open | \n", "high | \n", "low | \n", "close | \n", "date | \n", "
---|---|---|---|---|---|
0 | \n", "407.0 | \n", "424.7 | \n", "377.0 | \n", "424.7 | \n", "1996-12-09 | \n", "
1 | \n", "426.0 | \n", "450.5 | \n", "395.0 | \n", "411.4 | \n", "1997-01-02 | \n", "