Series and DataFrame grain
Quick answer
A Series is a one-dimensional collection with index labels. A DataFrame is a table of named columns that share a row index; columns can have different dtypes. Grain means what one row represents, such as one reading from one sensor at one time. Decide the grain before choosing features or checking duplicates. Examples run locally on Python 3.13.5 and pandas 2.2.3; ByteMind package support is not verified. All data are synthetic.
Inspect a sensor table
The sensor ID is a label, not a numerical feature. Select a single column with brackets and inspect its type and index.
import pandas as pdreadings = pd.DataFrame({'id': ['s1', 's2', 's3'], 'temp': [20, 22, 21]})print(readings.shape)print(type(readings['temp']).__name__)print(readings.columns.tolist())print(readings['id'].is_unique)Expected output (pandas 2.2.3):
(3, 2)Series['id', 'temp']TrueThere are three rows and two columns. Unique IDs are a useful check only when the intended grain is one row per ID.
Align values by label
Series arithmetic matches labels rather than row positions. Here each sensor receives its own calibration offset.
import pandas as pdraw = pd.Series([20, 30], index=['a', 'b'])offset = pd.Series([3, 1], index=['b', 'a'])print((raw - offset).to_dict())print(raw.index.is_unique)print(raw.reindex(['b', 'a']).tolist())Expected output (pandas 2.2.3):
{'a': 19, 'b': 27}True[30, 20]Label a subtracts 1, not 3. Unmatched labels in ordinary arithmetic introduce missing values. Reindex explicitly changes label order; it does not invent observations.
Common mistakes
Do not assume the row index is a primary key. Repeated sensor IDs may be legitimate if rows represent separate readings. Check the full key, e.g. sensor and timestamp, with duplicated(subset=[...]). Do not convert to NumPy before aligning labels; it loses index semantics.
Practice and next steps
Sketch the grain and key for one sensor reading per minute. Predict the result of adding two Series whose label orders differ. Try the pure-Python reasoning challenge and knowledge check separately; these do not assess pandas API execution.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Series and DataFrame grain — knowledge check
Apply tabular data reasoning to new synthetic fixtures.
What does grain describe?
What does df["temp"] return for a unique column label?
A table has one row per device per day. Which candidate key fits?
Series a has x=6 and y=8; b has y=2 and x=1. What is (a-b)["x"]?
What happens to an unmatched label in ordinary Series addition?