Course outline

pandas / Basic: labels, types and accepted input / Lesson 1

Series, DataFrames and labeled axes

Define what each row represents and distinguish labeled pandas objects from ordinary positional containers.

About 5 min readContent reviewed
On this page

Lesson goals

  • Explain Series, DataFrame, index and column responsibilities.
  • State row grain, identity and shape before calculating a result.

Start with the meaning of one row

A synthetic inventory table can contain one row per item, one row per item and depot, or one row per movement. These are different analytical grains even when column names look similar. Counting rows as items is correct only under the corresponding identity rule. Write the grain, units and source interval before loading data. pandas helps represent and transform tables, but it does not infer business uniqueness from a column called item_id. An index label can identify a row for selection while still containing duplicates, so an explicit uniqueness check remains necessary when the analysis depends on one row per entity.

A Series and a DataFrame carry labels

A Series is one-dimensional labeled data with a dtype and an index. A DataFrame is two-dimensional labeled tabular data whose columns can have different dtypes. Row index and column labels participate in selection and alignment; they are not merely decorative spreadsheet headings. shape reports axis lengths, while size counts cells rather than business entities. A DataFrame column selection can return a Series, whereas selecting a list of columns returns a DataFrame. Keep this dimensional distinction visible in helper-function contracts so a later method does not unexpectedly receive a scalar or one-dimensional result when it expects a table.

Read an explicit two-item frame

The illustrative pandas program creates two inventory rows and moves item into the row index. The declared shape becomes two rows and one data column; selecting I2 and units yields three. The assertions describe the expected result derived from the fixture and official object semantics, not a performed pandas runtime test. Setting an index does not automatically enforce uniqueness unless the chosen operation or an explicit check requests it. Retain a stable business key separately when exporting or resetting the index, because a default integer index can change after filtering, concatenation or reordering and should not silently become entity identity.

python Illustrative labeled inventory frame
import pandas as pd

frame = pd.DataFrame({'item': ['I1', 'I2'], 'units': [2, 3]}).set_index('item')
assert frame.shape == (2, 1)
assert frame.loc['I2', 'units'] == 3
print(frame.loc['I2', 'units'])
Expected output
3

Expected output is 3 from the declared fixture; pandas is not installed and this program was not executed.

Check a small source identity contract offline

The standard-library example inspects two plain dictionaries, verifies their expected fields and checks unique item identifiers. It establishes that supplied source records satisfy this small contract before any pandas construction. It is not a replacement for pandas dtype, alignment or index behavior. A duplicate item might be valid at movement grain but invalid at item grain, so the check must follow the stated analysis rather than a universal rule to remove duplicates. Validate missing identifiers and unexpected fields deliberately. Automatically dropping a row can make a table look clean while silently changing totals and concealing a source problem.

python Offline source-record contract check
rows = [{'item': 'I1', 'units': 2}, {'item': 'I2', 'units': 3}]
assert all(set(row) == {'item', 'units'} for row in rows)
assert len({row['item'] for row in rows}) == len(rows)
print(len(rows))
Expected output
2

Two supplied item records have the expected fields and unique IDs; no pandas behavior is exercised.

Record the library baseline and evidence boundary

This course selects the official pandas 3.1.0 documentation baseline, including Copy-on-Write and string behavior introduced in 3.0. It does not claim the latest release or current support. pandas is absent from the authoring environment, so every pandas program remains runnable:false and its expected result is reasoned rather than observed. Only complete standard-library models and parsers are executed in isolated temporary storage. Do not infer engine compatibility, memory use or performance from those models. A real analytical acceptance record should include exact pandas/Python versions, optional backends, source schema and the business invariant being tested.

Common errors and fixes

Row count is reported as unique item count without checking grain.

Why it happens
A movement-level source was mistaken for one row per item.
How to fix it
State the business grain and count the required unique identity explicitly.
Prevent it next time
Keep row-grain and uniqueness expectations with the source contract.

A default integer index is used as permanent item identity.

Why it happens
Positional convenience was confused with a business key.
How to fix it
Retain and validate an explicit item key through transformations and exports.
Prevent it next time
Test identity preservation after sorting and concatenation.

A helper expecting a table receives a Series unexpectedly.

Why it happens
Single-column and list-of-columns selection were treated identically.
How to fix it
Specify dimensionality and use the appropriate selection form.
Prevent it next time
Check shape and return-type contracts at helper boundaries.

Check your understanding

1. Can two rows with the same item identifier be legitimate data?

Show worked solution

Yes, if the declared grain is one movement or one item-depot observation rather than one item. Decide which fields identify a record before removing anything. Deduplication without that contract can erase valid activity and alter totals, while a true item-level table should reject or reconcile an unexpected repeated identity.

2. Does the source-dictionary assertion prove the pandas frame has the intended dtypes?

Show worked solution

No. It verifies fields and source identity only. Construction and dtype inference are separate pandas behavior, affected by explicit dtype choices and library version. Keep the local contract check and obtain authorized engine evidence separately when dtype or backend compatibility is the claim under review.

Key takeaways

  • Define row grain and identity before choosing labels or calculating totals.
  • pandas objects carry dimensional, label and dtype contracts together.

Official references