Skip to content

DOC: Code style documentation #30129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 129 additions & 0 deletions doc/source/development/code_style.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
.. _code_style:

{{ header }}

=======================
pandas code style guide
=======================

.. contents:: Table of contents:
:local:

Patterns
========

foo.__class__
-------------

*pandas* uses 'type(foo)' instead 'foo.__class__' as it is making the code more
readable.

For example:

**Good:**

.. code-block:: python

foo = "bar"
type(foo)

**Bad:**

.. code-block:: python

foo = "bar"
foo.__class__


String formatting
=================

Concatenated strings
--------------------

f-strings
~~~~~~~~~

*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.

For example:

**Good:**

.. code-block:: python

foo = "old_function"
bar = "new_function"

my_warning_message = (
f"Warning, {foo} is deprecated, "
"please use the new and way better "
f"{bar}"
)

**Bad:**

.. code-block:: python

foo = "old_function"
bar = "new_function"

my_warning_message = (
f"Warning, {foo} is deprecated, "
f"please use the new and way better "
f"{bar}"
)

White spaces
~~~~~~~~~~~~

Putting the white space only at the end of the previous line, so
there is no whitespace at the beggining of the concatenated string.

For example:

**Good:**

.. code-block:: python

example_string = (
"Some long concatenated string, "
"with good placement of the "
"whitespaces"
)

**Bad:**

.. code-block:: python

example_string = (
"Some long concatenated string,"
" with bad placement of the"
" whitespaces"
)

Representation function (aka 'repr()')
--------------------------------------

*pandas* uses 'repr()' instead of '%r' and '!r'.

The use of 'repr()' will only happend when the value is not an obvious string.

For example:

**Good:**

.. code-block:: python

value = str
f"Unknown recived value, got: {repr(value)}"

**Good:**

.. code-block:: python

value = str
f"Unknown recived type, got: '{type(value).__name__}'"
3 changes: 1 addition & 2 deletions doc/source/development/contributing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -569,8 +569,7 @@ do not make sudden changes to the code that could have the potential to break
a lot of user code as a result, that is, we need it to be as *backwards compatible*
as possible to avoid mass breakages.

Additional standards are outlined on the `code style wiki
page <https://github.com/pandas-dev/pandas/wiki/Code-Style-and-Conventions>`_.
Additional standards are outlined on the `pandas code style guide <code_style>`_

Optional dependencies
---------------------
Expand Down
1 change: 1 addition & 0 deletions doc/source/development/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Development
:maxdepth: 2

contributing
code_style
maintaining
internals
extending
Expand Down
1 change: 1 addition & 0 deletions doc/source/index.rst.template
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ See the :ref:`overview` for more detail about what's in the library.
* :doc:`development/index`

* :doc:`development/contributing`
* :doc:`development/code_style`
* :doc:`development/internals`
* :doc:`development/extending`
* :doc:`development/developer`
Expand Down