Table of contents:
Patterns
foo.__class__
String formatting
Concatenated strings
f-strings
White spaces
Representation function (aka ‘repr()’)
Imports (aim for absolute)
pandas uses ‘type(foo)’ instead ‘foo.__class__’ as it is making the code more readable.
For example:
Good:
foo = "bar" type(foo)
Bad:
foo = "bar" foo.__class__
pandas uses f-strings formatting instead of ‘%’ and ‘.format()’ string formatters.
The convention of using f-strings on a string that is concatenated over serveral lines, is to prefix only the lines containing the value needs to be interpeted.
foo = "old_function" bar = "new_function" my_warning_message = ( f"Warning, {foo} is deprecated, " "please use the new and way better " f"{bar}" )
foo = "old_function" bar = "new_function" my_warning_message = ( f"Warning, {foo} is deprecated, " f"please use the new and way better " f"{bar}" )
Putting the white space only at the end of the previous line, so there is no whitespace at the beggining of the concatenated string.
example_string = ( "Some long concatenated string, " "with good placement of the " "whitespaces" )
example_string = ( "Some long concatenated string," " with bad placement of the" " whitespaces" )
pandas uses ‘repr()’ instead of ‘%r’ and ‘!r’.
The use of ‘repr()’ will only happend when the value is not an obvious string.
value = str f"Unknown recived value, got: {repr(value)}"
value = str f"Unknown recived type, got: '{type(value).__name__}'"
In Python 3, absolute imports are recommended. In absolute import doing something like import string will import the string module rather than string.py in the same directory. As much as possible, you should try to write out absolute imports that show the whole import chain from toplevel pandas.
import string
string.py
Explicit relative imports are also supported in Python 3. But it is not recommended to use it. Implicit relative imports should never be used and is removed in Python 3.
# preferred import pandas.core.common as com # not preferred from .common import test_base # wrong from common import test_base