{ "cells": [ { "cell_type": "markdown", "id": "3cc1df2e", "metadata": {}, "source": [ "# Importing Necessary Libraries" ] }, { "cell_type": "code", "execution_count": 1, "id": "9651be55", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import datetime as dt" ] }, { "cell_type": "markdown", "id": "7c13b9af", "metadata": {}, "source": [ "## `date`" ] }, { "cell_type": "code", "execution_count": 2, "id": "fb2d4793", "metadata": {}, "outputs": [], "source": [ "today = dt.date(2021, 8, 22)" ] }, { "cell_type": "code", "execution_count": 3, "id": "5d03f65d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "22" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "today.day" ] }, { "cell_type": "code", "execution_count": 4, "id": "33882041", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "today.month" ] }, { "cell_type": "code", "execution_count": 5, "id": "99c7155a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2021" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "today.year" ] }, { "cell_type": "markdown", "id": "d48e091f", "metadata": {}, "source": [ "## `datetime`" ] }, { "cell_type": "code", "execution_count": 6, "id": "665e9b04", "metadata": {}, "outputs": [], "source": [ "today = dt.datetime(2021, 8, 22, 17, 45, 51)" ] }, { "cell_type": "code", "execution_count": 8, "id": "9a091611", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2021-08-22 17:45:51\n" ] } ], "source": [ "print(today)" ] }, { "cell_type": "code", "execution_count": 9, "id": "431cc89d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "17" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "today.hour" ] }, { "cell_type": "code", "execution_count": 10, "id": "44c4908e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "45" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "today.minute" ] }, { "cell_type": "code", "execution_count": 11, "id": "6c9b4416", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "51" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "today.second" ] }, { "cell_type": "markdown", "id": "d2d04ef9", "metadata": {}, "source": [ "## `Timestamp`" ] }, { "cell_type": "code", "execution_count": 12, "id": "03c3438f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Timestamp('2021-12-22 00:00:00')" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.Timestamp('2021-12-22')" ] }, { "cell_type": "code", "execution_count": 13, "id": "248a1fcb", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Timestamp('2019-02-13 00:00:00')" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.Timestamp('2019/02/13')" ] }, { "cell_type": "code", "execution_count": 14, "id": "df9e1884", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Timestamp('2020-04-21 00:00:00')" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.Timestamp('2020, 4, 21')" ] }, { "cell_type": "code", "execution_count": 19, "id": "5932efac", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Timestamp('2020-04-21 18:33:12')" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.Timestamp('2020, 4, 21 18:33:12')" ] }, { "cell_type": "markdown", "id": "a285f6b1", "metadata": {}, "source": [ "## `DatetimeIndex`" ] }, { "cell_type": "code", "execution_count": 23, "id": "dae83469", "metadata": {}, "outputs": [], "source": [ "dates = ['2019/12/22', '2012/8/22', '2015/2/22']" ] }, { "cell_type": "code", "execution_count": 24, "id": "f60d761b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "DatetimeIndex(['2019-12-22', '2012-08-22', '2015-02-22'], dtype='datetime64[ns]', freq=None)" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.DatetimeIndex(dates)" ] }, { "cell_type": "code", "execution_count": 26, "id": "c027d505", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "list" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(dates)" ] }, { "cell_type": "code", "execution_count": 27, "id": "ec96df99", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'2012/8/22'" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dates[1]" ] }, { "cell_type": "code", "execution_count": 38, "id": "57ae6f6c", "metadata": {}, "outputs": [], "source": [ "dates2 = [dt.date(2021, 9, 8), dt.date(2021, 9, 9), dt.date(2021, 9, 10)]" ] }, { "cell_type": "code", "execution_count": 39, "id": "15cdb293", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[datetime.date(2021, 9, 8),\n", " datetime.date(2021, 9, 9),\n", " datetime.date(2021, 9, 10)]" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dates2" ] }, { "cell_type": "code", "execution_count": 40, "id": "072f7e4e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "DatetimeIndex(['2021-09-08', '2021-09-09', '2021-09-10'], dtype='datetime64[ns]', freq=None)" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dateIndex = pd.DatetimeIndex(dates2)\n", "dateIndex" ] }, { "cell_type": "code", "execution_count": 42, "id": "bda75f3c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2021-09-08 12\n", "2021-09-09 7\n", "2021-09-10 30\n", "dtype: int64" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "values = [12, 7, 30]\n", "pd.Series(data=values, index=dateIndex)" ] }, { "cell_type": "markdown", "id": "560b963d", "metadata": {}, "source": [ "## `pd.to_datetime()`" ] }, { "cell_type": "code", "execution_count": 44, "id": "056ec8bc", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Timestamp('2021-01-05 00:00:00')" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.to_datetime('2021, 1, 5')" ] }, { "cell_type": "code", "execution_count": 45, "id": "ec47ca72", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Timestamp('2001-12-12 00:00:00')" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.to_datetime(dt.date(2001, 12, 12))" ] }, { "cell_type": "code", "execution_count": 47, "id": "27cf74de", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Timestamp('2012-02-12 18:33:12')" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.to_datetime(dt.datetime(2012, 2, 12, 18, 33, 12))" ] }, { "cell_type": "code", "execution_count": 48, "id": "a38ee20b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "DatetimeIndex(['2019-12-22', '2012-08-22', '2015-02-22'], dtype='datetime64[ns]', freq=None)" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dates = ['2019/12/22', '2012/8/22', '2015/2/22']\n", "pd.to_datetime(dates)" ] }, { "cell_type": "code", "execution_count": 50, "id": "360ac042", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 December 22nd, 1999\n", "1 2019, 12, 12\n", "2 This is Date\n", "3 Jan 5th, 2021\n", "dtype: object" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dates3 = pd.Series(\n", " ['December 22nd, 1999', '2019, 12, 12', \"This is Date\", 'Jan 5th, 2021'])\n", "dates3" ] }, { "cell_type": "code", "execution_count": 51, "id": "52fa5519", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 1999-12-22\n", "1 2019-12-12\n", "2 NaT\n", "3 2021-01-05\n", "dtype: datetime64[ns]" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.to_datetime(dates3, errors='coerce') #invalid parsing will be set as NaT" ] }, { "cell_type": "code", "execution_count": 54, "id": "f0610740", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "DatetimeIndex(['1970-02-07 13:16:03', '1970-02-11 00:51:03',\n", " '1970-02-07 12:33:57', '1970-03-27 05:20:36'],\n", " dtype='datetime64[ns]', freq=None)" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "unixtime = [3244563, 3545463, 3242037, 7363236]\n", "pd.to_datetime(unixtime, unit='s')" ] }, { "cell_type": "markdown", "id": "d07def82", "metadata": {}, "source": [ "## Create Range in the form of Date `pd.date_range()`" ] }, { "cell_type": "code", "execution_count": 58, "id": "7d5c6691", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "DatetimeIndex(['2021-01-01', '2021-01-02', '2021-01-03', '2021-01-04',\n", " '2021-01-05', '2021-01-06', '2021-01-07', '2021-01-08',\n", " '2021-01-09', '2021-01-10',\n", " ...\n", " '2021-12-22', '2021-12-23', '2021-12-24', '2021-12-25',\n", " '2021-12-26', '2021-12-27', '2021-12-28', '2021-12-29',\n", " '2021-12-30', '2021-12-31'],\n", " dtype='datetime64[ns]', length=365, freq='D')" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "times = pd.date_range(start='2021-01-01', end='2021-12-31', freq='D')\n", "times" ] }, { "cell_type": "code", "execution_count": 61, "id": "c13d6ac9", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "DatetimeIndex(['2021-12-31', '2022-12-31', '2023-12-31', '2024-12-31',\n", " '2025-12-31', '2026-12-31', '2027-12-31', '2028-12-31',\n", " '2029-12-31'],\n", " dtype='datetime64[ns]', freq='A-DEC')" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "times = pd.date_range(start='2021-01-01', end='2029-12-31', freq='A')\n", "times" ] }, { "cell_type": "code", "execution_count": 62, "id": "21aa354a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "DatetimeIndex(['2021-01-01 00:00:00', '2021-01-01 05:00:00',\n", " '2021-01-01 10:00:00', '2021-01-01 15:00:00',\n", " '2021-01-01 20:00:00', '2021-01-02 01:00:00',\n", " '2021-01-02 06:00:00', '2021-01-02 11:00:00',\n", " '2021-01-02 16:00:00', '2021-01-02 21:00:00',\n", " ...\n", " '2021-12-29 02:00:00', '2021-12-29 07:00:00',\n", " '2021-12-29 12:00:00', '2021-12-29 17:00:00',\n", " '2021-12-29 22:00:00', '2021-12-30 03:00:00',\n", " '2021-12-30 08:00:00', '2021-12-30 13:00:00',\n", " '2021-12-30 18:00:00', '2021-12-30 23:00:00'],\n", " dtype='datetime64[ns]', length=1748, freq='5H')" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "times = pd.date_range(start='2021-01-01', end='2021-12-31', freq='5H')\n", "times" ] }, { "cell_type": "code", "execution_count": 63, "id": "5db2d6ec", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "DatetimeIndex(['2021-01-01 00:00:00', '2021-01-01 04:00:00',\n", " '2021-01-01 08:00:00', '2021-01-01 12:00:00',\n", " '2021-01-01 16:00:00', '2021-01-01 20:00:00',\n", " '2021-01-02 00:00:00', '2021-01-02 04:00:00',\n", " '2021-01-02 08:00:00', '2021-01-02 12:00:00',\n", " ...\n", " '2021-12-29 12:00:00', '2021-12-29 16:00:00',\n", " '2021-12-29 20:00:00', '2021-12-30 00:00:00',\n", " '2021-12-30 04:00:00', '2021-12-30 08:00:00',\n", " '2021-12-30 12:00:00', '2021-12-30 16:00:00',\n", " '2021-12-30 20:00:00', '2021-12-31 00:00:00'],\n", " dtype='datetime64[ns]', length=2185, freq='4H')" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "times = pd.date_range(start='2021-01-01', end='2021-12-31', freq='4H')\n", "times" ] }, { "cell_type": "code", "execution_count": null, "id": "49d3384c", "metadata": {}, "outputs": [], "source": [ "times = pd.date_range(start='2021-01-01', end='2021-12-31', freq='5H')\n", "times" ] }, { "cell_type": "markdown", "id": "6482c17e", "metadata": {}, "source": [ "## Importing Stock Data using `datareader`" ] }, { "cell_type": "code", "execution_count": 66, "id": "bece31b6", "metadata": {}, "outputs": [], "source": [ "from pandas_datareader import data" ] }, { "cell_type": "code", "execution_count": 67, "id": "593964d9", "metadata": {}, "outputs": [], "source": [ "# !pip install pandas_datareader" ] }, { "cell_type": "code", "execution_count": 69, "id": "08543a47", "metadata": {}, "outputs": [], "source": [ "company = 'MSFT'\n", "start = '2020-12-22'\n", "end = '2021-06-22'" ] }, { "cell_type": "code", "execution_count": 71, "id": "61dee028", "metadata": {}, "outputs": [], "source": [ "stocks = data.DataReader(name=company,\n", " data_source='yahoo',\n", " start=start,\n", " end=end)" ] }, { "cell_type": "code", "execution_count": 72, "id": "81a64a17", "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", " \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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
HighLowOpenCloseVolumeAdj Close
Date
2020-12-21224.000000217.279999217.550003222.58999637181900.0221.143524
2020-12-22225.630005221.850006222.690002223.94000222612200.0222.484772
2020-12-23223.559998220.800003223.110001221.02000418699600.0219.583740
2020-12-24223.610001221.199997221.419998222.75000010550600.0221.302490
2020-12-28226.029999223.020004224.449997224.96000717933500.0223.498138
.....................
2021-06-16260.579987254.419998259.399994257.38000527220000.0256.888214
2021-06-17261.750000256.010010256.070007260.89999427565500.0260.401489
2021-06-18262.299988258.750000259.630005259.42999337202200.0258.934296
2021-06-21263.519989257.920013259.820007262.63000526696100.0262.128174
2021-06-22265.790009262.399994262.720001265.51001024694100.0265.002686
\n", "

126 rows × 6 columns

\n", "
" ], "text/plain": [ " High Low Open Close Volume \\\n", "Date \n", "2020-12-21 224.000000 217.279999 217.550003 222.589996 37181900.0 \n", "2020-12-22 225.630005 221.850006 222.690002 223.940002 22612200.0 \n", "2020-12-23 223.559998 220.800003 223.110001 221.020004 18699600.0 \n", "2020-12-24 223.610001 221.199997 221.419998 222.750000 10550600.0 \n", "2020-12-28 226.029999 223.020004 224.449997 224.960007 17933500.0 \n", "... ... ... ... ... ... \n", "2021-06-16 260.579987 254.419998 259.399994 257.380005 27220000.0 \n", "2021-06-17 261.750000 256.010010 256.070007 260.899994 27565500.0 \n", "2021-06-18 262.299988 258.750000 259.630005 259.429993 37202200.0 \n", "2021-06-21 263.519989 257.920013 259.820007 262.630005 26696100.0 \n", "2021-06-22 265.790009 262.399994 262.720001 265.510010 24694100.0 \n", "\n", " Adj Close \n", "Date \n", "2020-12-21 221.143524 \n", "2020-12-22 222.484772 \n", "2020-12-23 219.583740 \n", "2020-12-24 221.302490 \n", "2020-12-28 223.498138 \n", "... ... \n", "2021-06-16 256.888214 \n", "2021-06-17 260.401489 \n", "2021-06-18 258.934296 \n", "2021-06-21 262.128174 \n", "2021-06-22 265.002686 \n", "\n", "[126 rows x 6 columns]" ] }, "execution_count": 72, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stocks" ] }, { "cell_type": "code", "execution_count": 73, "id": "c0819ad8", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "High 2.617500e+02\n", "Low 2.560100e+02\n", "Open 2.560700e+02\n", "Close 2.609000e+02\n", "Volume 2.756550e+07\n", "Adj Close 2.604015e+02\n", "Name: 2021-06-17 00:00:00, dtype: float64" ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stocks.loc['2021-06-17']" ] }, { "cell_type": "code", "execution_count": 84, "id": "2470f50a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "High 2.492700e+02\n", "Low 2.458400e+02\n", "Open 2.481300e+02\n", "Close 2.473000e+02\n", "Volume 1.940670e+07\n", "Adj Close 2.468275e+02\n", "Name: 2021-06-02 00:00:00, dtype: float64" ] }, "execution_count": 84, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stocks.iloc[111]" ] }, { "cell_type": "code", "execution_count": 88, "id": "84ca2b96", "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", " \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", " \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", " \n", " \n", " \n", " \n", "
HighLowOpenCloseVolumeAdj Close
Date
2020-12-24223.610001221.199997221.419998222.75000010550600.0221.302490
2020-12-28226.029999223.020004224.449997224.96000717933500.0223.498138
2020-12-29227.179993223.580002226.309998224.14999417403200.0222.693390
2020-12-30225.630005221.470001225.229996221.67999320272300.0220.239456
2020-12-31223.000000219.679993221.699997222.41999820942100.0220.974640
2021-01-04223.000000214.809998222.529999217.69000237130100.0216.275375
2021-01-05218.520004215.699997217.259995217.89999423823000.0216.484009
2021-01-06216.490005211.940002212.169998212.25000035930700.0210.870728
2021-01-07219.339996213.710007214.039993218.28999327694500.0216.871460
2021-01-08220.580002217.029999218.679993219.61999522956200.0218.192841
2021-01-11218.910004216.729996218.470001217.49000523031300.0216.076675
2021-01-12217.100006213.320007216.500000214.92999323249300.0213.533310
2021-01-13216.759995213.929993214.020004216.33999620087100.0214.934143
2021-01-14217.460007212.740005215.910004213.02000429480800.0211.635742
2021-01-15214.509995212.029999213.520004212.64999431746500.0211.268112
\n", "
" ], "text/plain": [ " High Low Open Close Volume \\\n", "Date \n", "2020-12-24 223.610001 221.199997 221.419998 222.750000 10550600.0 \n", "2020-12-28 226.029999 223.020004 224.449997 224.960007 17933500.0 \n", "2020-12-29 227.179993 223.580002 226.309998 224.149994 17403200.0 \n", "2020-12-30 225.630005 221.470001 225.229996 221.679993 20272300.0 \n", "2020-12-31 223.000000 219.679993 221.699997 222.419998 20942100.0 \n", "2021-01-04 223.000000 214.809998 222.529999 217.690002 37130100.0 \n", "2021-01-05 218.520004 215.699997 217.259995 217.899994 23823000.0 \n", "2021-01-06 216.490005 211.940002 212.169998 212.250000 35930700.0 \n", "2021-01-07 219.339996 213.710007 214.039993 218.289993 27694500.0 \n", "2021-01-08 220.580002 217.029999 218.679993 219.619995 22956200.0 \n", "2021-01-11 218.910004 216.729996 218.470001 217.490005 23031300.0 \n", "2021-01-12 217.100006 213.320007 216.500000 214.929993 23249300.0 \n", "2021-01-13 216.759995 213.929993 214.020004 216.339996 20087100.0 \n", "2021-01-14 217.460007 212.740005 215.910004 213.020004 29480800.0 \n", "2021-01-15 214.509995 212.029999 213.520004 212.649994 31746500.0 \n", "\n", " Adj Close \n", "Date \n", "2020-12-24 221.302490 \n", "2020-12-28 223.498138 \n", "2020-12-29 222.693390 \n", "2020-12-30 220.239456 \n", "2020-12-31 220.974640 \n", "2021-01-04 216.275375 \n", "2021-01-05 216.484009 \n", "2021-01-06 210.870728 \n", "2021-01-07 216.871460 \n", "2021-01-08 218.192841 \n", "2021-01-11 216.076675 \n", "2021-01-12 213.533310 \n", "2021-01-13 214.934143 \n", "2021-01-14 211.635742 \n", "2021-01-15 211.268112 " ] }, "execution_count": 88, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stocks.loc['2020-12-24':'2021-01-18']" ] }, { "cell_type": "code", "execution_count": 89, "id": "dd19ce99", "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", " \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", "
HighLowOpenCloseVolumeAdj Close
Date
2020-12-30225.630005221.470001225.229996221.67999320272300.0220.239456
2020-12-31223.000000219.679993221.699997222.41999820942100.0220.974640
2021-01-04223.000000214.809998222.529999217.69000237130100.0216.275375
2021-01-05218.520004215.699997217.259995217.89999423823000.0216.484009
2021-01-06216.490005211.940002212.169998212.25000035930700.0210.870728
2021-01-07219.339996213.710007214.039993218.28999327694500.0216.871460
2021-01-08220.580002217.029999218.679993219.61999522956200.0218.192841
2021-01-11218.910004216.729996218.470001217.49000523031300.0216.076675
\n", "
" ], "text/plain": [ " High Low Open Close Volume \\\n", "Date \n", "2020-12-30 225.630005 221.470001 225.229996 221.679993 20272300.0 \n", "2020-12-31 223.000000 219.679993 221.699997 222.419998 20942100.0 \n", "2021-01-04 223.000000 214.809998 222.529999 217.690002 37130100.0 \n", "2021-01-05 218.520004 215.699997 217.259995 217.899994 23823000.0 \n", "2021-01-06 216.490005 211.940002 212.169998 212.250000 35930700.0 \n", "2021-01-07 219.339996 213.710007 214.039993 218.289993 27694500.0 \n", "2021-01-08 220.580002 217.029999 218.679993 219.619995 22956200.0 \n", "2021-01-11 218.910004 216.729996 218.470001 217.490005 23031300.0 \n", "\n", " Adj Close \n", "Date \n", "2020-12-30 220.239456 \n", "2020-12-31 220.974640 \n", "2021-01-04 216.275375 \n", "2021-01-05 216.484009 \n", "2021-01-06 210.870728 \n", "2021-01-07 216.871460 \n", "2021-01-08 218.192841 \n", "2021-01-11 216.076675 " ] }, "execution_count": 89, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stocks.truncate(before='2020-12-30', after='2021-01-11')" ] }, { "cell_type": "markdown", "id": "ee6b0220", "metadata": {}, "source": [ "## `Timedelta`" ] }, { "cell_type": "code", "execution_count": 97, "id": "f372b303", "metadata": {}, "outputs": [], "source": [ "timeA = pd.Timestamp('2021-01-01 10:26:00')\n", "timeB = pd.Timestamp('2020-01-15 10:33:00')" ] }, { "cell_type": "code", "execution_count": 98, "id": "84a5004e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Timedelta('351 days 23:53:00')" ] }, "execution_count": 98, "metadata": {}, "output_type": "execute_result" } ], "source": [ "timeA - timeB" ] }, { "cell_type": "code", "execution_count": 99, "id": "c831c431", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Timedelta('79 days 00:12:00')" ] }, "execution_count": 99, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.Timedelta(weeks=10, days=9, minutes=12)" ] }, { "cell_type": "code", "execution_count": 101, "id": "9b36f576", "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", " \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", " \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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
datetimecontinent_codecountry_namecountry_codestate/provincepopulationcity/towndistancelocation_description...geolocationhazard_typelandslide_typelandslide_sizetriggerstorm_nameinjuriesfatalitiessource_namesource_link
id
343/2/07NightNaNUnited StatesUSVirginia16000Cherry Hill3.40765Unknown...(38.600900000000003, -77.268199999999993)LandslideLandslideSmallRainNaNNaNNaNNBC 4 newshttp://www.nbc4.com/news/11186871/detail.html
423/22/07NaNNaNUnited StatesUSOhio17288New Philadelphia3.33522NaN...(40.517499999999998, -81.430499999999995)LandslideLandslideSmallRainNaNNaNNaNCanton Rep.comhttp://www.cantonrep.com/index.php?ID=345054&C...
564/6/07NaNNaNUnited StatesUSPennsylvania15930Wilkinsburg2.91977Urban area...(40.4377, -79.915999999999997)LandslideLandslideSmallRainNaNNaNNaNThe Pittsburgh Channel.comhttps://web.archive.org/web/20080423132842/htt...
594/14/07NaNNaNCanadaCAQuebec42786Châteauguay2.98682Above river...(45.322600000000001, -73.777100000000004)LandslideRiverbank collapseSmallRainNaNNaNNaNLe Soleilhttp://www.hebdos.net/lsc/edition162007/articl...
614/15/07NaNNaNUnited StatesUSKentucky6903Pikeville5.66542Below road...(37.432499999999997, -82.493099999999998)LandslideLandslideSmallDownpourNaNNaN0.0Matthew Crawford (KGS)NaN
\n", "

5 rows × 22 columns

\n", "
" ], "text/plain": [ " date time continent_code country_name country_code state/province \\\n", "id \n", "34 3/2/07 Night NaN United States US Virginia \n", "42 3/22/07 NaN NaN United States US Ohio \n", "56 4/6/07 NaN NaN United States US Pennsylvania \n", "59 4/14/07 NaN NaN Canada CA Quebec \n", "61 4/15/07 NaN NaN United States US Kentucky \n", "\n", " population city/town distance location_description ... \\\n", "id ... \n", "34 16000 Cherry Hill 3.40765 Unknown ... \n", "42 17288 New Philadelphia 3.33522 NaN ... \n", "56 15930 Wilkinsburg 2.91977 Urban area ... \n", "59 42786 Châteauguay 2.98682 Above river ... \n", "61 6903 Pikeville 5.66542 Below road ... \n", "\n", " geolocation hazard_type \\\n", "id \n", "34 (38.600900000000003, -77.268199999999993) Landslide \n", "42 (40.517499999999998, -81.430499999999995) Landslide \n", "56 (40.4377, -79.915999999999997) Landslide \n", "59 (45.322600000000001, -73.777100000000004) Landslide \n", "61 (37.432499999999997, -82.493099999999998) Landslide \n", "\n", " landslide_type landslide_size trigger storm_name injuries \\\n", "id \n", "34 Landslide Small Rain NaN NaN \n", "42 Landslide Small Rain NaN NaN \n", "56 Landslide Small Rain NaN NaN \n", "59 Riverbank collapse Small Rain NaN NaN \n", "61 Landslide Small Downpour NaN NaN \n", "\n", " fatalities source_name \\\n", "id \n", "34 NaN NBC 4 news \n", "42 NaN Canton Rep.com \n", "56 NaN The Pittsburgh Channel.com \n", "59 NaN Le Soleil \n", "61 0.0 Matthew Crawford (KGS) \n", "\n", " source_link \n", "id \n", "34 http://www.nbc4.com/news/11186871/detail.html \n", "42 http://www.cantonrep.com/index.php?ID=345054&C... \n", "56 https://web.archive.org/web/20080423132842/htt... \n", "59 http://www.hebdos.net/lsc/edition162007/articl... \n", "61 NaN \n", "\n", "[5 rows x 22 columns]" ] }, "execution_count": 101, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#parsing/converting dates into datetime from csv files\n", "df = pd.read_csv('catalog.csv', index_col='id')\n", "df.head()" ] }, { "cell_type": "code", "execution_count": 102, "id": "4e65053d", "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", " \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", " \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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
datetimecontinent_codecountry_namecountry_codestate/provincepopulationcity/towndistancelocation_description...geolocationhazard_typelandslide_typelandslide_sizetriggerstorm_nameinjuriesfatalitiessource_namesource_link
id
342007-03-02NightNaNUnited StatesUSVirginia16000Cherry Hill3.40765Unknown...(38.600900000000003, -77.268199999999993)LandslideLandslideSmallRainNaNNaNNaNNBC 4 newshttp://www.nbc4.com/news/11186871/detail.html
422007-03-22NaNNaNUnited StatesUSOhio17288New Philadelphia3.33522NaN...(40.517499999999998, -81.430499999999995)LandslideLandslideSmallRainNaNNaNNaNCanton Rep.comhttp://www.cantonrep.com/index.php?ID=345054&C...
562007-04-06NaNNaNUnited StatesUSPennsylvania15930Wilkinsburg2.91977Urban area...(40.4377, -79.915999999999997)LandslideLandslideSmallRainNaNNaNNaNThe Pittsburgh Channel.comhttps://web.archive.org/web/20080423132842/htt...
592007-04-14NaNNaNCanadaCAQuebec42786Châteauguay2.98682Above river...(45.322600000000001, -73.777100000000004)LandslideRiverbank collapseSmallRainNaNNaNNaNLe Soleilhttp://www.hebdos.net/lsc/edition162007/articl...
612007-04-15NaNNaNUnited StatesUSKentucky6903Pikeville5.66542Below road...(37.432499999999997, -82.493099999999998)LandslideLandslideSmallDownpourNaNNaN0.0Matthew Crawford (KGS)NaN
\n", "

5 rows × 22 columns

\n", "
" ], "text/plain": [ " date time continent_code country_name country_code \\\n", "id \n", "34 2007-03-02 Night NaN United States US \n", "42 2007-03-22 NaN NaN United States US \n", "56 2007-04-06 NaN NaN United States US \n", "59 2007-04-14 NaN NaN Canada CA \n", "61 2007-04-15 NaN NaN United States US \n", "\n", " state/province population city/town distance \\\n", "id \n", "34 Virginia 16000 Cherry Hill 3.40765 \n", "42 Ohio 17288 New Philadelphia 3.33522 \n", "56 Pennsylvania 15930 Wilkinsburg 2.91977 \n", "59 Quebec 42786 Châteauguay 2.98682 \n", "61 Kentucky 6903 Pikeville 5.66542 \n", "\n", " location_description ... geolocation \\\n", "id ... \n", "34 Unknown ... (38.600900000000003, -77.268199999999993) \n", "42 NaN ... (40.517499999999998, -81.430499999999995) \n", "56 Urban area ... (40.4377, -79.915999999999997) \n", "59 Above river ... (45.322600000000001, -73.777100000000004) \n", "61 Below road ... (37.432499999999997, -82.493099999999998) \n", "\n", " hazard_type landslide_type landslide_size trigger storm_name \\\n", "id \n", "34 Landslide Landslide Small Rain NaN \n", "42 Landslide Landslide Small Rain NaN \n", "56 Landslide Landslide Small Rain NaN \n", "59 Landslide Riverbank collapse Small Rain NaN \n", "61 Landslide Landslide Small Downpour NaN \n", "\n", " injuries fatalities source_name \\\n", "id \n", "34 NaN NaN NBC 4 news \n", "42 NaN NaN Canton Rep.com \n", "56 NaN NaN The Pittsburgh Channel.com \n", "59 NaN NaN Le Soleil \n", "61 NaN 0.0 Matthew Crawford (KGS) \n", "\n", " source_link \n", "id \n", "34 http://www.nbc4.com/news/11186871/detail.html \n", "42 http://www.cantonrep.com/index.php?ID=345054&C... \n", "56 https://web.archive.org/web/20080423132842/htt... \n", "59 http://www.hebdos.net/lsc/edition162007/articl... \n", "61 NaN \n", "\n", "[5 rows x 22 columns]" ] }, "execution_count": 102, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.read_csv('catalog.csv', index_col='id', parse_dates=['date'])\n", "df.head()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "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.9.2" } }, "nbformat": 4, "nbformat_minor": 5 }