{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Examples**
\n",
"Filling in NaN in a Series via linear interpolation:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 0.0\n",
"1 2.0\n",
"2 NaN\n",
"3 4.0\n",
"dtype: float64"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s = pd.Series([0, 2, np.nan, 4])\n",
"s"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 0.0\n",
"1 2.0\n",
"2 3.0\n",
"3 4.0\n",
"dtype: float64"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s.interpolate()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Filling in NaN in a Series by padding, but filling at most two consecutive
\n",
"NaN at a time."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 NaN\n",
"1 single_one\n",
"2 NaN\n",
"3 fill_two_more\n",
"4 NaN\n",
"5 NaN\n",
"6 NaN\n",
"7 3.71\n",
"8 NaN\n",
"dtype: object"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s = pd.Series([np.nan, \"single_one\", np.nan,\n",
" \"fill_two_more\", np.nan, np.nan, np.nan,\n",
" 3.71, np.nan])\n",
"s"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 NaN\n",
"1 single_one\n",
"2 single_one\n",
"3 fill_two_more\n",
"4 fill_two_more\n",
"5 fill_two_more\n",
"6 NaN\n",
"7 3.71\n",
"8 3.71\n",
"dtype: object"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s.interpolate(method='pad', limit=2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Filling in NaN in a Series via polynomial interpolation or splines: Both ‘polynomial’ and ‘spline’
\n",
"methods require that you also specify an order (int)."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 0.000000\n",
"1 2.000000\n",
"2 4.666667\n",
"3 8.000000\n",
"dtype: float64"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s = pd.Series([0, 2, np.nan, 8])\n",
"s.interpolate(method='polynomial', order=2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Fill the DataFrame forward (that is, going down) along each column using linear interpolation.
\n",
"Note how the last entry in column ‘p’ is interpolated differently, because there is no entry after
\n",
"it to use for interpolation. Note how the first entry in column ‘q’ remains NaN, because there is no entry
\n",
"before it to use for interpolation."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" p | \n",
" q | \n",
" r | \n",
" s | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 0.0 | \n",
" NaN | \n",
" -1.0 | \n",
" 1.0 | \n",
"
\n",
" \n",
" 1 | \n",
" NaN | \n",
" 2.0 | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
" 2 | \n",
" 2.0 | \n",
" 3.0 | \n",
" NaN | \n",
" 9.0 | \n",
"
\n",
" \n",
" 3 | \n",
" NaN | \n",
" 4.0 | \n",
" -4.0 | \n",
" 16.0 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" p q r s\n",
"0 0.0 NaN -1.0 1.0\n",
"1 NaN 2.0 NaN NaN\n",
"2 2.0 3.0 NaN 9.0\n",
"3 NaN 4.0 -4.0 16.0"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df = pd.DataFrame([(0.0, np.nan, -1.0, 1.0),\n",
" (np.nan, 2.0, np.nan, np.nan),\n",
" (2.0, 3.0, np.nan, 9.0),\n",
" (np.nan, 4.0, -4.0, 16.0)],\n",
" columns=list('pqrs'))\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" p | \n",
" q | \n",
" r | \n",
" s | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 0.0 | \n",
" NaN | \n",
" -1.0 | \n",
" 1.0 | \n",
"
\n",
" \n",
" 1 | \n",
" 1.0 | \n",
" 2.0 | \n",
" -2.0 | \n",
" 5.0 | \n",
"
\n",
" \n",
" 2 | \n",
" 2.0 | \n",
" 3.0 | \n",
" -3.0 | \n",
" 9.0 | \n",
"
\n",
" \n",
" 3 | \n",
" 2.0 | \n",
" 4.0 | \n",
" -4.0 | \n",
" 16.0 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" p q r s\n",
"0 0.0 NaN -1.0 1.0\n",
"1 1.0 2.0 -2.0 5.0\n",
"2 2.0 3.0 -3.0 9.0\n",
"3 2.0 4.0 -4.0 16.0"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.interpolate(method='linear', limit_direction='forward', axis=0)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Using polynomial interpolation."
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 1.0\n",
"1 4.0\n",
"2 9.0\n",
"3 16.0\n",
"Name: s, dtype: float64"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df['s'].interpolate(method='polynomial', order=2)"
]
}
],
"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": 4
}