{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Demonstration of some properties related to `NaN`\n", "\n", "* `np.nan != np.nan`, `math.nan != math`.\n", "* use `np.isnan` or `math.nan`" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import math\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "mt_nan = math.nan" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "np_nan = np.nan" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.isnan(mt_nan)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.isnan(np_nan)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.isnan(mt_nan)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.isnan(np_nan)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.isnan == np.isnan" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np_nan == np_nan" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mt_nan == mt_nan" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## behavior of `pd.DataFrame` wst. `None`" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "import pandas as pd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`None` is converted to `NaN` when the column type is `float`" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ab
01NaN
121.4
\n", "
" ], "text/plain": [ " a b\n", "0 1 NaN\n", "1 2 1.4" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "df = pd.DataFrame([{'a':1, 'b': None}, {'a': 2, 'b': 1.4}])\n", "display(df)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "a int64\n", "b float64\n", "dtype: object" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "display(df.dtypes)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "scrolled": true }, "outputs": [], "source": [ "assert not (df['b'].values[0] is None)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "scrolled": true }, "outputs": [], "source": [ "assert np.isnan(df['b'].values[0])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`None` is unchanged when the column type is `object`" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ab
01None
12s
\n", "
" ], "text/plain": [ " a b\n", "0 1 None\n", "1 2 s" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "a int64\n", "b object\n", "dtype: object" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "df = pd.DataFrame([{'a':1, 'b': None}, {'a': 2, 'b': 's'}])\n", "display(df)\n", "display(df.dtypes)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "scrolled": true }, "outputs": [], "source": [ "assert df['b'].values[0] is None" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "import unittest" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "np.isnan isn't applicable to `None` type" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "✓ TypeError is raised!\n", "ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''\n" ] } ], "source": [ "try:\n", " np.isnan(df['b'].values[0])\n", "except TypeError as err:\n", " print('✓ TypeError is raised!')\n", " print(err)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "try another case of `object column`" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ab
01None
12[1, 2]
\n", "
" ], "text/plain": [ " a b\n", "0 1 None\n", "1 2 [1, 2]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "df = pd.DataFrame([{'a':1, 'b': None}, {'a': 2, 'b': [1, 2]}])\n", "display(df)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "a int64\n", "b object\n", "dtype: object" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "display(df.dtypes)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "assert df['b'].values[0] is None" ] } ], "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.3" } }, "nbformat": 4, "nbformat_minor": 2 }