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

360 lines
6.0 KiB
Python
Raw Normal View History

{
2015-05-14 18:55:14 +08:00
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://github.com/donnemartin/data-science-ipython-notebooks)."
]
},
{
2015-05-14 18:55:14 +08:00
"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": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from datetime import datetime, date, time"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"year = 2015\n",
"month = 1\n",
"day = 20\n",
"hour = 7\n",
"minute = 28\n",
"second = 15"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"dt = datetime(year, month, day, hour, minute, second)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
2015-05-14 18:55:14 +08:00
"data": {
"text/plain": [
"(7, 28, 15)"
]
},
"execution_count": 4,
"metadata": {},
2015-05-14 18:55:14 +08:00
"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": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
2015-05-14 18:55:14 +08:00
"data": {
"text/plain": [
"datetime.date(2015, 1, 20)"
]
},
"execution_count": 5,
"metadata": {},
2015-05-14 18:55:14 +08:00
"output_type": "execute_result"
}
],
"source": [
"dt.date()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Extract the equivalent time object:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
2015-05-14 18:55:14 +08:00
"data": {
"text/plain": [
"datetime.time(7, 28, 15)"
]
},
"execution_count": 6,
"metadata": {},
2015-05-14 18:55:14 +08:00
"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": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
2015-05-14 18:55:14 +08:00
"data": {
"text/plain": [
"datetime.datetime(2015, 1, 20, 7, 0)"
]
},
"execution_count": 7,
"metadata": {},
2015-05-14 18:55:14 +08:00
"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": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
2015-05-14 18:55:14 +08:00
"data": {
"text/plain": [
"'01/20/2015 07:28'"
]
},
"execution_count": 8,
"metadata": {},
2015-05-14 18:55:14 +08:00
"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": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
2015-05-14 18:55:14 +08:00
"data": {
"text/plain": [
"datetime.datetime(2015, 1, 20, 0, 0)"
]
},
"execution_count": 9,
"metadata": {},
2015-05-14 18:55:14 +08:00
"output_type": "execute_result"
}
],
"source": [
"datetime.strptime('20150120', '%Y%m%d')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## timedelta"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Get the current datetime:"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"dt_now = datetime.now()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Subtract two datetime fields to create a timedelta:"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
2015-05-14 18:55:14 +08:00
"data": {
"text/plain": [
"datetime.timedelta(6, 40171, 885211)"
]
},
"execution_count": 11,
"metadata": {},
2015-05-14 18:55:14 +08:00
"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": 12,
"metadata": {
"collapsed": false
},
"outputs": [
{
2015-05-14 18:55:14 +08:00
"data": {
"text/plain": [
"datetime.datetime(2015, 1, 26, 18, 37, 46, 885211)"
]
},
"execution_count": 12,
"metadata": {},
2015-05-14 18:55:14 +08:00
"output_type": "execute_result"
}
],
2015-05-14 18:55:14 +08:00
"source": [
"dt + delta"
]
}
2015-05-14 18:55:14 +08:00
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.10"
2015-05-14 18:55:14 +08:00
}
},
"nbformat": 4,
"nbformat_minor": 0
}