{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Python: Date and Time Data\n", "\n", "The standard library for manipulating dates and times is [`datetime`](https://docs.python.org/3/library/datetime.html). We will use the more full-featured [`arrow`](https://github.com/crsmithdev/arrow/) library to illustrate concepts for working with date/time data, but the concepts are generally also applicable to the use of `datetime`.\n", "\n", "Note that `pandas` also has a core set of functions for working with time series data.\n", "\n", "- Documentation for [Arrow](http://arrow.readthedocs.io/en/latest/)\n", "- Documentation for [datetime](https://pymotw.com/2/datetime/)\n", "- Documentation for [Pandas time series functions](https://pandas.pydata.org/pandas-docs/stable/timeseries.html)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import arrow" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from datetime import datetime" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A readable display for date and time" ] }, { "cell_type": "code", "execution_count": 177, "metadata": { "collapsed": true }, "outputs": [], "source": [ "fmt = 'ddd hh:mm:ss A, DD-MMM-YYYY'" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "## Creation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Local time now" ] }, { "cell_type": "code", "execution_count": 178, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 178, "metadata": {}, "output_type": "execute_result" } ], "source": [ "local = arrow.now()\n", "local" ] }, { "cell_type": "code", "execution_count": 179, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'2017-07-01 14:56:21-04:00'" ] }, "execution_count": 179, "metadata": {}, "output_type": "execute_result" } ], "source": [ "local.format()" ] }, { "cell_type": "code", "execution_count": 180, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Sat 02:56:21 PM, 01-Jul-2017'" ] }, "execution_count": 180, "metadata": {}, "output_type": "execute_result" } ], "source": [ "local.format(fmt)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### UTC\n", "\n", "Coordinated Universal Time (UTC) is the basis for civil time today. This 24-hour time standard is kept using highly precise atomic clocks combined with the Earth's rotation. In practice, UTC shares the same current time as Greenwich Mean Time (GMT). " ] }, { "cell_type": "code", "execution_count": 181, "metadata": { "collapsed": true }, "outputs": [], "source": [ "utc = arrow.utcnow()" ] }, { "cell_type": "code", "execution_count": 182, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'2017-07-01 18:56:22+00:00'" ] }, "execution_count": 182, "metadata": {}, "output_type": "execute_result" } ], "source": [ "utc.format()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Creation from timestamps" ] }, { "cell_type": "code", "execution_count": 187, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1498935385.06168" ] }, "execution_count": 187, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import time\n", "\n", "ts = time.time()\n", "ts" ] }, { "cell_type": "code", "execution_count": 188, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Sat 06:56:25 PM, 01-Jul-2017'" ] }, "execution_count": 188, "metadata": {}, "output_type": "execute_result" } ], "source": [ "arrow.get(ts).format(fmt)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Creation from strings" ] }, { "cell_type": "code", "execution_count": 189, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Sunday, 21 Oct 1956'" ] }, "execution_count": 189, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fisher_birthday = arrow.get('Fisher was born on October 21, 1956', 'MMMM DD, YYYY')\n", "fisher_birthday.format('dddd, DD MMM YYYY')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### From Unix date command" ] }, { "cell_type": "code", "execution_count": 190, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['Sat Jul 1 14:56:26 EDT 2017']" ] }, "execution_count": 190, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ts = ! date\n", "ts" ] }, { "cell_type": "code", "execution_count": 191, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Sat 02:56:26 PM, 01-Jul-2017'" ] }, "execution_count": 191, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tt = arrow.get(ts[0], 'ddd MMM D hh:mm:ss ZZZ YYYY')\n", "tt.format(fmt)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Creation from values" ] }, { "cell_type": "code", "execution_count": 192, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Sun 11:59:59 PM, 24-Dec-2017'" ] }, "execution_count": 192, "metadata": {}, "output_type": "execute_result" } ], "source": [ "santa_is_coming = arrow.get(2017, 12, 24, 23, 59, 59)\n", "santa_is_coming.format(fmt)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Conversion between time zones" ] }, { "cell_type": "code", "execution_count": 183, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Sat 02:56:22 PM, 01-Jul-2017'" ] }, "execution_count": 183, "metadata": {}, "output_type": "execute_result" } ], "source": [ "utc.to('local').format(fmt)" ] }, { "cell_type": "code", "execution_count": 184, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Sat 08:56:22 AM, 01-Jul-2017'" ] }, "execution_count": 184, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hawaii = utc.to('US/Hawaii')\n", "hawaii.format(fmt)" ] }, { "cell_type": "code", "execution_count": 185, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Sun 02:56:22 AM, 02-Jul-2017'" ] }, "execution_count": 185, "metadata": {}, "output_type": "execute_result" } ], "source": [ "singapore = utc.to('Asia/Singapore')\n", "singapore.format(fmt)" ] }, { "cell_type": "code", "execution_count": 186, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Sat 08:56:22 PM, 01-Jul-2017'" ] }, "execution_count": 186, "metadata": {}, "output_type": "execute_result" } ], "source": [ "paris = utc.to('Europe/Paris')\n", "paris.format(fmt)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Shifting" ] }, { "cell_type": "code", "execution_count": 193, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Sat 02:56:28 PM, 01-Jul-2017'" ] }, "execution_count": 193, "metadata": {}, "output_type": "execute_result" } ], "source": [ "current = arrow.now()\n", "current.format(fmt)" ] }, { "cell_type": "code", "execution_count": 213, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Sat 07:59:32 PM, 08-Jul-2017'" ] }, "execution_count": 213, "metadata": {}, "output_type": "execute_result" } ], "source": [ "homework_due = current.shift(weeks=1, hours=5)\n", "homework_due.format(fmt)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Replacing" ] }, { "cell_type": "code", "execution_count": 195, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Fri 02:56:28 PM, 01-Jul-2016'" ] }, "execution_count": 195, "metadata": {}, "output_type": "execute_result" } ], "source": [ "last_year = current.replace(year=2016)\n", "last_year.format(fmt)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Time differences" ] }, { "cell_type": "code", "execution_count": 196, "metadata": {}, "outputs": [], "source": [ "past = current.replace(hours=-4)" ] }, { "cell_type": "code", "execution_count": 214, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "datetime.timedelta(0, 14584, 145507)" ] }, "execution_count": 214, "metadata": {}, "output_type": "execute_result" } ], "source": [ "current - past" ] }, { "cell_type": "code", "execution_count": 197, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4:00:00\n" ] } ], "source": [ "print(current - past)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Ranges and iteration" ] }, { "cell_type": "code", "execution_count": 198, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(,\n", " )" ] }, "execution_count": 198, "metadata": {}, "output_type": "execute_result" } ], "source": [ "current.span('day')" ] }, { "cell_type": "code", "execution_count": 199, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['Sat 12:00:00 AM, 01-Jul-2017', 'Sat 11:59:59 PM, 01-Jul-2017']" ] }, "execution_count": 199, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[t.format(fmt) for t in current.span('day')]" ] }, { "cell_type": "code", "execution_count": 200, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['Sat 12:00:00 AM, 01-Jul-2017', 'Mon 11:59:59 PM, 31-Jul-2017']" ] }, "execution_count": 200, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[t.format(fmt) for t in current.span('month')]" ] }, { "cell_type": "code", "execution_count": 202, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Mon 12:00:00 AM, 26-Jun-2017\n", "Tue 12:00:00 AM, 27-Jun-2017\n", "Wed 12:00:00 AM, 28-Jun-2017\n", "Thu 12:00:00 AM, 29-Jun-2017\n", "Fri 12:00:00 AM, 30-Jun-2017\n", "Sat 12:00:00 AM, 01-Jul-2017\n", "Sun 12:00:00 AM, 02-Jul-2017\n" ] } ], "source": [ "for t in arrow.Arrow.range('day', *current.span('week')):\n", " print(t.format(fmt))" ] }, { "cell_type": "code", "execution_count": 203, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Sat 02:58:06 PM, 01-Jul-2017\n", "Sat 03:58:06 PM, 01-Jul-2017\n", "Sat 04:58:06 PM, 01-Jul-2017\n", "Sat 05:58:06 PM, 01-Jul-2017\n", "Sat 06:58:06 PM, 01-Jul-2017\n", "Sat 07:58:06 PM, 01-Jul-2017\n", "Sat 08:58:06 PM, 01-Jul-2017\n" ] } ], "source": [ "start = arrow.now()\n", "end = start.replace(hours=6)\n", "for t in arrow.Arrow.range('hour', start, end):\n", " print(t.format(fmt))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Generating readable strings\n", "\n", "Show a human readable difference between two times. By default, the difference is from the current time." ] }, { "cell_type": "code", "execution_count": 207, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'4 minutes ago'" ] }, "execution_count": 207, "metadata": {}, "output_type": "execute_result" } ], "source": [ "local.humanize()" ] }, { "cell_type": "code", "execution_count": 210, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'in 6 days'" ] }, "execution_count": 210, "metadata": {}, "output_type": "execute_result" } ], "source": [ "homework_due.humanize(arrow.now())" ] }, { "cell_type": "code", "execution_count": 211, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'6天后'" ] }, "execution_count": 211, "metadata": {}, "output_type": "execute_result" } ], "source": [ "homework_due.humanize(arrow.now(), locale='zh')" ] }, { "cell_type": "code", "execution_count": 212, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'dans 6 jours'" ] }, "execution_count": 212, "metadata": {}, "output_type": "execute_result" } ], "source": [ "homework_due.humanize(arrow.now(), locale='fr')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Conversion between arrow and datetime" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Arrow" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "t1 = arrow.now()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### arrow -> datetime" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "datetime.datetime(2017, 7, 1, 13, 40, 57, 541580, tzinfo=tzlocal())" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t1.datetime" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1498930857" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t1.timestamp" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Compare with direct datetime call" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "t2 = datetime.now()" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "datetime.datetime(2017, 7, 1, 13, 40, 57, 571316)" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Full compatibility with datetime" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1498930857.571316" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "datetime.timestamp(t2)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1498930857.54158" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "datetime.timestamp(t1.datetime)" ] } ], "metadata": { "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.6.1" } }, "nbformat": 4, "nbformat_minor": 2 }