Skip to content

Instantly share code, notes, and snippets.

@amirziai
Created January 24, 2018 19:05
Show Gist options
  • Select an option

  • Save amirziai/3d48b9c00835adc584d149bf70bc4abc to your computer and use it in GitHub Desktop.

Select an option

Save amirziai/3d48b9c00835adc584d149bf70bc4abc to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from datetime import datetime\n",
"import pandas as pd\n",
"%matplotlib inline\n",
"import matplotlib.pyplot as pyplot"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"data = {'date': ['2014-05-01 18:47:05.069722', '2014-05-01 18:47:05.119994', '2014-05-02 18:47:05.178768', '2014-05-02 18:47:05.230071', '2014-05-02 18:47:05.230071', '2014-05-02 18:47:05.280592', '2014-05-03 18:47:05.332662', '2014-05-03 18:47:05.385109', '2014-05-04 18:47:05.436523', '2014-05-04 18:47:05.486877'], \n",
" 'battle_deaths': [34, 25, 26, 15, 15, 14, 26, 25, 62, 41]}\n",
"df = pd.DataFrame(data, columns = ['date', 'battle_deaths'])\n",
"df['date'] = pd.to_datetime(df['date'])\n",
"df = df.set_index('date')"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dtype('<M8[ns]')"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.index.dtype"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>battle_deaths</th>\n",
" </tr>\n",
" <tr>\n",
" <th>date</th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2014-05-02 18:47:05.178768</th>\n",
" <td>26</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-05-02 18:47:05.230071</th>\n",
" <td>15</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-05-02 18:47:05.230071</th>\n",
" <td>15</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-05-02 18:47:05.280592</th>\n",
" <td>14</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-05-03 18:47:05.332662</th>\n",
" <td>26</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-05-03 18:47:05.385109</th>\n",
" <td>25</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" battle_deaths\n",
"date \n",
"2014-05-02 18:47:05.178768 26\n",
"2014-05-02 18:47:05.230071 15\n",
"2014-05-02 18:47:05.230071 15\n",
"2014-05-02 18:47:05.280592 14\n",
"2014-05-03 18:47:05.332662 26\n",
"2014-05-03 18:47:05.385109 25"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df['5/2/14':'5/3/14']"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>battle_deaths</th>\n",
" </tr>\n",
" <tr>\n",
" <th>date</th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2014-05-04 18:47:05.436523</th>\n",
" <td>62</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-05-04 18:47:05.486877</th>\n",
" <td>41</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" battle_deaths\n",
"date \n",
"2014-05-04 18:47:05.436523 62\n",
"2014-05-04 18:47:05.486877 41"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df['5/4/14']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### References\n",
"- https://chrisalbon.com/python/data_wrangling/pandas_time_series_basics/\n",
"- https://pandas.pydata.org/pandas-docs/stable/timeseries.html"
]
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python [default]",
"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.5.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment