|
| 1 | +.. _Not_sure_what_to_put_here: |
| 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 | + cls = type(self) |
| 28 | +
|
| 29 | +**Good:** |
| 30 | + |
| 31 | +.. code-block:: python |
| 32 | +
|
| 33 | + name = type(self).__name__ |
| 34 | +
|
| 35 | +**Bad:** |
| 36 | + |
| 37 | +.. code-block:: python |
| 38 | +
|
| 39 | + cls = self.__class__ |
| 40 | +
|
| 41 | +**Bad:** |
| 42 | + |
| 43 | +.. code-block:: python |
| 44 | +
|
| 45 | + name = self.__class__.__name__ |
| 46 | +
|
| 47 | +String formatting |
| 48 | +================= |
| 49 | + |
| 50 | +Concatenated strings |
| 51 | +-------------------- |
| 52 | + |
| 53 | +f-strings |
| 54 | +~~~~~~~~~ |
| 55 | + |
| 56 | +*pandas* uses f-strings formatting instead of '%' and '.format()' string formatters. |
| 57 | + |
| 58 | +The convention of using f-strings on a string that is concatenated over serveral lines, |
| 59 | +is to prefix only the lines containing the value needs to be interpeted. |
| 60 | + |
| 61 | +For example: |
| 62 | + |
| 63 | +**Good:** |
| 64 | + |
| 65 | +.. code-block:: python |
| 66 | +
|
| 67 | + my_warning_message = ( |
| 68 | + f"Warning, {old_function_name} is deprecated, " |
| 69 | + "please use the new and way better " |
| 70 | + f"{new_function_name}" |
| 71 | + ) |
| 72 | +
|
| 73 | +**Bad:** |
| 74 | + |
| 75 | +.. code-block:: python |
| 76 | +
|
| 77 | + my_warning_message = ( |
| 78 | + f"Warning, {old_function_name} is deprecated, " |
| 79 | + f"please use the new and way better " |
| 80 | + f"{new_function_name}" |
| 81 | + ) |
| 82 | +
|
| 83 | +White spaces |
| 84 | +~~~~~~~~~~~~ |
| 85 | + |
| 86 | +Putting the white space only at the end of the previous line, so |
| 87 | +there is no whitespace at the beggining of the concatenated string. |
| 88 | + |
| 89 | +For example: |
| 90 | + |
| 91 | +**Good:** |
| 92 | + |
| 93 | +.. code-block:: python |
| 94 | +
|
| 95 | + example_string = ( |
| 96 | + "Some long concatenated string, " |
| 97 | + "with good placement of the " |
| 98 | + "whitespaces" |
| 99 | + ) |
| 100 | +
|
| 101 | +**Bad:** |
| 102 | + |
| 103 | +.. code-block:: python |
| 104 | +
|
| 105 | + example_string = ( |
| 106 | + "Some long concatenated string," |
| 107 | + " with bad placement of the" |
| 108 | + " whitespaces" |
| 109 | + ) |
| 110 | +
|
| 111 | +Representation function (aka 'repr()') |
| 112 | +-------------------------------------- |
| 113 | + |
| 114 | +*pandas* uses 'repr()' instead of '%r' and '!r'. |
| 115 | + |
| 116 | +The use of 'repr()' will only happend when the value is not an obvious string. |
| 117 | + |
| 118 | +For example: |
| 119 | + |
| 120 | +**Good:** |
| 121 | + |
| 122 | +.. code-block:: python |
| 123 | +
|
| 124 | + raise ValueError(f"Unknown recived value, got: {repr(value)}") |
| 125 | +
|
| 126 | +**Good:** |
| 127 | + |
| 128 | +.. code-block:: python |
| 129 | +
|
| 130 | + raise ValueError(f"Unknown recived type, got: '{type(value).__name__}'") |
| 131 | +
|
| 132 | +**Bad:** |
| 133 | + |
| 134 | +.. code-block:: python |
| 135 | +
|
| 136 | + raise ValueError(f"Unknown recived type, got: {repr(type(value).__name__)}") |
| 137 | +
|
| 138 | +Single and double quotes |
| 139 | +------------------------ |
| 140 | + |
| 141 | +*pandas* uses single quotes when .... |
| 142 | + |
| 143 | +For example: |
| 144 | + |
| 145 | +**Good:** |
| 146 | + |
| 147 | +.. code-block:: python |
| 148 | +
|
| 149 | + placeholder = True |
| 150 | +
|
| 151 | +
|
| 152 | +**Bad:** |
| 153 | + |
| 154 | +.. code-block:: python |
| 155 | +
|
| 156 | + placeholder = True |
| 157 | +
|
| 158 | +And using double quotes when.... |
| 159 | + |
| 160 | +For example: |
| 161 | + |
| 162 | +**Good:** |
| 163 | + |
| 164 | +.. code-block:: python |
| 165 | +
|
| 166 | + placeholder = True |
| 167 | +
|
| 168 | +
|
| 169 | +**Bad:** |
| 170 | + |
| 171 | +.. code-block:: python |
| 172 | +
|
| 173 | + placeholder = True |
| 174 | +
|
| 175 | +Typing |
| 176 | +====== |
| 177 | + |
| 178 | +Annotating __init__ |
| 179 | +------------------- |
| 180 | + |
| 181 | +*pandas* does not annotate the '__init__' function. it is redundant |
| 182 | +as almost every '__init__' function will most like not to return anything. |
| 183 | + |
| 184 | +For example: |
| 185 | + |
| 186 | +**Good:** |
| 187 | + |
| 188 | +.. code-block:: python |
| 189 | +
|
| 190 | + def __init__(self, name): |
| 191 | + self.name = name |
| 192 | +
|
| 193 | +**Bad:** |
| 194 | + |
| 195 | +.. code-block:: python |
| 196 | +
|
| 197 | + def __init__(self, name) -> None: |
| 198 | + self.name = name |
| 199 | +
|
0 commit comments