Skip to content

Instantly share code, notes, and snippets.

@yngtodd
Created May 30, 2019 19:05
Show Gist options
  • Save yngtodd/e2889eceb4e67b0ed72eebad7c0a66d7 to your computer and use it in GitHub Desktop.
Save yngtodd/e2889eceb4e67b0ed72eebad7c0a66d7 to your computer and use it in GitHub Desktop.
datetime.datetime_to_datetime.time
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"import datetime\n",
"import pandas as pd\n",
"\n",
"from typing import List"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [],
"source": [
"def get_times(dates: List) -> List:\n",
" \"\"\"Get datetime.time from datetime.datetime\"\"\"\n",
" return [x.time() for x in dates]"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [],
"source": [
"# Create an example list of datetime.datetime objects\n",
"base = datetime.datetime.today()\n",
"date_list = [base - datetime.timedelta(days=x) for x in range(10)]"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[datetime.datetime(2019, 5, 30, 15, 2, 13, 403944),\n",
" datetime.datetime(2019, 5, 29, 15, 2, 13, 403944),\n",
" datetime.datetime(2019, 5, 28, 15, 2, 13, 403944),\n",
" datetime.datetime(2019, 5, 27, 15, 2, 13, 403944),\n",
" datetime.datetime(2019, 5, 26, 15, 2, 13, 403944),\n",
" datetime.datetime(2019, 5, 25, 15, 2, 13, 403944),\n",
" datetime.datetime(2019, 5, 24, 15, 2, 13, 403944),\n",
" datetime.datetime(2019, 5, 23, 15, 2, 13, 403944),\n",
" datetime.datetime(2019, 5, 22, 15, 2, 13, 403944),\n",
" datetime.datetime(2019, 5, 21, 15, 2, 13, 403944)]"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"date_list"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"datetime.datetime"
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(date_list[0])"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [],
"source": [
"times = get_times(date_list)"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[datetime.time(15, 2, 13, 403944),\n",
" datetime.time(15, 2, 13, 403944),\n",
" datetime.time(15, 2, 13, 403944),\n",
" datetime.time(15, 2, 13, 403944),\n",
" datetime.time(15, 2, 13, 403944),\n",
" datetime.time(15, 2, 13, 403944),\n",
" datetime.time(15, 2, 13, 403944),\n",
" datetime.time(15, 2, 13, 403944),\n",
" datetime.time(15, 2, 13, 403944),\n",
" datetime.time(15, 2, 13, 403944)]"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"times"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"datetime.time"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(times[0])"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment