Last active
January 14, 2016 08:44
-
-
Save anshulkgupta93/a9241352477dc67fff44 to your computer and use it in GitHub Desktop.
Datetime manipulation in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"year- 2008, month- 11, day- 29, hour- 0\n" | |
] | |
} | |
], | |
"source": [ | |
"from datetime import datetime\n", | |
"\n", | |
"date_object = datetime.strptime(\"29nov-2008\",\"%d%b-%Y\")\n", | |
"print \"year- {0}, month- {1}, day- {2}, hour- {3}\".format(date_object.year,date_object.month,date_object.day, \n", | |
" date_object.hour)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"year- 2005, month- 6, day- 1, hour- 13\n" | |
] | |
} | |
], | |
"source": [ | |
"date_object = datetime.strptime('Jun 1 2005 1:33PM', '%b %d %Y %I:%M%p')\n", | |
"print \"year- {0}, month- {1}, day- {2}, hour- {3}\".format(date_object.year,date_object.month,date_object.day, \n", | |
" date_object.hour)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"dobofclass=[\"1April2007\",\n", | |
" \"28th july 1984\",\n", | |
" \"05 May 1988\",\n", | |
" \"29nov-2008\",\n", | |
" \"1985-July-1\"]\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1984-07-28 00:00:00\n", | |
"1988-05-05 00:00:00\n", | |
"1985-07-01 00:00:00\n" | |
] | |
} | |
], | |
"source": [ | |
"from dateutil.parser import parse\n", | |
"not_parsed=[]\n", | |
"for index,dob in enumerate(dobofclass):\n", | |
" try:\n", | |
" dobofclass[index]=parse(dob)\n", | |
" print dobofclass[index]\n", | |
" except:\n", | |
" not_parsed.append(index)\n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1April2007\n", | |
"29nov-2008\n" | |
] | |
} | |
], | |
"source": [ | |
"for index in not_parsed:\n", | |
" print dobofclass[index]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 17, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"dobofclass[not_parsed[0]] = datetime.strptime(dobofclass[not_parsed[0]], '%d%B%Y')\n", | |
"dobofclass[not_parsed[1]] = datetime.strptime(dobofclass[not_parsed[1]], '%d%b-%Y')\n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 18, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"2007-04-01 00:00:00\n", | |
"1984-07-28 00:00:00\n", | |
"1988-05-05 00:00:00\n", | |
"2008-11-29 00:00:00\n", | |
"1985-07-01 00:00:00\n" | |
] | |
} | |
], | |
"source": [ | |
"for dob in dobofclass:\n", | |
" print dob" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 28, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"['23', '56', '78', '89']\n", | |
"[23, 56, 78, 89]\n" | |
] | |
} | |
], | |
"source": [ | |
"#Converting to Charachter\n", | |
"x=[23,56,78,89]\n", | |
"character=map(str ,x)\n", | |
"print character\n", | |
"numeric=map(int ,x)\n", | |
"print numeric" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 22, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Help on built-in function strptime:\n", | |
"\n", | |
"strptime(...)\n", | |
" string, format -> new datetime parsed from a string (like time.strptime()).\n", | |
"\n" | |
] | |
} | |
], | |
"source": [ | |
"help(datetime.strptime)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Directive\tMeaning\tExample\tNotes\n", | |
"%a\tWeekday as locale’s abbreviated name.\t\n", | |
"Sun, Mon, ..., Sat (en_US);\n", | |
"So, Mo, ..., Sa (de_DE)\n", | |
"(1)\n", | |
"%A\tWeekday as locale’s full name.\t\n", | |
"Sunday, Monday, ..., Saturday (en_US);\n", | |
"Sonntag, Montag, ..., Samstag (de_DE)\n", | |
"(1)\n", | |
"%w\tWeekday as a decimal number, where 0 is Sunday and 6 is Saturday.\t0, 1, ..., 6\t \n", | |
"%d\tDay of the month as a zero-padded decimal number.\t01, 02, ..., 31\t \n", | |
"%b\tMonth as locale’s abbreviated name.\t\n", | |
"Jan, Feb, ..., Dec (en_US);\n", | |
"Jan, Feb, ..., Dez (de_DE)\n", | |
"(1)\n", | |
"%B\tMonth as locale’s full name.\t\n", | |
"January, February, ..., December (en_US);\n", | |
"Januar, Februar, ..., Dezember (de_DE)\n", | |
"(1)\n", | |
"%m\tMonth as a zero-padded decimal number.\t01, 02, ..., 12\t \n", | |
"%y\tYear without century as a zero-padded decimal number.\t00, 01, ..., 99\t \n", | |
"%Y\tYear with century as a decimal number.\t1970, 1988, 2001, 2013\t \n", | |
"%H\tHour (24-hour clock) as a zero-padded decimal number.\t00, 01, ..., 23\t \n", | |
"%I\tHour (12-hour clock) as a zero-padded decimal number.\t01, 02, ..., 12\t \n", | |
"%p\tLocale’s equivalent of either AM or PM.\t\n", | |
"AM, PM (en_US);\n", | |
"am, pm (de_DE)\n", | |
"(1), (2)\n", | |
"%M\tMinute as a zero-padded decimal number.\t00, 01, ..., 59\t \n", | |
"%S\tSecond as a zero-padded decimal number.\t00, 01, ..., 59\t(3)\n", | |
"%f\tMicrosecond as a decimal number, zero-padded on the left.\t000000, 000001, ..., 999999\t(4)\n", | |
"%z\tUTC offset in the form +HHMM or -HHMM (empty string if the the object is naive).\t(empty), +0000, -0400, +1030\t(5)\n", | |
"%Z\tTime zone name (empty string if the object is naive).\t(empty), UTC, EST, CST\t \n", | |
"%j\tDay of the year as a zero-padded decimal number.\t001, 002, ..., 366\t \n", | |
"%U\tWeek number of the year (Sunday as the first day of the week) as a zero padded decimal number. All days in a new year preceding the first Sunday are considered to be in week 0.\t00, 01, ..., 53\t(6)\n", | |
"%W\tWeek number of the year (Monday as the first day of the week) as a decimal number. All days in a new year preceding the first Monday are considered to be in week 0.\t00, 01, ..., 53\t(6)\n", | |
"%c\tLocale’s appropriate date and time representation.\t\n", | |
"Tue Aug 16 21:30:00 1988 (en_US);\n", | |
"Di 16 Aug 21:30:00 1988 (de_DE)\n", | |
"(1)\n", | |
"%x\tLocale’s appropriate date representation.\t\n", | |
"08/16/88 (None);\n", | |
"08/16/1988 (en_US);\n", | |
"16.08.1988 (de_DE)\n", | |
"(1)\n", | |
"%X\tLocale’s appropriate time representation.\t\n", | |
"21:30:00 (en_US);\n", | |
"21:30:00 (de_DE)\n", | |
"(1)\n", | |
"%%\tA literal '%' character.\t%\t" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 31, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"0" | |
] | |
}, | |
"execution_count": 31, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"(dobofclass[not_parsed[0]]- dobofclass[not_parsed[1]]).days" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 48, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Difference of 8087 days between dob and current day \n" | |
] | |
} | |
], | |
"source": [ | |
"print \"Difference of {0} days between dob and current day \".format((datetime.today()-parse(\"23-November-1993\")).days)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 49, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Difference of 673 days between dob and current day \n" | |
] | |
} | |
], | |
"source": [ | |
"print \"Difference of {0} months between dob and current day \".format((datetime.today()-parse(\"23-November-1993\")).days/12)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 50, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Difference of 22 years between dob and current day \n" | |
] | |
} | |
], | |
"source": [ | |
"print \"Difference of {0} years between dob and current day \".format((datetime.today()-parse(\"23-November-1993\")).days/365)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"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.11" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment