Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save kshirsagarsiddharth/7f3af8458cdcf1c51d4009bd659e9156 to your computer and use it in GitHub Desktop.

Select an option

Save kshirsagarsiddharth/7f3af8458cdcf1c51d4009bd659e9156 to your computer and use it in GitHub Desktop.
Created on Cognitive Class Labs
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"DatetimeIndex(['2019-01-02'], dtype='datetime64[ns]', freq=None)"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"date = ['01.02.2019']\n",
"pd.to_datetime(date)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"DatetimeIndex(['2019-01-02 13:30:00'], dtype='datetime64[ns]', freq=None)"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"date = ['01.02.2019 1:30:00 PM']\n",
"pd.to_datetime(date)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2019-08-02 00:00:00\n",
"2019-02-08 00:00:00\n",
"2010-02-08 00:00:00\n"
]
}
],
"source": [
"print(pd.to_datetime('8-2-2019'))\n",
"print(pd.to_datetime('8-2-2019',dayfirst = True))\n",
"print(pd.to_datetime('10-2-8',yearfirst=True))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Timestamp('2019-01-01 14:00:00+0000', tz='UTC')"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"date = '2019-01-01T15:00:00+0100'\n",
"pd.to_datetime(date,utc=True)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"df = pd.DataFrame({'year': [2015, 2016],'month': [2, 3],'day': [4, 5]})"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 2015-02-04\n",
"1 2016-03-05\n",
"dtype: datetime64[ns]"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.to_datetime(df)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
" s = pd.Series(['3/11/2000', '3/12/2000', '3/13/2000'] * 1000)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 3/11/2000\n",
"1 3/12/2000\n",
"2 3/13/2000\n",
"3 3/11/2000\n",
"4 3/12/2000\n",
"dtype: object"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s.head()"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3.19 ms ± 27.1 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
]
}
],
"source": [
"%timeit pd.to_datetime(s,infer_datetime_format=True)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3.2 ms ± 16.6 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
]
}
],
"source": [
"%timeit pd.to_datetime(s,infer_datetime_format = False)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"DatetimeIndex(['1960-01-02', '1960-01-03', '1960-01-04'], dtype='datetime64[ns]', freq=None)"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.to_datetime([1,2,3],unit='D',origin = pd.Timestamp('1960-01-01'))"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"DatetimeIndex(['2019-02-02', '2019-02-03', '2019-02-04', '2019-02-05',\n",
" '2019-02-06', '2019-02-07', '2019-02-08'],\n",
" dtype='datetime64[ns]', freq='D')"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.date_range(start='2/2/2019',end='2/08/2019')"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"DatetimeIndex(['2019-02-02', '2019-02-03', '2019-02-04', '2019-02-05',\n",
" '2019-02-06', '2019-02-07', '2019-02-08', '2019-02-09'],\n",
" dtype='datetime64[ns]', freq='D')"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.date_range(start='2/2/2019',periods=8)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"DatetimeIndex(['2019-06-20 00:00:00', '2019-06-01 08:00:00',\n",
" '2019-05-13 16:00:00', '2019-04-25 00:00:00'],\n",
" dtype='datetime64[ns]', freq=None)"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.date_range(start='2019-06-20',end = '2019-04-25',periods=4)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"DatetimeIndex(['2019-02-28', '2019-03-31', '2019-04-30', '2019-05-31',\n",
" '2019-06-30', '2019-07-31'],\n",
" dtype='datetime64[ns]', freq='M')"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.date_range(start='2/2/2019',periods=6,freq='M')"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"DatetimeIndex(['2019-02-28', '2019-05-31', '2019-08-31', '2019-11-30',\n",
" '2020-02-29', '2020-05-31'],\n",
" dtype='datetime64[ns]', freq='3M')"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.date_range(start = '2/2/2019',periods=6,freq='3M')"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"DatetimeIndex(['2019-02-28', '2019-05-31', '2019-08-31', '2019-11-30',\n",
" '2020-02-29', '2020-05-31'],\n",
" dtype='datetime64[ns]', freq='3M')"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.date_range(start='2/2/2019',periods=6,freq=pd.offsets.MonthEnd(3))"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"DatetimeIndex(['2019-02-02 00:00:00+09:00', '2019-02-03 00:00:00+09:00',\n",
" '2019-02-04 00:00:00+09:00', '2019-02-05 00:00:00+09:00',\n",
" '2019-02-06 00:00:00+09:00', '2019-02-07 00:00:00+09:00'],\n",
" dtype='datetime64[ns, Asia/Tokyo]', freq='D')"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.date_range(start='2/2/2019',periods=6,tz='Asia/Tokyo')"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"DatetimeIndex(['2018-02-02', '2018-02-03', '2018-02-04', '2018-02-05'], dtype='datetime64[ns]', freq='D')"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.date_range(start = '2018-02-02',end='2018-02-05',closed=None)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"DatetimeIndex(['2018-02-02', '2018-02-03', '2018-02-04'], dtype='datetime64[ns]', freq='D')"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.date_range(start = '2018-02-02',end='2018-02-05',closed='left')"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"DatetimeIndex(['2018-02-03', '2018-02-04', '2018-02-05'], dtype='datetime64[ns]', freq='D')"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.date_range(start = '2018-02-02',end='2018-02-05',closed='right')"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2019-01-01 00:00:00 0\n",
"2019-01-01 00:01:00 1\n",
"2019-01-01 00:02:00 2\n",
"2019-01-01 00:03:00 3\n",
"2019-01-01 00:04:00 4\n",
"2019-01-01 00:05:00 5\n",
"2019-01-01 00:06:00 6\n",
"2019-01-01 00:07:00 7\n",
"Freq: T, dtype: int64"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"index = pd.date_range('1/1/2019', periods=8, freq='T')\n",
"series = pd.Series(range(8), index=index)\n",
"series"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2019-01-01 00:00:00 3\n",
"2019-01-01 00:03:00 12\n",
"2019-01-01 00:06:00 13\n",
"Freq: 3T, dtype: int64"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"series.resample('3T').sum()"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2019-01-01 00:03:00 3\n",
"2019-01-01 00:06:00 12\n",
"2019-01-01 00:09:00 13\n",
"Freq: 3T, dtype: int64"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"series.resample('3T',label='right').sum()"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2019-01-01 00:00:00 0\n",
"2019-01-01 00:03:00 6\n",
"2019-01-01 00:06:00 15\n",
"2019-01-01 00:09:00 7\n",
"Freq: 3T, dtype: int64"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"series.resample('3T',label='right',closed='right').sum()"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2019-01-01 00:00:00 0.0\n",
"2019-01-01 00:00:30 NaN\n",
"2019-01-01 00:01:00 1.0\n",
"2019-01-01 00:01:30 NaN\n",
"2019-01-01 00:02:00 2.0\n",
"2019-01-01 00:02:30 NaN\n",
"2019-01-01 00:03:00 3.0\n",
"2019-01-01 00:03:30 NaN\n",
"2019-01-01 00:04:00 4.0\n",
"2019-01-01 00:04:30 NaN\n",
"2019-01-01 00:05:00 5.0\n",
"2019-01-01 00:05:30 NaN\n",
"2019-01-01 00:06:00 6.0\n",
"2019-01-01 00:06:30 NaN\n",
"2019-01-01 00:07:00 7.0\n",
"Freq: 30S, dtype: float64"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"series.resample('30S').asfreq()"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2019-01-01 00:00:00 0\n",
"2019-01-01 00:00:30 1\n",
"2019-01-01 00:01:00 1\n",
"2019-01-01 00:01:30 2\n",
"2019-01-01 00:02:00 2\n",
"2019-01-01 00:02:30 3\n",
"2019-01-01 00:03:00 3\n",
"2019-01-01 00:03:30 4\n",
"2019-01-01 00:04:00 4\n",
"2019-01-01 00:04:30 5\n",
"2019-01-01 00:05:00 5\n",
"2019-01-01 00:05:30 6\n",
"2019-01-01 00:06:00 6\n",
"2019-01-01 00:06:30 7\n",
"2019-01-01 00:07:00 7\n",
"Freq: 30S, dtype: int64"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"series.resample('30S').bfill()"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2019-01-01 00:00:00 0\n",
"2019-01-01 00:00:30 0\n",
"2019-01-01 00:01:00 1\n",
"2019-01-01 00:01:30 1\n",
"2019-01-01 00:02:00 2\n",
"2019-01-01 00:02:30 2\n",
"2019-01-01 00:03:00 3\n",
"2019-01-01 00:03:30 3\n",
"2019-01-01 00:04:00 4\n",
"2019-01-01 00:04:30 4\n",
"2019-01-01 00:05:00 5\n",
"2019-01-01 00:05:30 5\n",
"2019-01-01 00:06:00 6\n",
"2019-01-01 00:06:30 6\n",
"2019-01-01 00:07:00 7\n",
"Freq: 30S, dtype: int64"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"series.resample('30S').ffill()"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [],
"source": [
"def custom_resampler(array_like):\n",
" return np.sum(array_like) + 5"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2019-01-01 00:00:00 8\n",
"2019-01-01 00:03:00 17\n",
"2019-01-01 00:06:00 18\n",
"Freq: 3T, dtype: int64"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"series.resample('3T').apply(custom_resampler)"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2018 1\n",
"2019 2\n",
"Freq: A-DEC, dtype: int64"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s = pd.Series([1, 2], index=pd.period_range('2018-01-01',\n",
" freq='A',\n",
" periods=2))\n",
"s"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2018Q1 1.0\n",
"2018Q2 NaN\n",
"2018Q3 NaN\n",
"2018Q4 NaN\n",
"2019Q1 2.0\n",
"2019Q2 NaN\n",
"2019Q3 NaN\n",
"2019Q4 NaN\n",
"Freq: Q-DEC, dtype: float64"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s.resample('Q').asfreq()"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2019Q1 2\n",
"2019Q2 3\n",
"2019Q3 4\n",
"2019Q4 5\n",
"Freq: Q-DEC, dtype: int64"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"q = pd.Series([2, 3, 4, 5], index=pd.period_range('2019-01-01',\n",
" freq='Q',\n",
" periods=4))\n",
"q"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2019-03 2.0\n",
"2019-04 NaN\n",
"2019-05 NaN\n",
"2019-06 3.0\n",
"2019-07 NaN\n",
"2019-08 NaN\n",
"2019-09 4.0\n",
"2019-10 NaN\n",
"2019-11 NaN\n",
"2019-12 5.0\n",
"Freq: M, dtype: float64"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"q.resample('M',convention='end').asfreq()"
]
},
{
"cell_type": "code",
"execution_count": 42,
"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>price</th>\n",
" <th>volume</th>\n",
" <th>week_starting</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>8</td>\n",
" <td>40</td>\n",
" <td>2019-01-06</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>9</td>\n",
" <td>50</td>\n",
" <td>2019-01-13</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>7</td>\n",
" <td>30</td>\n",
" <td>2019-01-20</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>11</td>\n",
" <td>80</td>\n",
" <td>2019-01-27</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>12</td>\n",
" <td>40</td>\n",
" <td>2019-02-03</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>16</td>\n",
" <td>80</td>\n",
" <td>2019-02-10</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>15</td>\n",
" <td>30</td>\n",
" <td>2019-02-17</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>17</td>\n",
" <td>40</td>\n",
" <td>2019-02-24</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" price volume week_starting\n",
"0 8 40 2019-01-06\n",
"1 9 50 2019-01-13\n",
"2 7 30 2019-01-20\n",
"3 11 80 2019-01-27\n",
"4 12 40 2019-02-03\n",
"5 16 80 2019-02-10\n",
"6 15 30 2019-02-17\n",
"7 17 40 2019-02-24"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d = dict({'price': [8, 9, 7, 11, 12, 16, 15, 17],\n",
" 'volume': [40, 50, 30, 80, 40, 80, 30, 40]})\n",
"df = pd.DataFrame(d)\n",
"df['week_starting'] = pd.date_range('01/01/2019',\n",
" periods=8,\n",
" freq='W')\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": 44,
"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>price</th>\n",
" <th>volume</th>\n",
" </tr>\n",
" <tr>\n",
" <th>week_starting</th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2019-01-31</th>\n",
" <td>8.75</td>\n",
" <td>50.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2019-02-28</th>\n",
" <td>15.00</td>\n",
" <td>47.5</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" price volume\n",
"week_starting \n",
"2019-01-31 8.75 50.0\n",
"2019-02-28 15.00 47.5"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.resample('M',on='week_starting').mean()"
]
},
{
"cell_type": "code",
"execution_count": 45,
"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></th>\n",
" <th>price</th>\n",
" <th>volume</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th rowspan=\"2\" valign=\"top\">2019-01-01</th>\n",
" <th>morning</th>\n",
" <td>8</td>\n",
" <td>40</td>\n",
" </tr>\n",
" <tr>\n",
" <th>afternoon</th>\n",
" <td>9</td>\n",
" <td>50</td>\n",
" </tr>\n",
" <tr>\n",
" <th rowspan=\"2\" valign=\"top\">2019-01-02</th>\n",
" <th>morning</th>\n",
" <td>7</td>\n",
" <td>30</td>\n",
" </tr>\n",
" <tr>\n",
" <th>afternoon</th>\n",
" <td>11</td>\n",
" <td>80</td>\n",
" </tr>\n",
" <tr>\n",
" <th rowspan=\"2\" valign=\"top\">2019-01-03</th>\n",
" <th>morning</th>\n",
" <td>12</td>\n",
" <td>40</td>\n",
" </tr>\n",
" <tr>\n",
" <th>afternoon</th>\n",
" <td>16</td>\n",
" <td>80</td>\n",
" </tr>\n",
" <tr>\n",
" <th rowspan=\"2\" valign=\"top\">2019-01-04</th>\n",
" <th>morning</th>\n",
" <td>15</td>\n",
" <td>30</td>\n",
" </tr>\n",
" <tr>\n",
" <th>afternoon</th>\n",
" <td>17</td>\n",
" <td>40</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" price volume\n",
"2019-01-01 morning 8 40\n",
" afternoon 9 50\n",
"2019-01-02 morning 7 30\n",
" afternoon 11 80\n",
"2019-01-03 morning 12 40\n",
" afternoon 16 80\n",
"2019-01-04 morning 15 30\n",
" afternoon 17 40"
]
},
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"days = pd.date_range('1/1/2019', periods=4, freq='D')\n",
"d2 = dict({'price': [8, 9, 7, 11, 12, 16, 15, 17],\n",
" 'volume': [40, 50, 30, 80, 40, 80, 30, 40]})\n",
"df2 = pd.DataFrame(d2,\n",
" index=pd.MultiIndex.from_product([days,\n",
" ['morning',\n",
" 'afternoon']]\n",
" ))\n",
"df2"
]
},
{
"cell_type": "code",
"execution_count": 47,
"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>price</th>\n",
" <th>volume</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2019-01-01</th>\n",
" <td>17</td>\n",
" <td>90</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2019-01-02</th>\n",
" <td>18</td>\n",
" <td>110</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2019-01-03</th>\n",
" <td>28</td>\n",
" <td>120</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2019-01-04</th>\n",
" <td>32</td>\n",
" <td>70</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" price volume\n",
"2019-01-01 17 90\n",
"2019-01-02 18 110\n",
"2019-01-03 28 120\n",
"2019-01-04 32 70"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df2.resample('D',level=0).sum()"
]
},
{
"cell_type": "code",
"execution_count": 49,
"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>price</th>\n",
" <th>volume</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2019-01-01</th>\n",
" <td>8.5</td>\n",
" <td>45.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2019-01-02</th>\n",
" <td>9.0</td>\n",
" <td>55.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2019-01-03</th>\n",
" <td>14.0</td>\n",
" <td>60.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2019-01-04</th>\n",
" <td>16.0</td>\n",
" <td>35.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" price volume\n",
"2019-01-01 8.5 45.0\n",
"2019-01-02 9.0 55.0\n",
"2019-01-03 14.0 60.0\n",
"2019-01-04 16.0 35.0"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df2.resample('D',level=0).mean()"
]
},
{
"cell_type": "code",
"execution_count": 50,
"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>s</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2019-01-01 00:00:00</th>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2019-01-01 00:01:00</th>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2019-01-01 00:02:00</th>\n",
" <td>4.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2019-01-01 00:03:00</th>\n",
" <td>5.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" s\n",
"2019-01-01 00:00:00 0.0\n",
"2019-01-01 00:01:00 NaN\n",
"2019-01-01 00:02:00 4.0\n",
"2019-01-01 00:03:00 5.0"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"index = pd.date_range('1/1/2019', periods=4, freq='T')\n",
"series = pd.Series([0.0, None, 4.0, 5.0], index=index)\n",
"df = pd.DataFrame({'s':series})\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": 52,
"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>s</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2019-01-01 00:00:00</th>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2019-01-01 00:00:30</th>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2019-01-01 00:01:00</th>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2019-01-01 00:01:30</th>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2019-01-01 00:02:00</th>\n",
" <td>4.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2019-01-01 00:02:30</th>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2019-01-01 00:03:00</th>\n",
" <td>5.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" s\n",
"2019-01-01 00:00:00 0.0\n",
"2019-01-01 00:00:30 NaN\n",
"2019-01-01 00:01:00 NaN\n",
"2019-01-01 00:01:30 NaN\n",
"2019-01-01 00:02:00 4.0\n",
"2019-01-01 00:02:30 NaN\n",
"2019-01-01 00:03:00 5.0"
]
},
"execution_count": 52,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.asfreq('30S')"
]
},
{
"cell_type": "code",
"execution_count": 53,
"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>s</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2019-01-01 00:00:00</th>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2019-01-01 00:00:30</th>\n",
" <td>0.7</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2019-01-01 00:01:00</th>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2019-01-01 00:01:30</th>\n",
" <td>0.7</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2019-01-01 00:02:00</th>\n",
" <td>4.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2019-01-01 00:02:30</th>\n",
" <td>0.7</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2019-01-01 00:03:00</th>\n",
" <td>5.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" s\n",
"2019-01-01 00:00:00 0.0\n",
"2019-01-01 00:00:30 0.7\n",
"2019-01-01 00:01:00 NaN\n",
"2019-01-01 00:01:30 0.7\n",
"2019-01-01 00:02:00 4.0\n",
"2019-01-01 00:02:30 0.7\n",
"2019-01-01 00:03:00 5.0"
]
},
"execution_count": 53,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.asfreq(freq='30S',fill_value=0.7)"
]
},
{
"cell_type": "code",
"execution_count": 54,
"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>s</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2019-01-01 00:00:00</th>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2019-01-01 00:00:30</th>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2019-01-01 00:01:00</th>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2019-01-01 00:01:30</th>\n",
" <td>4.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2019-01-01 00:02:00</th>\n",
" <td>4.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2019-01-01 00:02:30</th>\n",
" <td>5.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2019-01-01 00:03:00</th>\n",
" <td>5.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" s\n",
"2019-01-01 00:00:00 0.0\n",
"2019-01-01 00:00:30 NaN\n",
"2019-01-01 00:01:00 NaN\n",
"2019-01-01 00:01:30 4.0\n",
"2019-01-01 00:02:00 4.0\n",
"2019-01-01 00:02:30 5.0\n",
"2019-01-01 00:03:00 5.0"
]
},
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.asfreq(freq='30S',method='bfill')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python",
"language": "python",
"name": "conda-env-python-py"
},
"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.7"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment