|
| 1 | + |
| 2 | +# Pandas Code Guidelines |
| 3 | + |
| 4 | +This document consolidates all coding style and contribution guidelines for Pandas developers into one place. Please follow these rules to ensure clean, consistent, and maintainable code. |
| 5 | + |
| 6 | +--- |
| 7 | + |
| 8 | +## ✍️ Code Formatting |
| 9 | + |
| 10 | +### 🔹 Use `black` |
| 11 | +- Pandas uses [Black](https://black.readthedocs.io/en/stable/) to automatically format code. |
| 12 | +- Run `black` before committing: |
| 13 | + ```bash |
| 14 | + black pandas/ |
| 15 | + ``` |
| 16 | + |
| 17 | +### 🔹 Line Length |
| 18 | +- Limit lines to **88 characters** (as per Black). |
| 19 | +- For docstrings or comments, use **80 characters** when possible. |
| 20 | + |
| 21 | +--- |
| 22 | + |
| 23 | +## 🚫 Linting & Static Checks |
| 24 | + |
| 25 | +### 🔹 Use `flake8` |
| 26 | +- Run `flake8` to check for style violations: |
| 27 | + ```bash |
| 28 | + flake8 pandas/ |
| 29 | + ``` |
| 30 | + |
| 31 | +### 🔹 Use `isort` for import sorting |
| 32 | +- Keeps imports grouped and ordered. |
| 33 | +- Run: |
| 34 | + ```bash |
| 35 | + isort pandas/ |
| 36 | + ``` |
| 37 | + |
| 38 | +--- |
| 39 | + |
| 40 | +## 📦 Imports |
| 41 | + |
| 42 | +- Standard library imports first |
| 43 | +- Third-party packages next (e.g., `numpy`, `pytest`) |
| 44 | +- Local pandas imports last |
| 45 | + |
| 46 | +**Example:** |
| 47 | +```python |
| 48 | +import os |
| 49 | +import numpy as np |
| 50 | +from pandas.core.frame import DataFrame |
| 51 | +``` |
| 52 | + |
| 53 | +--- |
| 54 | + |
| 55 | +## 🔤 Strings |
| 56 | + |
| 57 | +- Use **f-strings** for formatting: |
| 58 | + ```python |
| 59 | + name = "Abu" |
| 60 | + print(f"Hello, {name}") |
| 61 | + ``` |
| 62 | + |
| 63 | +- Prefer `repr()` over `str()` for debugging. |
| 64 | +- Avoid unnecessary whitespace inside `{}`: |
| 65 | + ```python |
| 66 | + f"{x}" ✅ |
| 67 | + f"{ x }" ❌ |
| 68 | + ``` |
| 69 | + |
| 70 | +--- |
| 71 | + |
| 72 | +## 🧪 Testing Guidelines |
| 73 | + |
| 74 | +- Use `pytest` framework. |
| 75 | +- Test files should be placed in `pandas/tests`. |
| 76 | +- Use fixtures instead of setup/teardown methods. |
| 77 | +- Mark known failures: |
| 78 | + ```python |
| 79 | + @pytest.mark.xfail(reason="Issue #12345") |
| 80 | + def test_example(): |
| 81 | + ... |
| 82 | + ``` |
| 83 | + |
| 84 | +- Run all tests with: |
| 85 | + ```bash |
| 86 | + pytest pandas/ |
| 87 | + ``` |
| 88 | + |
| 89 | +--- |
| 90 | + |
| 91 | +## 👨🔧 General Python Style |
| 92 | + |
| 93 | +- Follow [PEP8](https://pep8.org/) |
| 94 | +- Avoid `type(x) == y` — prefer `isinstance(x, y)` |
| 95 | +- Use `is` for `None` comparison: |
| 96 | + ```python |
| 97 | + if x is None: |
| 98 | + ... |
| 99 | + ``` |
| 100 | + |
| 101 | +- Avoid importing inside functions unless necessary. |
| 102 | +- Avoid `import *` |
| 103 | + |
| 104 | +--- |
| 105 | + |
| 106 | +## 🧠 Function/Variable Naming |
| 107 | + |
| 108 | +| Type | Style | Example | |
| 109 | +|--------------|--------------|-----------------| |
| 110 | +| Function | `snake_case` | `compute_mean()`| |
| 111 | +| Variable | `snake_case` | `total_count` | |
| 112 | +| Class | `PascalCase` | `DataFrame` | |
| 113 | +| Constant | `UPPER_CASE` | `MAX_ROWS` | |
| 114 | + |
| 115 | +--- |
| 116 | + |
| 117 | +## 📝 Docstrings |
| 118 | + |
| 119 | +- Use [numpydoc](https://numpydoc.readthedocs.io/en/latest/format.html) format. |
| 120 | +- Include: |
| 121 | + - Short summary |
| 122 | + - Parameters |
| 123 | + - Returns |
| 124 | + - Examples |
| 125 | + |
| 126 | +**Example:** |
| 127 | +```python |
| 128 | +def compute_mean(data): |
| 129 | + ''' |
| 130 | + Compute the mean of a list. |
| 131 | +
|
| 132 | + Parameters |
| 133 | + ---------- |
| 134 | + data : list of float |
| 135 | + The input data. |
| 136 | +
|
| 137 | + Returns |
| 138 | + ------- |
| 139 | + float |
| 140 | + Mean of the input. |
| 141 | + ''' |
| 142 | +``` |
| 143 | + |
| 144 | +--- |
| 145 | + |
| 146 | +## 🔁 Pre-commit Hook |
| 147 | + |
| 148 | +- Pandas uses `pre-commit` to automate linting and formatting: |
| 149 | + ```bash |
| 150 | + pip install pre-commit |
| 151 | + pre-commit install |
| 152 | + ``` |
| 153 | + |
| 154 | +--- |
| 155 | + |
| 156 | +## 💡 Git & Pull Request Guidelines |
| 157 | + |
| 158 | +- Use meaningful commit messages. |
| 159 | +- Format: `DOC: Add new section to code guidelines (#issue-number)` |
| 160 | +- PRs should be small and focused. |
| 161 | +- Always reference the issue being addressed. |
| 162 | + |
| 163 | +--- |
| 164 | + |
| 165 | +## 📚 Reference |
| 166 | + |
| 167 | +- [Pandas Contribution Guide](https://pandas.pydata.org/docs/development/contributing.html) |
| 168 | +- [Pandas Code Style](https://pandas.pydata.org/docs/development/code_style.html) |
| 169 | +- [PEP8](https://pep8.org/) |
| 170 | +- [Black](https://black.readthedocs.io/en/stable/) |
| 171 | +- [pytest](https://docs.pytest.org/en/stable/) |
| 172 | + |
| 173 | +--- |
| 174 | + |
| 175 | +*This file was created to unify the code formatting and contribution expectations for new and experienced developers working on Pandas. Following these guidelines will help ensure high-quality and maintainable code.* |
0 commit comments