data-science-ipython-notebooks/python-core/datetime.ipynb

326 lines
6.1 KiB
Plaintext

{
"metadata": {
"name": "",
"signature": "sha256:4dcac17430f8456d8b357f87e754eeda54837fbcd49ef8901953fec4b2dcab52"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"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",
"collapsed": false,
"input": [
"from datetime import datetime, date, time"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"year = 2015\n",
"month = 1\n",
"day = 20\n",
"hour = 7\n",
"minute = 28\n",
"second = 15"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"dt = datetime(year, month, day, hour, minute, second)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"dt.hour, dt.minute, dt.second"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 4,
"text": [
"(7, 28, 15)"
]
}
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Extract the equivalent date object:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"dt.date()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 5,
"text": [
"datetime.date(2015, 1, 20)"
]
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Extract the equivalent time object:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"dt.time()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 6,
"text": [
"datetime.time(7, 28, 15)"
]
}
],
"prompt_number": 6
},
{
"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",
"collapsed": false,
"input": [
"dt.replace(minute=0, second=0)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 7,
"text": [
"datetime.datetime(2015, 1, 20, 7, 0)"
]
}
],
"prompt_number": 7
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## strftime"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Format a datetime string:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"dt.strftime('%m/%d/%Y %H:%M')"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 8,
"text": [
"'01/20/2015 07:28'"
]
}
],
"prompt_number": 8
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## strptime"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Convert a string into a datetime object:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"datetime.strptime('20150120', '%Y%m%d')"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 9,
"text": [
"datetime.datetime(2015, 1, 20, 0, 0)"
]
}
],
"prompt_number": 9
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## timedelta"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Get the current datetime:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"dt_now = datetime.now()"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 10
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Subtract two datetime fields to create a timedelta:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"delta = dt_now - dt\n",
"delta"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 11,
"text": [
"datetime.timedelta(6, 40171, 885211)"
]
}
],
"prompt_number": 11
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Add a datetime and a timedelta to get a new datetime:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"dt + delta"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 12,
"text": [
"datetime.datetime(2015, 1, 26, 18, 37, 46, 885211)"
]
}
],
"prompt_number": 12
}
],
"metadata": {}
}
]
}