Tech

Python for data

Read NumPy and pandas code the way a desk quant does: views and copies, axes and broadcasting, NaN everywhere, joins that add rows, and backtests that do not peek at tomorrow.

Read first: Coding and algorithms

7 concepts7 min read25-question test, from a bank of 33Updated

Concept 1 of 71 min

NumPy: views, axes, broadcasting and masks

The rule
A NumPy slice is a view, so changing it changes the original unless you call .copy(), while a list slice is a copy.
Worked examples

A slice is a view

Given
anp.arange(6)
ba[2:4]
  1. a is [0 1 2 3 4 5]
  2. b views the 2 and the 3
  3. b[0] = 50 makes a [0 1 50 3 4 5]

Setting b changed a too, while a[2:4].copy() would have left a as it was.

The axis that disappears

Given
Array[[2, 4], [6, 8]]
axis0
  1. First column: (2 + 6) ÷ 2 = 4
  2. Second column: (4 + 8) ÷ 2 = 6

axis=0 averages down each column, giving 4 and 6.

The picture
Formula
b=a[1:3].copy()
a.mean(axis=0) per column, axis=1 per row
(n,) with (m, 1) gives (m, n)
x[x > k].sum()
Why it holds

An array times 2 doubles each element where a list times 2 repeats it. Setting axis=0 collapses the rows to one value per column, broadcasting stretches (3,) and (2, 1) into (2, 3), and a boolean mask filters without a loop.

Views make NumPy fast on large data but surprise anyone used to lists. Mixing lists and arrays silently changes lengths: the list [1, 2] multiplied by 2 has 4 items.

Read axis as 'the axis that disappears': axis=0 averages down each column, axis=1 across each row.

Broadcasting is powerful and a common source of silent shape bugs, so check .shape when results look odd. x[x > 2] keeps the matching items, replacing explicit loops with compiled filters.

Watch out
  • Expecting a NumPy slice to be independent.
  • Mixing up axis=0 and axis=1.
  • Multiplying a list and expecting element-wise results.
In the interview
  1. A colleague's function modifies a NumPy array it was given and the caller's data changes too. Why, and how do you make it safe?

    A complete answer covers

    • Arrays are passed by reference and slices are views.
    • In-place operations change the caller's memory.
    • Copy at the boundary with .copy() when the function must modify.
    • Or write the function to return a new array.

Also in our technical interviews, on camera, with a score.

Concept 2 of 71 min

NaN: missing values that spread

The rule
NaN is a float that equals nothing, itself included, so test it with np.isnan or pd.isna.

The why, the charts, the worked example and the interview questions are in Essentials, with every quiz.

See the plansSign in

Concept 3 of 71 min

pandas indexing and assignment

The rule
Select and assign in a single .loc call, df.loc[df.a > 0, 'b'] = 1, because chained indexing writes into a temporary copy.

The why, the charts, the worked example and the interview questions are in Essentials, with every quiz.

See the plansSign in

Concept 4 of 71 min

Merges, counts and speed

The rule
A left merge repeats each left row once per matching right row, so duplicate right keys add rows.

The why, the charts, the worked example and the interview questions are in Essentials, with every quiz.

See the plansSign in

Concept 5 of 71 min

Time series in pandas: returns, windows and dates

The rule
Shift a signal with .shift(1) before multiplying by returns, and compound returns with (1 + r).cumprod().

The why, the charts, the worked example and the interview questions are in Essentials, with every quiz.

See the plansSign in

Concept 6 of 71 min

Python traps that come up with data

The rule
Python's // rounds towards minus infinity and % takes the divisor's sign, while == compares values and is compares identity.

The why, the charts, the worked example and the interview questions are in Essentials, with every quiz.

See the plansSign in

Concept 7 of 71 min

AdvancedWhy NumPy is fast: memory layout

The rule
A NumPy float64 array stores each number in 8 contiguous bytes, while a Python list stores an 8-byte pointer to a separate 24-byte float object.

The why, the charts, the worked example and the interview questions are in Essentials, with every quiz.

See the plansSign in

Recap

  • A NumPy slice is a view, a list slice a copy; * repeats a list but multiplies an array element by element.
  • NaN is a float, never equal to itself; pandas skips it in sums and means, NumPy does not.
  • Index and assign in one .loc call; combine conditions with &, | and ~, each in brackets.
  • A left join adds rows when the right key repeats; overlapping columns get _x and _y.
  • Shift signals by 1 day before multiplying by returns, compound with (1 + r).cumprod(), and say dayfirst=True for European dates.
Now test it

25 questions on Python for data

Timed, like the real one, drawn from a bank of 33. Every answer explained.

Test yourself →