2015-01-25 02:18:54 +08:00
|
|
|
{
|
|
|
|
"metadata": {
|
|
|
|
"name": "",
|
2015-01-25 02:29:58 +08:00
|
|
|
"signature": "sha256:edcc4cf748f3ab5825f4dab84a30b30044d5590de3f7466f257b76edb77cb900"
|
2015-01-25 02:18:54 +08:00
|
|
|
},
|
|
|
|
"nbformat": 3,
|
|
|
|
"nbformat_minor": 0,
|
|
|
|
"worksheets": [
|
|
|
|
{
|
|
|
|
"cells": [
|
|
|
|
{
|
|
|
|
"cell_type": "markdown",
|
|
|
|
"metadata": {},
|
|
|
|
"source": [
|
|
|
|
"# Dates and Times"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"collapsed": false,
|
|
|
|
"input": [
|
|
|
|
"from datetime import datetime, date, time"
|
|
|
|
],
|
|
|
|
"language": "python",
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
2015-01-25 02:29:58 +08:00
|
|
|
"prompt_number": 1
|
2015-01-25 02:18:54 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"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": [],
|
2015-01-25 02:29:58 +08:00
|
|
|
"prompt_number": 2
|
2015-01-25 02:18:54 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "markdown",
|
|
|
|
"metadata": {},
|
|
|
|
"source": [
|
|
|
|
"## datetime Basics"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"collapsed": false,
|
|
|
|
"input": [
|
|
|
|
"dt = datetime(year, month, day, hour, minute, second)"
|
|
|
|
],
|
|
|
|
"language": "python",
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
2015-01-25 02:29:58 +08:00
|
|
|
"prompt_number": 3
|
2015-01-25 02:18:54 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"collapsed": false,
|
|
|
|
"input": [
|
|
|
|
"dt.hour, dt.minute, dt.second"
|
|
|
|
],
|
|
|
|
"language": "python",
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"metadata": {},
|
|
|
|
"output_type": "pyout",
|
2015-01-25 02:29:58 +08:00
|
|
|
"prompt_number": 4,
|
2015-01-25 02:18:54 +08:00
|
|
|
"text": [
|
|
|
|
"(7, 28, 15)"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
2015-01-25 02:29:58 +08:00
|
|
|
"prompt_number": 4
|
2015-01-25 02:18:54 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"collapsed": false,
|
|
|
|
"input": [
|
|
|
|
"# Extract the equivalent date object\n",
|
|
|
|
"dt.date()"
|
|
|
|
],
|
|
|
|
"language": "python",
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"metadata": {},
|
|
|
|
"output_type": "pyout",
|
2015-01-25 02:29:58 +08:00
|
|
|
"prompt_number": 5,
|
2015-01-25 02:18:54 +08:00
|
|
|
"text": [
|
|
|
|
"datetime.date(2015, 1, 20)"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
2015-01-25 02:29:58 +08:00
|
|
|
"prompt_number": 5
|
2015-01-25 02:18:54 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"collapsed": false,
|
|
|
|
"input": [
|
|
|
|
"# Extract the equivalent time object\n",
|
|
|
|
"dt.time()"
|
|
|
|
],
|
|
|
|
"language": "python",
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"metadata": {},
|
|
|
|
"output_type": "pyout",
|
2015-01-25 02:29:58 +08:00
|
|
|
"prompt_number": 6,
|
2015-01-25 02:18:54 +08:00
|
|
|
"text": [
|
|
|
|
"datetime.time(7, 28, 15)"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
2015-01-25 02:29:58 +08:00
|
|
|
"prompt_number": 6
|
2015-01-25 02:18:54 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"collapsed": false,
|
|
|
|
"input": [
|
|
|
|
"# When aggregating or grouping time series data, it is sometimes\n",
|
|
|
|
"# useful to replace fields of a series of datetimes such as zeroing\n",
|
|
|
|
"# out the minute and second fields.\n",
|
|
|
|
"dt.replace(minute=0, second=0)"
|
|
|
|
],
|
|
|
|
"language": "python",
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"metadata": {},
|
|
|
|
"output_type": "pyout",
|
2015-01-25 02:29:58 +08:00
|
|
|
"prompt_number": 7,
|
2015-01-25 02:18:54 +08:00
|
|
|
"text": [
|
|
|
|
"datetime.datetime(2015, 1, 20, 7, 0)"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
2015-01-25 02:29:58 +08:00
|
|
|
"prompt_number": 7
|
2015-01-25 02:18:54 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "markdown",
|
|
|
|
"metadata": {},
|
|
|
|
"source": [
|
|
|
|
"## strftime"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"collapsed": false,
|
|
|
|
"input": [
|
|
|
|
"# Format datetime string\n",
|
|
|
|
"dt.strftime('%m/%d/%Y %H:%M')"
|
|
|
|
],
|
|
|
|
"language": "python",
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"metadata": {},
|
|
|
|
"output_type": "pyout",
|
2015-01-25 02:29:58 +08:00
|
|
|
"prompt_number": 8,
|
2015-01-25 02:18:54 +08:00
|
|
|
"text": [
|
|
|
|
"'01/20/2015 07:28'"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
2015-01-25 02:29:58 +08:00
|
|
|
"prompt_number": 8
|
2015-01-25 02:18:54 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "markdown",
|
|
|
|
"metadata": {},
|
|
|
|
"source": [
|
|
|
|
"## strptime"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"collapsed": false,
|
|
|
|
"input": [
|
|
|
|
"# Convert a string into a datetime object\n",
|
|
|
|
"datetime.strptime('20150120', '%Y%m%d')"
|
|
|
|
],
|
|
|
|
"language": "python",
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"metadata": {},
|
|
|
|
"output_type": "pyout",
|
2015-01-25 02:29:58 +08:00
|
|
|
"prompt_number": 9,
|
2015-01-25 02:18:54 +08:00
|
|
|
"text": [
|
|
|
|
"datetime.datetime(2015, 1, 20, 0, 0)"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
2015-01-25 02:29:58 +08:00
|
|
|
"prompt_number": 9
|
2015-01-25 02:18:54 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "markdown",
|
|
|
|
"metadata": {},
|
|
|
|
"source": [
|
|
|
|
"## timedelta"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"collapsed": false,
|
|
|
|
"input": [
|
|
|
|
"# Get the current datetime\n",
|
|
|
|
"dt_now = datetime.now()"
|
|
|
|
],
|
|
|
|
"language": "python",
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
2015-01-25 02:29:58 +08:00
|
|
|
"prompt_number": 10
|
2015-01-25 02:18:54 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"collapsed": false,
|
|
|
|
"input": [
|
|
|
|
"# Subtracting two datetimes results in a timedelta\n",
|
|
|
|
"delta = dt_now - dt\n",
|
|
|
|
"delta"
|
|
|
|
],
|
|
|
|
"language": "python",
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"metadata": {},
|
|
|
|
"output_type": "pyout",
|
2015-01-25 02:29:58 +08:00
|
|
|
"prompt_number": 11,
|
2015-01-25 02:18:54 +08:00
|
|
|
"text": [
|
2015-01-25 02:29:58 +08:00
|
|
|
"datetime.timedelta(4, 21501, 259121)"
|
2015-01-25 02:18:54 +08:00
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
2015-01-25 02:29:58 +08:00
|
|
|
"prompt_number": 11
|
2015-01-25 02:18:54 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"collapsed": false,
|
|
|
|
"input": [
|
|
|
|
"# Adding a datetime and a timedelta results in a datetime\n",
|
|
|
|
"dt + delta"
|
|
|
|
],
|
|
|
|
"language": "python",
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"metadata": {},
|
|
|
|
"output_type": "pyout",
|
2015-01-25 02:29:58 +08:00
|
|
|
"prompt_number": 12,
|
2015-01-25 02:18:54 +08:00
|
|
|
"text": [
|
2015-01-25 02:29:58 +08:00
|
|
|
"datetime.datetime(2015, 1, 24, 13, 26, 36, 259121)"
|
2015-01-25 02:18:54 +08:00
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
2015-01-25 02:29:58 +08:00
|
|
|
"prompt_number": 12
|
2015-01-25 02:18:54 +08:00
|
|
|
}
|
|
|
|
],
|
|
|
|
"metadata": {}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|