Skip to content

Commit de05cf1

Browse files
DOC: Clarify parentheses vs. brackets usage (GH#62314)
1 parent e503c13 commit de05cf1

File tree

4 files changed

+201
-0
lines changed

4 files changed

+201
-0
lines changed

doc/source/getting_started/index.rst

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,49 @@ Data sets often contain more than just numerical data. pandas provides a wide ra
520520

521521
:ref:`To user guide <text>`
522522

523+
.. raw:: html
524+
525+
</span>
526+
</div>
527+
</div>
528+
</div>
529+
</div>
530+
531+
<div class="card tutorial-card">
532+
<div class="card-header collapsed card-link" data-bs-toggle="collapse" data-bs-target="#collapseEleven">
533+
<div class="d-flex flex-row tutorial-card-header-1">
534+
<div class="d-flex flex-row tutorial-card-header-2">
535+
<button class="btn btn-dark btn-sm"></button>
536+
How to use parentheses vs square brackets?
537+
</div>
538+
<span class="badge gs-badge-link">
539+
540+
:ref:`Straight to tutorial...<10min_tut_11_brackets_vs_parenthesis>`
541+
542+
.. raw:: html
543+
544+
</span>
545+
</div>
546+
</div>
547+
<div id="collapseEleven" class="collapse" data-parent="#accordion">
548+
<div class="card-body">
549+
550+
Understanding the difference between parentheses and square brackets is crucial for pandas users. Parentheses are used for function calls and grouping, while square brackets are for indexing and selection.
551+
552+
.. raw:: html
553+
554+
<div class="d-flex flex-row">
555+
<span class="badge gs-badge-link">
556+
557+
:ref:`To introduction tutorial <10min_tut_11_brackets_vs_parenthesis>`
558+
559+
.. raw:: html
560+
561+
</span>
562+
<span class="badge gs-badge-link">
563+
564+
:ref:`To user guide <indexing>`
565+
523566
.. raw:: html
524567

525568
</span>
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
.. _10min_tut_11_brackets_vs_parenthesis:
2+
3+
Parentheses vs. Square Brackets in Python and pandas
4+
====================================================
5+
6+
Python and pandas use both **parentheses** ``()`` and **square brackets** ``[]``, but these have separate and important roles. Understanding their differences is essential for writing correct Python and pandas code.
7+
8+
Overview of Bracket Usage
9+
-------------------------
10+
11+
+------------------------------------+-------------------------------+------------------------------+
12+
| Context | Parentheses ``()`` | Square Brackets ``[]`` |
13+
+====================================+===============================+==============================+
14+
| Function and method calls | Yes: ``df.mean()`` | No |
15+
+------------------------------------+-------------------------------+------------------------------+
16+
| Tuple creation | Yes: ``(a, b, c)`` | No |
17+
+------------------------------------+-------------------------------+------------------------------+
18+
| List creation and item access | No | Yes: ``a[0]``, ``[1, 2]`` |
19+
+------------------------------------+-------------------------------+------------------------------+
20+
| Dictionary item access | No | Yes: ``mydict['key']`` |
21+
+------------------------------------+-------------------------------+------------------------------+
22+
| pandas column selection | No | Yes: ``df['A']``, |
23+
| | | ``df[['A', 'B']]`` |
24+
+------------------------------------+-------------------------------+------------------------------+
25+
| pandas row selection and slicing | No | Yes: ``df[0:5]``, |
26+
| | | ``df.loc[2]`` |
27+
+------------------------------------+-------------------------------+------------------------------+
28+
| Boolean indexing | No | Yes: ``df[df['col'] > 10]`` |
29+
+------------------------------------+-------------------------------+------------------------------+
30+
| Grouping/logical expressions | Yes: ``(x + y) * z`` | No |
31+
+------------------------------------+-------------------------------+------------------------------+
32+
33+
Detailed Explanations and Examples
34+
----------------------------------
35+
36+
**Parentheses ``()``**
37+
38+
- Used to *call* functions and methods, enclosing arguments:
39+
.. code-block:: python
40+
41+
df.mean()
42+
print("Hello, world!")
43+
sum([1, 2, 3])
44+
45+
- Used to create *tuples*, which are immutable ordered collections:
46+
.. code-block:: python
47+
48+
coordinates = (10, 20)
49+
empty_tuple = ()
50+
51+
- Used for *grouping expressions* in mathematics and logic:
52+
.. code-block:: python
53+
54+
result = (1 + 2) * 3
55+
is_valid = (score > 0) and (score < 100)
56+
57+
- Used to spread Python statements over multiple lines:
58+
59+
.. code-block:: python
60+
61+
total = (
62+
1 +
63+
2 +
64+
3
65+
)
66+
67+
**Square Brackets ``[]``**
68+
69+
- Used to *define* and *access* elements of Python lists:
70+
.. code-block:: python
71+
72+
numbers = [1, 2, 3, 4]
73+
first = numbers[0]
74+
sub = numbers[1:3]
75+
76+
- Used to *access* values in dictionaries by key:
77+
.. code-block:: python
78+
79+
prices = {'apple': 40, 'banana': 10}
80+
apple_price = prices['apple']
81+
82+
- Used for all kinds of *indexing* and *selection* in pandas DataFrames and Series:
83+
84+
*Single column selection* (returns Series):
85+
.. code-block:: python
86+
87+
df['A']
88+
89+
*Multiple columns selection* (returns DataFrame):
90+
.. code-block:: python
91+
92+
df[['A', 'B']]
93+
94+
Here, the inner brackets create a Python list of column labels, and the outer brackets are pandas selection syntax.
95+
96+
*Row selection and slicing*:
97+
.. code-block:: python
98+
99+
df[0:2] # selects rows 0 and 1 by integer position
100+
df.loc[2] # selects row with label/index 2
101+
df.iloc[2] # selects row at integer position 2
102+
103+
*Boolean indexing (row filtering)*:
104+
.. code-block:: python
105+
106+
df[df['A'] > 5] # returns only rows where column 'A' is greater than 5
107+
108+
Key Points to Remember
109+
----------------------
110+
111+
- **Parentheses** are for function/method calls, tuple creation, grouping, and continuation of statements.
112+
- **Square brackets** are for creating and accessing lists, dictionary values, slicing sequences, and—critically for pandas—selecting/subsetting columns and rows.
113+
- In pandas, *single* square brackets select a single column as a Series (``df['A']``), while *double* square brackets select multiple columns as a DataFrame (``df[['A', 'B']]``) because the *inner brackets create a Python list* of column labels.
114+
- Boolean indexing in pandas always uses square brackets enclosing a boolean Series: ``df[df['A'] > 5]``.
115+
116+
Common Pitfalls
117+
---------------
118+
119+
- Attempting to use parentheses for list/tensor/column access or slicing will result in errors.
120+
- Using single brackets with a list inside (like ``df[['A']]``) still returns a DataFrame, not a Series—bracket count and the type of object inside matters.
121+
- Remember that method calls (like ``df.mean()`` or ``df.groupby('A')``) always require parentheses, even if there are no arguments.
122+
123+
Example Summary
124+
---------------
125+
126+
.. code-block:: python
127+
128+
import pandas as pd
129+
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
130+
131+
# Function and method calls
132+
df.mean()
133+
134+
# Tuple creation
135+
t = (1, 2, 3)
136+
137+
# List creation/access
138+
mylist = [10, 20, 30]
139+
first_item = mylist[0]
140+
141+
# pandas column selection
142+
df['A']
143+
df[['A', 'B']]
144+
145+
# pandas boolean indexing
146+
df[df['B'] > 4]
147+
148+
# Grouping/logical expressions
149+
selected = (df['A'] > 1) & (df['B'] < 6)
150+
151+
Getting comfortable with the distinction between parentheses and square brackets is a major milestone for every new pandas user. This understanding leads to correct code and enhanced productivity when working in both core Python and pandas.

doc/source/getting_started/intro_tutorials/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ Getting started tutorials
1919
08_combine_dataframes
2020
09_timeseries
2121
10_text_data
22+
11_brackets_vs_parenthesis

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)