Skip to content

Commit

Permalink
Draft. Pandas to_datetime
Browse files Browse the repository at this point in the history
  • Loading branch information
Dhaval Patel committed Jul 29, 2017
1 parent 67d821f commit 262be50
Showing 1 changed file with 298 additions and 0 deletions.
298 changes: 298 additions & 0 deletions pandas/17_ts_to_date_time/pandas_ts_to_date_time.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,298 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h1 style=\"color:blue\" align=\"center\">Pandas Time Series Analysis Tutorial: to_datetime</h1>"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"DatetimeIndex(['2017-01-05', '2017-01-06', '2016-01-07', '2017-08-01',\n",
" '2017-09-01'],\n",
" dtype='datetime64[ns]', freq=None)"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import pandas as pd\n",
"pd.to_datetime(['2017-01-05', 'Jan 6, 2017', '01/07/2016', '2017.08.01', '2017/09/01'])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3 style=\"color:purple\">Custom date time format</h3>"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Timestamp('2017-01-05 00:00:00')"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.to_datetime('2017$01$05', format='%Y$%m$%d')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Timestamp('2017-01-05 00:00:00')"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.to_datetime('20170105', format='%Y%m%d')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3 style=\"color:purple\">Handling invalid dates</h3>"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array(['2017-01-05', 'Jan 6, 2017', 'abc'], dtype=object)"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.to_datetime(['2017-01-05', 'Jan 6, 2017', 'abc'], errors='ignore')"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"DatetimeIndex(['2017-01-05', '2017-01-06', 'NaT'], dtype='datetime64[ns]', freq=None)"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.to_datetime(['2017-01-05', 'Jan 6, 2017', 'abc'], errors='coerce')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3 style=\"color:purple\">European style dates with day first</h3>"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Timestamp('2016-12-30 00:00:00')"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.to_datetime('30-12-2016')"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Timestamp('2016-01-05 00:00:00')"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.to_datetime('5-1-2016', dayfirst=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3 style=\"color:purple\">Epoch</h3>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Epoch or Unix time means number of seconds that have passed since Jan 1, 1970 00:00:00 UTC time**"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"Timestamp('2017-07-29 10:34:38')"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"current_epoch = 1501324478\n",
"pd.to_datetime(current_epoch, unit='s')"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"Timestamp('2017-07-29 10:34:38')"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.to_datetime(current_epoch*1000, unit='ms')"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"DatetimeIndex(['2017-07-29 10:34:38'], dtype='datetime64[ns]', freq=None)"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"t = pd.to_datetime([current_epoch], unit='s')\n",
"t"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1501324478000000000], dtype=int64)"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"t.view('int64')"
]
}
],
"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.6.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit 262be50

Please sign in to comment.