{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np\n", "from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN\n", "import platform" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.6.5\n", "0.23.0\n", "1.14.3\n" ] } ], "source": [ "print(platform.python_version())\n", "print(pd.__version__)\n", "print(np.__version__)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "s_f = pd.Series([123.456, 654.123])" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 123.456\n", "1 654.123\n", "dtype: float64\n" ] } ], "source": [ "print(s_f)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 123.0\n", "1 654.0\n", "dtype: float64\n" ] } ], "source": [ "print(s_f.round())" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 123\n", "1 654\n", "dtype: int64\n" ] } ], "source": [ "print(s_f.round().astype(int))" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 123.46\n", "1 654.12\n", "dtype: float64\n" ] } ], "source": [ "print(s_f.round(2))" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 100.0\n", "1 700.0\n", "dtype: float64\n" ] } ], "source": [ "print(s_f.round(-2))" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": true }, "outputs": [], "source": [ "s_i = pd.Series([123, 654])" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 123\n", "1 654\n", "dtype: int64\n" ] } ], "source": [ "print(s_i)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 123\n", "1 654\n", "dtype: int64\n" ] } ], "source": [ "print(s_i.round())" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 123\n", "1 654\n", "dtype: int64\n" ] } ], "source": [ "print(s_i.round(2))" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 100\n", "1 700\n", "dtype: int64\n" ] } ], "source": [ "print(s_i.round(-2))" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 100\n", "1 700\n", "dtype: int64\n" ] } ], "source": [ "s_i_round = s_i.round(-2)\n", "\n", "print(s_i_round)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 123\n", "1 654\n", "dtype: int64\n" ] } ], "source": [ "print(s_i)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": true }, "outputs": [], "source": [ "df = pd.DataFrame({'f': [123.456, 654.321], 'i': [123, 654], 's': ['abc', 'xyz']})" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " f i s\n", "0 123.456 123 abc\n", "1 654.321 654 xyz\n" ] } ], "source": [ "print(df)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "f float64\n", "i int64\n", "s object\n", "dtype: object\n" ] } ], "source": [ "print(df.dtypes)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " f i s\n", "0 123.0 123 abc\n", "1 654.0 654 xyz\n" ] } ], "source": [ "print(df.round())" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " f i s\n", "0 123.46 123 abc\n", "1 654.32 654 xyz\n" ] } ], "source": [ "print(df.round(2))" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " f i s\n", "0 100.0 100 abc\n", "1 700.0 700 xyz\n" ] } ], "source": [ "print(df.round(-2))" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " f i s\n", "0 123.46 120 abc\n", "1 654.32 650 xyz\n" ] } ], "source": [ "print(df.round({'f': 2, 'i': -1}))" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " f i s\n", "0 123.456 100 abc\n", "1 654.321 700 xyz\n" ] } ], "source": [ "print(df.round({'i': -2}))" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 0.5\n", "1 1.5\n", "2 2.5\n", "3 3.5\n", "4 4.5\n", "dtype: float64\n" ] } ], "source": [ "s = pd.Series([0.5, 1.5, 2.5, 3.5, 4.5])\n", "\n", "print(s)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 0.0\n", "1 2.0\n", "2 2.0\n", "3 4.0\n", "4 4.0\n", "dtype: float64\n" ] } ], "source": [ "print(s.round())" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 5.0\n", "1 15.0\n", "2 25.0\n", "3 5.1\n", "4 15.1\n", "5 25.1\n", "dtype: float64\n" ] } ], "source": [ "s = pd.Series([5, 15, 25, 5.1, 15.1, 25.1])\n", "\n", "print(s)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 0.0\n", "1 20.0\n", "2 20.0\n", "3 10.0\n", "4 20.0\n", "5 30.0\n", "dtype: float64\n" ] } ], "source": [ "print(s.round(-1))" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Python round\n", "0.005 => 0.01\n", "0.015 => 0.01\n", "0.025 => 0.03\n", "0.035 => 0.04\n", "0.045 => 0.04\n", "2.675 => 2.67\n" ] } ], "source": [ "print('Python round')\n", "print('0.005 => ', round(0.005, 2))\n", "print('0.015 => ', round(0.015, 2))\n", "print('0.025 => ', round(0.025, 2))\n", "print('0.035 => ', round(0.035, 2))\n", "print('0.045 => ', round(0.045, 2))\n", "print('2.675 => ', round(2.675, 2))" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "NumPy np.around (np.round)\n", "0.005 => 0.0\n", "0.015 => 0.02\n", "0.025 => 0.02\n", "0.035 => 0.04\n", "0.045 => 0.04\n", "2.675 => 2.68\n" ] } ], "source": [ "print('NumPy np.around (np.round)')\n", "print('0.005 => ', np.around(0.005, 2))\n", "print('0.015 => ', np.around(0.015, 2))\n", "print('0.025 => ', np.around(0.025, 2))\n", "print('0.035 => ', np.around(0.035, 2))\n", "print('0.045 => ', np.around(0.045, 2))\n", "print('2.675 => ', np.around(2.675, 2))" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 0.005\n", "1 0.015\n", "2 0.025\n", "3 0.035\n", "4 0.045\n", "5 2.675\n", "dtype: float64\n" ] } ], "source": [ "s = pd.Series([0.005, 0.015, 0.025, 0.035, 0.045, 2.675])\n", "\n", "print(s)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 0.00\n", "1 0.02\n", "2 0.02\n", "3 0.04\n", "4 0.04\n", "5 2.68\n", "dtype: float64\n" ] } ], "source": [ "print(s.round(2))" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2.67499999999999982236431605997495353221893310546875\n" ] } ], "source": [ "print(Decimal(2.675))" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2.675\n" ] } ], "source": [ "print(Decimal('2.675'))" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2.68\n" ] } ], "source": [ "print(Decimal('2.675').quantize(Decimal('0.01'), rounding=ROUND_HALF_UP))" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2.68\n" ] } ], "source": [ "print(Decimal('2.675').quantize(Decimal('0.01'), rounding=ROUND_HALF_EVEN))" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n" ] } ], "source": [ "print(Decimal('0.5').quantize(Decimal('0'), rounding=ROUND_HALF_UP))" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n" ] } ], "source": [ "print(Decimal('0.5').quantize(Decimal('0'), rounding=ROUND_HALF_EVEN))" ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "collapsed": true }, "outputs": [], "source": [ "s = pd.Series([0.5, 1.5, 2.5, 3.5, 4.5])" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 1.0\n", "1 2.0\n", "2 3.0\n", "3 4.0\n", "4 5.0\n", "dtype: float64\n" ] } ], "source": [ "print(s.map(lambda x: float(Decimal(str(x)).\n", " quantize(Decimal('0'), rounding=ROUND_HALF_UP))))" ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "collapsed": true }, "outputs": [], "source": [ "s = pd.Series([0.005, 0.015, 0.025, 0.035, 0.045, 2.675])" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 0.01\n", "1 0.02\n", "2 0.03\n", "3 0.04\n", "4 0.05\n", "5 2.68\n", "dtype: float64\n" ] } ], "source": [ "print(s.map(lambda x: float(Decimal(str(x))\n", " .quantize(Decimal('0.01'), rounding=ROUND_HALF_UP))))" ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "collapsed": true }, "outputs": [], "source": [ "s = pd.Series([5, 15, 25, 5.1, 15.1, 25.1])" ] }, { "cell_type": "code", "execution_count": 43, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 10\n", "1 20\n", "2 30\n", "3 10\n", "4 20\n", "5 30\n", "dtype: int64\n" ] } ], "source": [ "print(s.map(lambda x: int(Decimal(str(x))\n", " .quantize(Decimal('1E1'), rounding=ROUND_HALF_UP))))" ] }, { "cell_type": "code", "execution_count": 44, "metadata": { "collapsed": true }, "outputs": [], "source": [ "s = pd.Series([0.005, 0.015, 0.025, 0.035, 0.045, 2.675])" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 0.00\n", "1 0.02\n", "2 0.02\n", "3 0.04\n", "4 0.04\n", "5 2.68\n", "dtype: float64\n" ] } ], "source": [ "print(s.map(lambda x: float(Decimal(str(x))\n", " .quantize(Decimal('0.01'), rounding=ROUND_HALF_EVEN))))" ] } ], "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 }