|
| 1 | +.. _code_style: |
| 2 | + |
| 3 | +{{ header }} |
| 4 | + |
| 5 | +======================= |
| 6 | +pandas code style guide |
| 7 | +======================= |
| 8 | + |
| 9 | +.. contents:: Table of contents: |
| 10 | + :local: |
| 11 | + |
| 12 | +Patterns |
| 13 | +======== |
| 14 | + |
| 15 | +foo.__class__ |
| 16 | +------------- |
| 17 | + |
| 18 | +*pandas* uses 'type(foo)' instead 'foo.__class__' as it is making the code more |
| 19 | +readable. |
| 20 | + |
| 21 | +For example: |
| 22 | + |
| 23 | +**Good:** |
| 24 | + |
| 25 | +.. code-block:: python |
| 26 | +
|
| 27 | + foo = "bar" |
| 28 | + type(foo) |
| 29 | +
|
| 30 | +**Bad:** |
| 31 | + |
| 32 | +.. code-block:: python |
| 33 | +
|
| 34 | + foo = "bar" |
| 35 | + foo.__class__ |
| 36 | +
|
| 37 | +
|
| 38 | +String formatting |
| 39 | +================= |
| 40 | + |
| 41 | +Concatenated strings |
| 42 | +-------------------- |
| 43 | + |
| 44 | +f-strings |
| 45 | +~~~~~~~~~ |
| 46 | + |
| 47 | +*pandas* uses f-strings formatting instead of '%' and '.format()' string formatters. |
| 48 | + |
| 49 | +The convention of using f-strings on a string that is concatenated over serveral lines, |
| 50 | +is to prefix only the lines containing the value needs to be interpeted. |
| 51 | + |
| 52 | +For example: |
| 53 | + |
| 54 | +**Good:** |
| 55 | + |
| 56 | +.. code-block:: python |
| 57 | +
|
| 58 | + foo = "old_function" |
| 59 | + bar = "new_function" |
| 60 | +
|
| 61 | + my_warning_message = ( |
| 62 | + f"Warning, {foo} is deprecated, " |
| 63 | + "please use the new and way better " |
| 64 | + f"{bar}" |
| 65 | + ) |
| 66 | +
|
| 67 | +**Bad:** |
| 68 | + |
| 69 | +.. code-block:: python |
| 70 | +
|
| 71 | + foo = "old_function" |
| 72 | + bar = "new_function" |
| 73 | +
|
| 74 | + my_warning_message = ( |
| 75 | + f"Warning, {foo} is deprecated, " |
| 76 | + f"please use the new and way better " |
| 77 | + f"{bar}" |
| 78 | + ) |
| 79 | +
|
| 80 | +White spaces |
| 81 | +~~~~~~~~~~~~ |
| 82 | + |
| 83 | +Putting the white space only at the end of the previous line, so |
| 84 | +there is no whitespace at the beggining of the concatenated string. |
| 85 | + |
| 86 | +For example: |
| 87 | + |
| 88 | +**Good:** |
| 89 | + |
| 90 | +.. code-block:: python |
| 91 | +
|
| 92 | + example_string = ( |
| 93 | + "Some long concatenated string, " |
| 94 | + "with good placement of the " |
| 95 | + "whitespaces" |
| 96 | + ) |
| 97 | +
|
| 98 | +**Bad:** |
| 99 | + |
| 100 | +.. code-block:: python |
| 101 | +
|
| 102 | + example_string = ( |
| 103 | + "Some long concatenated string," |
| 104 | + " with bad placement of the" |
| 105 | + " whitespaces" |
| 106 | + ) |
| 107 | +
|
| 108 | +Representation function (aka 'repr()') |
| 109 | +-------------------------------------- |
| 110 | + |
| 111 | +*pandas* uses 'repr()' instead of '%r' and '!r'. |
| 112 | + |
| 113 | +The use of 'repr()' will only happend when the value is not an obvious string. |
| 114 | + |
| 115 | +For example: |
| 116 | + |
| 117 | +**Good:** |
| 118 | + |
| 119 | +.. code-block:: python |
| 120 | +
|
| 121 | + value = str |
| 122 | + f"Unknown recived value, got: {repr(value)}" |
| 123 | +
|
| 124 | +**Good:** |
| 125 | + |
| 126 | +.. code-block:: python |
| 127 | +
|
| 128 | + value = str |
| 129 | + f"Unknown recived type, got: '{type(value).__name__}'" |
0 commit comments