|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [] |
| 7 | + }, |
| 8 | + { |
| 9 | + "cell_type": "code", |
| 10 | + "execution_count": 1, |
| 11 | + "metadata": {}, |
| 12 | + "outputs": [], |
| 13 | + "source": [ |
| 14 | + "import pandas as pd\n", |
| 15 | + "import numpy as np\n", |
| 16 | + "import matplotlib.pyplot as plt\n", |
| 17 | + "%matplotlib inline\n", |
| 18 | + "plt.style.use('fivethirtyeight')" |
| 19 | + ] |
| 20 | + }, |
| 21 | + { |
| 22 | + "cell_type": "markdown", |
| 23 | + "metadata": {}, |
| 24 | + "source": [ |
| 25 | + "## 一些坑\n", |
| 26 | + "如果你试图尝试传入这样的Series,会报错的" |
| 27 | + ] |
| 28 | + }, |
| 29 | + { |
| 30 | + "cell_type": "code", |
| 31 | + "execution_count": 2, |
| 32 | + "metadata": {}, |
| 33 | + "outputs": [ |
| 34 | + { |
| 35 | + "ename": "ValueError", |
| 36 | + "evalue": "The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().", |
| 37 | + "output_type": "error", |
| 38 | + "traceback": [ |
| 39 | + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", |
| 40 | + "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", |
| 41 | + "\u001b[0;32m<ipython-input-2-9cae3ab0f79f>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mif\u001b[0m \u001b[0mpd\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mSeries\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;32mFalse\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"I was true\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", |
| 42 | + "\u001b[0;32m~/anaconda3/lib/python3.6/site-packages/pandas/core/generic.py\u001b[0m in \u001b[0;36m__nonzero__\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 953\u001b[0m raise ValueError(\"The truth value of a {0} is ambiguous. \"\n\u001b[1;32m 954\u001b[0m \u001b[0;34m\"Use a.empty, a.bool(), a.item(), a.any() or a.all().\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 955\u001b[0;31m .format(self.__class__.__name__))\n\u001b[0m\u001b[1;32m 956\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 957\u001b[0m \u001b[0m__bool__\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m__nonzero__\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", |
| 43 | + "\u001b[0;31mValueError\u001b[0m: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()." |
| 44 | + ] |
| 45 | + } |
| 46 | + ], |
| 47 | + "source": [ |
| 48 | + "if pd.Series([False, True, False]):\n", |
| 49 | + " print(\"I was true\")" |
| 50 | + ] |
| 51 | + }, |
| 52 | + { |
| 53 | + "cell_type": "markdown", |
| 54 | + "metadata": {}, |
| 55 | + "source": [ |
| 56 | + "可以在[Comparisons]()找到相关的解释,和应该怎么避免错误,[Gotchas]()也可以。" |
| 57 | + ] |
| 58 | + }, |
| 59 | + { |
| 60 | + "cell_type": "markdown", |
| 61 | + "metadata": {}, |
| 62 | + "source": [ |
| 63 | + ">译者注:因为pandas认为这种表达是不明确的,所以会报错,以下是[Gotchas]()里的一些例子" |
| 64 | + ] |
| 65 | + }, |
| 66 | + { |
| 67 | + "cell_type": "code", |
| 68 | + "execution_count": 4, |
| 69 | + "metadata": {}, |
| 70 | + "outputs": [ |
| 71 | + { |
| 72 | + "data": { |
| 73 | + "text/plain": [ |
| 74 | + "True" |
| 75 | + ] |
| 76 | + }, |
| 77 | + "execution_count": 4, |
| 78 | + "metadata": {}, |
| 79 | + "output_type": "execute_result" |
| 80 | + } |
| 81 | + ], |
| 82 | + "source": [ |
| 83 | + "pd.Series([False, True, False]) is not None" |
| 84 | + ] |
| 85 | + }, |
| 86 | + { |
| 87 | + "cell_type": "code", |
| 88 | + "execution_count": 5, |
| 89 | + "metadata": {}, |
| 90 | + "outputs": [ |
| 91 | + { |
| 92 | + "data": { |
| 93 | + "text/plain": [ |
| 94 | + "True" |
| 95 | + ] |
| 96 | + }, |
| 97 | + "execution_count": 5, |
| 98 | + "metadata": {}, |
| 99 | + "output_type": "execute_result" |
| 100 | + } |
| 101 | + ], |
| 102 | + "source": [ |
| 103 | + "pd.Series([True]).bool()" |
| 104 | + ] |
| 105 | + }, |
| 106 | + { |
| 107 | + "cell_type": "code", |
| 108 | + "execution_count": 6, |
| 109 | + "metadata": {}, |
| 110 | + "outputs": [ |
| 111 | + { |
| 112 | + "data": { |
| 113 | + "text/plain": [ |
| 114 | + "False" |
| 115 | + ] |
| 116 | + }, |
| 117 | + "execution_count": 6, |
| 118 | + "metadata": {}, |
| 119 | + "output_type": "execute_result" |
| 120 | + } |
| 121 | + ], |
| 122 | + "source": [ |
| 123 | + "pd.DataFrame([[False]]).bool()" |
| 124 | + ] |
| 125 | + } |
| 126 | + ], |
| 127 | + "metadata": { |
| 128 | + "kernelspec": { |
| 129 | + "display_name": "Python 3", |
| 130 | + "language": "python", |
| 131 | + "name": "python3" |
| 132 | + }, |
| 133 | + "language_info": { |
| 134 | + "codemirror_mode": { |
| 135 | + "name": "ipython", |
| 136 | + "version": 3 |
| 137 | + }, |
| 138 | + "file_extension": ".py", |
| 139 | + "mimetype": "text/x-python", |
| 140 | + "name": "python", |
| 141 | + "nbconvert_exporter": "python", |
| 142 | + "pygments_lexer": "ipython3", |
| 143 | + "version": "3.6.3" |
| 144 | + } |
| 145 | + }, |
| 146 | + "nbformat": 4, |
| 147 | + "nbformat_minor": 2 |
| 148 | +} |
0 commit comments