Created
October 28, 2020 08:57
-
-
Save minrk/4f2c064ac1516666e93840b5f5230016 to your computer and use it in GitHub Desktop.
sorting-dates.ipynb
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", | |
"source": [ | |
"import datetime" | |
], | |
"outputs": [], | |
"execution_count": 1, | |
"metadata": { | |
"execution": { | |
"iopub.status.busy": "2020-10-28T08:55:29.598Z", | |
"iopub.execute_input": "2020-10-28T08:55:29.602Z", | |
"iopub.status.idle": "2020-10-28T08:55:29.611Z", | |
"shell.execute_reply": "2020-10-28T08:55:29.615Z" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"today = datetime.date.today()\n", | |
"today" | |
], | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 2, | |
"data": { | |
"text/plain": "datetime.date(2020, 10, 28)" | |
}, | |
"metadata": {} | |
} | |
], | |
"execution_count": 2, | |
"metadata": { | |
"execution": { | |
"iopub.status.busy": "2020-10-28T08:55:30.268Z", | |
"iopub.execute_input": "2020-10-28T08:55:30.273Z", | |
"iopub.status.idle": "2020-10-28T08:55:30.282Z", | |
"shell.execute_reply": "2020-10-28T08:55:30.286Z" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"now = datetime.datetime.now()\n", | |
"now" | |
], | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 3, | |
"data": { | |
"text/plain": "datetime.datetime(2020, 10, 28, 9, 55, 31, 64038)" | |
}, | |
"metadata": {} | |
} | |
], | |
"execution_count": 3, | |
"metadata": { | |
"execution": { | |
"iopub.status.busy": "2020-10-28T08:55:31.104Z", | |
"iopub.execute_input": "2020-10-28T08:55:31.113Z", | |
"iopub.status.idle": "2020-10-28T08:55:31.125Z", | |
"shell.execute_reply": "2020-10-28T08:55:31.130Z" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"sorted([now, today])" | |
], | |
"outputs": [ | |
{ | |
"output_type": "error", | |
"ename": "TypeError", | |
"evalue": "can't compare datetime.datetime to datetime.date", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-4-97aef87903c9>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0msorted\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mnow\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtoday\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[0;31mTypeError\u001b[0m: can't compare datetime.datetime to datetime.date" | |
] | |
} | |
], | |
"execution_count": 4, | |
"metadata": { | |
"execution": { | |
"iopub.status.busy": "2020-10-28T08:55:34.696Z", | |
"iopub.execute_input": "2020-10-28T08:55:34.700Z", | |
"iopub.status.idle": "2020-10-28T08:55:34.898Z", | |
"shell.execute_reply": "2020-10-28T08:55:34.909Z" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"def dates_before_datetimes(dt):\n", | |
" if isinstance(dt, datetime.datetime):\n", | |
" return (dt.date(), dt) # return a length-2 tuple with date as the first item, datetime as the second \n", | |
" elif isinstance(dt, datetime.date):\n", | |
" return (dt,) # return a length-1 tuple with date object\n", | |
" " | |
], | |
"outputs": [], | |
"execution_count": 5, | |
"metadata": { | |
"execution": { | |
"shell.execute_reply": "2020-10-28T08:55:35.792Z", | |
"iopub.status.busy": "2020-10-28T08:55:35.776Z", | |
"iopub.execute_input": "2020-10-28T08:55:35.781Z", | |
"iopub.status.idle": "2020-10-28T08:55:35.788Z" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"dates_before_datetimes(today)" | |
], | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 7, | |
"data": { | |
"text/plain": "(datetime.date(2020, 10, 28),)" | |
}, | |
"metadata": {} | |
} | |
], | |
"execution_count": 7, | |
"metadata": { | |
"collapsed": true, | |
"jupyter": { | |
"source_hidden": false, | |
"outputs_hidden": false | |
}, | |
"nteract": { | |
"transient": { | |
"deleting": false | |
} | |
}, | |
"execution": { | |
"iopub.status.busy": "2020-10-28T08:55:58.123Z", | |
"iopub.execute_input": "2020-10-28T08:55:58.130Z", | |
"iopub.status.idle": "2020-10-28T08:55:58.146Z", | |
"shell.execute_reply": "2020-10-28T08:55:58.152Z" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"dates_before_datetimes(now)" | |
], | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 8, | |
"data": { | |
"text/plain": "(datetime.date(2020, 10, 28),\n datetime.datetime(2020, 10, 28, 9, 55, 31, 64038))" | |
}, | |
"metadata": {} | |
} | |
], | |
"execution_count": 8, | |
"metadata": { | |
"collapsed": true, | |
"jupyter": { | |
"source_hidden": false, | |
"outputs_hidden": false | |
}, | |
"nteract": { | |
"transient": { | |
"deleting": false | |
} | |
}, | |
"execution": { | |
"iopub.status.busy": "2020-10-28T08:56:06.291Z", | |
"iopub.execute_input": "2020-10-28T08:56:06.298Z", | |
"iopub.status.idle": "2020-10-28T08:56:06.311Z", | |
"shell.execute_reply": "2020-10-28T08:56:06.320Z" | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"sorted([now, today], key=dates_before_datetimes)" | |
], | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 6, | |
"data": { | |
"text/plain": "[datetime.date(2020, 10, 28),\n datetime.datetime(2020, 10, 28, 9, 55, 31, 64038)]" | |
}, | |
"metadata": {} | |
} | |
], | |
"execution_count": 6, | |
"metadata": { | |
"execution": { | |
"shell.execute_reply": "2020-10-28T08:55:36.946Z", | |
"iopub.status.busy": "2020-10-28T08:55:36.921Z", | |
"iopub.execute_input": "2020-10-28T08:55:36.927Z", | |
"iopub.status.idle": "2020-10-28T08:55:36.941Z" | |
} | |
} | |
} | |
], | |
"metadata": { | |
"language_info": { | |
"name": "python", | |
"version": "3.8.5", | |
"mimetype": "text/x-python", | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"pygments_lexer": "ipython3", | |
"nbconvert_exporter": "python", | |
"file_extension": ".py" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment