2015-01-25 02:18:54 +08:00
{
2015-05-14 18:55:14 +08:00
" cells " : [
2015-06-19 09:07:36 +08:00
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
2015-11-01 19:44:00 +08:00
" 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-06-19 09:07:36 +08:00
]
} ,
2015-01-25 02:18:54 +08:00
{
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-01-25 02:18:54 +08:00
{
2015-05-14 18:55:14 +08:00
" data " : {
" text/plain " : [
" (7, 28, 15) "
]
} ,
" execution_count " : 4 ,
2015-01-25 02:18:54 +08:00
" 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-01-25 02:18:54 +08:00
{
2015-05-14 18:55:14 +08:00
" data " : {
" text/plain " : [
" datetime.date(2015, 1, 20) "
]
} ,
" execution_count " : 5 ,
2015-01-25 02:18:54 +08:00
" 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-01-25 22:22:23 +08:00
{
2015-05-14 18:55:14 +08:00
" data " : {
" text/plain " : [
" datetime.time(7, 28, 15) "
]
} ,
" execution_count " : 6 ,
2015-01-25 22:22:23 +08:00
" 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-01-25 02:18:54 +08:00
{
2015-05-14 18:55:14 +08:00
" data " : {
" text/plain " : [
" datetime.datetime(2015, 1, 20, 7, 0) "
]
} ,
" execution_count " : 7 ,
2015-01-25 02:18:54 +08:00
" 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-01-25 22:22:23 +08:00
{
2015-05-14 18:55:14 +08:00
" data " : {
" text/plain " : [
" ' 01/20/2015 07:28 ' "
]
} ,
" execution_count " : 8 ,
2015-01-25 22:22:23 +08:00
" 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-01-25 02:18:54 +08:00
{
2015-05-14 18:55:14 +08:00
" data " : {
" text/plain " : [
" datetime.datetime(2015, 1, 20, 0, 0) "
]
} ,
" execution_count " : 9 ,
2015-01-25 02:18:54 +08:00
" 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-01-25 22:22:23 +08:00
{
2015-05-14 18:55:14 +08:00
" data " : {
" text/plain " : [
" datetime.timedelta(6, 40171, 885211) "
]
} ,
" execution_count " : 11 ,
2015-01-25 22:22:23 +08:00
" 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-01-25 02:18:54 +08:00
{
2015-05-14 18:55:14 +08:00
" data " : {
" text/plain " : [
" datetime.datetime(2015, 1, 26, 18, 37, 46, 885211) "
]
} ,
" execution_count " : 12 ,
2015-01-25 02:18:54 +08:00
" metadata " : { } ,
2015-05-14 18:55:14 +08:00
" output_type " : " execute_result "
2015-01-25 02:18:54 +08:00
}
] ,
2015-05-14 18:55:14 +08:00
" source " : [
" dt + delta "
]
2015-01-25 02:18:54 +08:00
}
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 " ,
2015-06-19 09:07:36 +08:00
" version " : " 2.7.10 "
2015-05-14 18:55:14 +08:00
}
} ,
" nbformat " : 4 ,
" nbformat_minor " : 0
}