{"cells": [{"cell_type": "markdown", "metadata": {"tags": ["module-prog", "module-dsml"]}, "source": ["(Pandas_date_time)=\n", "# Creating date-time stamps\n", "[Programming for Geoscientists](module-prog) [Data Science and Machine Learning for Geoscientists](module-dsml) \n", "``` {index} Pandas: date-time stamps\n", "```\n", "Let's create columns with hours, minutes and seconds from utc_time column (contains strings) in format:\n", "\n", " HH:MM:SS\n", " \n", "by splitting the strings with a colon and creating float columns.\n", "\n", "Let's load New Zealand earthquake data:"]}, {"cell_type": "code", "execution_count": 5, "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", "
yearmonthdayutc_timemaglatlondepth_kmregioniris_idtimestamp
0200971509:22:317.8-45.8339166.636320.9OFF W. COAST OF S. ISLAND, N.Z.28716981247649751
12016111311:02:597.8-42.7245173.064722.0SOUTH ISLAND, NEW ZEALAND51977221479034979
2200382112:12:477.2-45.0875167.08926.8SOUTH ISLAND, NEW ZEALAND16280071061467967
3200182106:52:067.1-36.8010-179.723033.5EAST OF NORTH ISLAND, N.Z.1169374998376726
\n", "
"], "text/plain": [" year month day utc_time mag lat lon depth_km \\\n", "0 2009 7 15 09:22:31 7.8 -45.8339 166.6363 20.9 \n", "1 2016 11 13 11:02:59 7.8 -42.7245 173.0647 22.0 \n", "2 2003 8 21 12:12:47 7.2 -45.0875 167.0892 6.8 \n", "3 2001 8 21 06:52:06 7.1 -36.8010 -179.7230 33.5 \n", "\n", " region iris_id timestamp \n", "0 OFF W. COAST OF S. ISLAND, N.Z. 2871698 1247649751 \n", "1 SOUTH ISLAND, NEW ZEALAND 5197722 1479034979 \n", "2 SOUTH ISLAND, NEW ZEALAND 1628007 1061467967 \n", "3 EAST OF NORTH ISLAND, N.Z. 1169374 998376726 "]}, "execution_count": 5, "metadata": {}, "output_type": "execute_result"}], "source": ["import pandas as pd\n", "\n", "nz_eqs = pd.read_csv(\"../../geosciences/data/nz_largest_eq_since_1970.csv\")\n", "nz_eqs.head(4)"]}, {"cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": ["nz_eqs[\"hour\"] = nz_eqs[\"utc_time\"].str.split(':').str.get(0).astype(float)\n", "nz_eqs[\"minute\"] = nz_eqs[\"utc_time\"].str.split(':').str.get(1).astype(float)\n", "nz_eqs[\"second\"] = nz_eqs[\"utc_time\"].str.split(':').str.get(2).astype(float)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["With to_datetime() function, we can easily create date time stamp used by pandas:"]}, {"cell_type": "code", "execution_count": 7, "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", "
yearmonthdayutc_timemaglatlondepth_kmregioniris_idtimestamphourminuteseconddatetime
0200971509:22:317.8-45.8339166.636320.9OFF W. COAST OF S. ISLAND, N.Z.287169812476497519.022.031.02009-07-15 09:22:31
12016111311:02:597.8-42.7245173.064722.0SOUTH ISLAND, NEW ZEALAND5197722147903497911.02.059.02016-11-13 11:02:59
2200382112:12:477.2-45.0875167.08926.8SOUTH ISLAND, NEW ZEALAND1628007106146796712.012.047.02003-08-21 12:12:47
3200182106:52:067.1-36.8010-179.723033.5EAST OF NORTH ISLAND, N.Z.11693749983767266.052.06.02001-08-21 06:52:06
\n", "
"], "text/plain": [" year month day utc_time mag lat lon depth_km \\\n", "0 2009 7 15 09:22:31 7.8 -45.8339 166.6363 20.9 \n", "1 2016 11 13 11:02:59 7.8 -42.7245 173.0647 22.0 \n", "2 2003 8 21 12:12:47 7.2 -45.0875 167.0892 6.8 \n", "3 2001 8 21 06:52:06 7.1 -36.8010 -179.7230 33.5 \n", "\n", " region iris_id timestamp hour minute second \\\n", "0 OFF W. COAST OF S. ISLAND, N.Z. 2871698 1247649751 9.0 22.0 31.0 \n", "1 SOUTH ISLAND, NEW ZEALAND 5197722 1479034979 11.0 2.0 59.0 \n", "2 SOUTH ISLAND, NEW ZEALAND 1628007 1061467967 12.0 12.0 47.0 \n", "3 EAST OF NORTH ISLAND, N.Z. 1169374 998376726 6.0 52.0 6.0 \n", "\n", " datetime \n", "0 2009-07-15 09:22:31 \n", "1 2016-11-13 11:02:59 \n", "2 2003-08-21 12:12:47 \n", "3 2001-08-21 06:52:06 "]}, "execution_count": 7, "metadata": {}, "output_type": "execute_result"}], "source": ["nz_eqs[\"datetime\"] = pd.to_datetime(nz_eqs[['year', 'month', 'day', 'hour', 'minute', 'second']])\n", "nz_eqs.head(4)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["The datetime column is not yet recognised as index so we can simply use:"]}, {"cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": ["nz_eqs = nz_eqs.set_index('datetime')"]}, {"cell_type": "markdown", "metadata": {}, "source": ["This timestamp will be useful for plotting data."]}, {"cell_type": "markdown", "metadata": {}, "source": ["# References\n", "The notebook was compiled based on:\n", "* [Pandas official Getting Started tutorials](https://pandas.pydata.org/docs/getting_started/index.html#getting-started)\n", "* [Kaggle tutorial](https://www.kaggle.com/learn/pandas)"]}], "metadata": {"celltoolbar": "Tags", "kernelspec": {"display_name": "Python 3", "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.8.8"}}, "nbformat": 4, "nbformat_minor": 2}