{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Dates and Times" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Basics\n", "* strftime\n", "* strptime\n", "* timedelta" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Basics" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from datetime import datetime, date, time" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "year = 2017\n", "month = 8\n", "day = 29\n", "hour =18\n", "minute = 4\n", "second = 15" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "dt = datetime(year, month, day, hour, minute, second)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(18, 4, 15)" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dt.hour, dt.minute, dt.second" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Extract the equivalent date object:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "datetime.date(2017, 8, 29)" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dt.date()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Extract the equivalent time object:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "datetime.time(18, 4, 15)" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dt.time()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When aggregating or grouping time series data, it is sometimes useful to replace fields of a series of datetimes such as zeroing out the minute and second fields:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "datetime.datetime(2017, 8, 29, 18, 0)" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dt.replace(minute=0, second=0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## strftime" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Format a datetime string:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'08/29/2017 18:04'" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dt.strftime('%m/%d/%Y %H:%M')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## strptime" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Convert a string into a datetime object:" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "datetime.datetime(2017, 8, 29, 0, 0)" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "datetime.strptime('20170829', '%Y%m%d')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## timedelta" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Get the current datetime:" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": true }, "outputs": [], "source": [ "dt_now = datetime.now()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Subtract two datetime fields to create a timedelta:" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "datetime.timedelta(0, 8, 836993)" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "delta = dt_now - dt\n", "delta" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Add a datetime and a timedelta to get a new datetime:" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "datetime.datetime(2017, 8, 29, 18, 4, 23, 836993)" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dt + delta" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "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": 1 }