|
15 | 15 | Caveats and Gotchas |
16 | 16 | ******************* |
17 | 17 |
|
| 18 | +Using If/Truth Statements with Pandas |
| 19 | +------------------------------------- |
| 20 | + |
| 21 | +.. _gotchas.truth: |
| 22 | + |
| 23 | +Pandas follows the numpy convention of raising an error when you try to convert something to a ``bool``. |
| 24 | +This happens in a ``if`` or when using the boolean operations, ``and``, ``or``, or ``not``. It is not clear |
| 25 | +what the result of |
| 26 | + |
| 27 | +.. code-block:: python |
| 28 | +
|
| 29 | + >>> if Series([False, True, False]): |
| 30 | + ... |
| 31 | +
|
| 32 | +should be. Should it be ``True`` because it's not zero-length? ``False`` because there are ``False`` values? |
| 33 | +It is unclear, so instead, pandas raises a ``ValueError``: |
| 34 | + |
| 35 | +.. code-block:: python |
| 36 | +
|
| 37 | + >>> if pd.Series([False, True, False]): |
| 38 | + print("I was true") |
| 39 | + Traceback |
| 40 | + ... |
| 41 | + ValueError: The truth value of an array is ambiguous. Use a.empty, a.any() or a.all(). |
| 42 | +
|
| 43 | +
|
| 44 | +If you see that, you need to explicitly choose what you want to do with it (e.g., use `any()`, `all()` or `empty`). |
| 45 | +or, you might want to compare if the pandas object is ``None`` |
| 46 | + |
| 47 | +.. code-block:: python |
| 48 | +
|
| 49 | + >>> if pd.Series([False, True, False]) is not None: |
| 50 | + print("I was not None") |
| 51 | + >>> I was not None |
| 52 | +
|
| 53 | +Bitwise boolean |
| 54 | +~~~~~~~~~~~~~~~ |
| 55 | + |
| 56 | +Bitwise boolean operators like ``==`` and ``!=`` will return a boolean ``Series``, |
| 57 | +which is almost always what you want anyways. |
| 58 | + |
| 59 | +.. code-block:: python |
| 60 | +
|
| 61 | + >>> s = pd.Series(range(5)) |
| 62 | + >>> s == 4 |
| 63 | + 0 False |
| 64 | + 1 False |
| 65 | + 2 False |
| 66 | + 3 False |
| 67 | + 4 True |
| 68 | + dtype: bool |
| 69 | +
|
18 | 70 | ``NaN``, Integer ``NA`` values and ``NA`` type promotions |
19 | 71 | --------------------------------------------------------- |
20 | 72 |
|
@@ -428,7 +480,7 @@ parse HTML tables in the top-level pandas io function ``read_html``. |
428 | 480 | lxml will work correctly: |
429 | 481 |
|
430 | 482 | .. code-block:: sh |
431 | | - |
| 483 | +
|
432 | 484 | # remove the included version |
433 | 485 | conda remove lxml |
434 | 486 |
|
|
0 commit comments