Skip to content

Instantly share code, notes, and snippets.

@vinaykudari
Created July 27, 2018 18:59
Show Gist options
  • Save vinaykudari/60cf36838ea535733b49b13e03f969dd to your computer and use it in GitHub Desktop.
Save vinaykudari/60cf36838ea535733b49b13e03f969dd to your computer and use it in GitHub Desktop.
Reindexing
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 81,
"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-01</th>\n",
" <td>34</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-05-02</th>\n",
" <td>25</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-05-05</th>\n",
" <td>26</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-05-07</th>\n",
" <td>15</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" battle_deaths\n",
"date \n",
"2014-05-01 34\n",
"2014-05-02 25\n",
"2014-05-05 26\n",
"2014-05-07 15"
]
},
"execution_count": 81,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data = {'date': ['2014-05-01', '2014-05-02', '2014-05-05', '2014-05-07'], \n",
" 'battle_deaths': [34, 25, 26, 15]}\n",
"\n",
"df = pd.DataFrame(data, columns = ['date', 'battle_deaths'])\n",
"df['date'] = pd.to_datetime(df['date'])\n",
"df.set_index('date', inplace=True)\n",
"df['May 2014']"
]
},
{
"cell_type": "code",
"execution_count": 90,
"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",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2014-05-01</th>\n",
" <td>34</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-05-02</th>\n",
" <td>25</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-05-03</th>\n",
" <td>25</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-05-04</th>\n",
" <td>25</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-05-05</th>\n",
" <td>26</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-05-06</th>\n",
" <td>26</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-05-07</th>\n",
" <td>15</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" battle_deaths\n",
"2014-05-01 34\n",
"2014-05-02 25\n",
"2014-05-03 25\n",
"2014-05-04 25\n",
"2014-05-05 26\n",
"2014-05-06 26\n",
"2014-05-07 15"
]
},
"execution_count": 90,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"time_range = pd.date_range('2014-05-01', '2014-05-07') #will return datetime series object\n",
"df_reindexed = df.reindex(time_range, method='ffill') #ffill is forward fill\n",
"df_reindexed"
]
}
],
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment