Your country

Tools that support it use your country for local currency, number formats, units and paper size. Your choice is saved only in this browser.

Type a name or a two-letter code. Use the up and down arrow keys to move through the countries, Enter to choose one and Escape to close.

Data Analysis with Python (pandas, polars, DuckDB)  Module 1 – The data-analysis workflow and your toolkit

NumPy essentials for analysts

Why NumPy arrays are fast, how an array picks its dtype, where integers overflow, and how masks, np.where and np.select replace if-else loops.

  • Beginner
  • 25 minutes
  • Examples run with Python 3.14.8, pandas 3.0.6 and numpy 2.5.3
  • By MySmartCoPilot
Free lesson, Pro answers: 25 minutes
Free: the whole lesson. With a pass: the model answers to its 2 interview questions.

What you will learn

  • Explain why vectorised array operations beat Python loops, with a measured comparison
  • Predict the dtype of an array, including what a missing value does to integers
  • Compute conditional columns with boolean masks, np.where and np.select
  • Generate reproducible random data with a seeded Generator

Before you start

What you’ll learn · Before you start

What you will learn

  • Explain why vectorised array operations beat Python loops, with a measured comparison
  • Predict the dtype of an array, including what a missing value does to integers
  • Compute conditional columns with boolean masks, np.where and np.select
  • Generate reproducible random data with a seeded Generator

Before you start

On this page

A type too small for 40,000 × 75,000 can turn a positive order total negative. NumPy is the Python library for arrays: blocks of values of one type, stored side by side in memory, with operations that run over the whole block at once in compiled code. Analysts use it constantly, often without typing import numpy, because for the most part pandas keeps each numeric column of a DataFrame as a NumPy array (pandas user guide).

Four things about arrays decide whether column calculations are fast and right: why whole-array operations are fast, how an array chooses its type, where integers overflow without a word, and how to write an if-else rule for a whole column at once. This lesson takes each in turn on NumPy 2.5.3 and ends with random data that you, and anyone else, can reproduce.

An array dtype lets one compiled loop process its values

A Python list holds references, and each reference points to a separate object that carries its own type and size. A NumPy array holds plain values of a single type, its dtype: The representation used for each element of a NumPy array, including its type and byte size. One array applies that representation to all its elements., packed next to each other:

A Python list holds references to three separate int objects; a NumPy int64 array holds the three values side by side.Python list [3, 5, 8]int objects, each stored on its ownNumPy array, dtype int64: 3 values in 24 bytesreference 1reference 2reference 3int 3: type, size, valueint 5: type, size, valueint 8: type, size, value358

The same three numbers in a Python list and in a NumPy array

Text description of the diagram

The diagram has three boxes.

  1. "Python list [3, 5, 8]" holds three references, one above the other.
  2. Each reference points with an arrow to its own box in "int objects, each stored on its own": "int 3", "int 5" and "int 8", each with its type, its size and its value.
  3. Apart from these, "NumPy array, dtype int64: 3 values in 24 bytes" holds the plain values 3, 5 and 8 in one row of cells, side by side, with no references.

To add 1 to every number, Python follows each reference and works on each object in turn, while NumPy runs one compiled loop over the 24 bytes.

Because every value has the same type and size, NumPy can run one loop, written in C, over the whole block, without asking each value what it is. That is what vectorisation: Expressing a calculation as operations on whole arrays so a compiled implementation processes the elements rather than a Python loop handling them one at a time. means: you write one operation on whole arrays, such as quantity * price, and NumPy applies it to every element. An array also knows its shape (rows and columns), its dtype and its size in bytes, nbytes, which is simply the number of values times the size of one.

Read the array side of the diagram as the numeric-array case. The dtype describes how to interpret each value’s bytes, and nbytes measures the element buffer rather than every possible Python-object overhead. An object array, shown later, stores references and does not get the same benefit as this packed numeric array.

Checkpoint

Check: Dtype and array layout

  1. One question A numeric array holds 1,000 int64 values of 8 bytes each. How many bytes are in its element buffer?

    Type a number.

    Show the answer: One question

    Answer: 8000 bytes

    1,000 × 8 = 8,000 bytes. This is the element-buffer size, not a measure of all surrounding object overhead.

Measure speed only after the implementations agree

This program computes 100,000 order totals, quantity times price, in three ways. It first checks that all three give exactly the same values, then times each one with timeit and keeps its best round:

Order totals for 100,000 orders, timed three ways Python · speed/order_totals_speed.py
# Order totals (quantity × price) for 100,000 orders, three ways, timed with timeit.
import platform
import timeit

import numpy as np
import pandas as pd

rng = np.random.default_rng(7)
quantity = rng.integers(1, 6, size=100_000)  # 1 to 5 items
price = rng.integers(20, 500, size=100_000) * 1.0  # 20 to 499 a piece
q_list, p_list = quantity.tolist(), price.tolist()  # the same numbers as Python lists
orders = pd.DataFrame({"quantity": quantity, "price": price})


def pandas_apply():
    return orders.apply(lambda row: row["quantity"] * row["price"], axis=1)


def python_loop():
    totals = []
    for i in range(len(q_list)):
        totals.append(q_list[i] * p_list[i])
    return totals


def vectorised():
    return quantity * price


assert pandas_apply().tolist() == python_loop() == vectorised().tolist()  # all three give the same values
ways = [("pandas apply, axis=1", pandas_apply, 1, 3), ("Python loop", python_loop, 20, 7), ("NumPy vectorised", vectorised, 20, 7)]
best = {}
for name, fn, number, rounds in ways:
    best[name] = min(timeit.repeat(fn, number=number, repeat=rounds)) / number  # seconds per call, best round
    print(f"{name:<21} {best[name] * 1000:9.3f} ms")
loop, apply = best["Python loop"], best["pandas apply, axis=1"]
print(f"vectorised: {loop / best['NumPy vectorised']:.0f} times faster than the loop, {apply / best['NumPy vectorised']:.0f} times faster than apply")
print(f"CPython {platform.python_version()}, NumPy {np.__version__}, pandas {pd.__version__}, {platform.machine()}")

Output

pandas apply, axis=1    582.682 ms
Python loop               6.253 ms
NumPy vectorised          0.080 ms
vectorised: 79 times faster than the loop, 7324 times faster than apply
CPython 3.14.8, NumPy 2.5.3, pandas 3.0.6, arm64

This output changes from run to run: the times depend on the computer and on what else it is doing

Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 order_totals_speed.py

In the run recorded here, on a Mac with an Apple processor, the vectorised version took 0.080 ms, the Python loop 6.253 ms and pandas apply with axis=1 582.682 ms: the recorded speed-ups were 79 and 7,324 times. The ratios use the unrounded timings; the displayed milliseconds are rounded to three decimals. Your times will differ, and so will the next run’s, because they depend on the computer and on what else it is doing, but the gaps stay enormous. apply with axis=1 is the slowest because it builds a small Series for every row and calls your Python function on it, 100,000 times. A list comprehension writes the loop more compactly, yet it still handles one Python object at a time, so it lands near the loop, nowhere near NumPy.

Use the recorded times as evidence from one run, not a promise for every computer. The code first compares all three result sequences exactly, then measures repeated calls. Keep those two steps separate: a fast implementation that changes the result is not an equivalent replacement.

Check What it establishes
Compare all results before timing The three implementations agree on the measured input.
Repeat and record the toolchain The reported timings describe that run and environment.

Changing the hardware, workload or surrounding load can change the ratios, so rerun the program on the case you need.

A value that needs another representation changes the dtype

NumPy chooses one dtype that can hold every value you give it. Predict each line before you read the output:

Six arrays and the dtype NumPy picks for each Python · dtypes/predict_dtype.py
# What dtype does NumPy pick? Six small arrays, then the same missing value in pandas.
import numpy as np
import pandas as pd

examples = {
    "[3, 5, 8]": np.array([3, 5, 8]),
    "[3, 5, 8.5]": np.array([3, 5, 8.5]),
    "[3, 5, np.nan]": np.array([3, 5, np.nan]),
    "[3, 5, None]": np.array([3, 5, None]),
    "[True, False, 1]": np.array([True, False, 1]),
    '["3", 5, 8]': np.array(["3", 5, 8]),
}
for text, arr in examples.items():
    print(f"np.array({text:<16}) -> {str(arr.dtype):<8} {arr}")

print()
print("int64 uses", np.array([3, 5, 8]).itemsize, "bytes per value; a million values take",
      np.zeros(1_000_000, dtype=np.int64).nbytes // 1_000_000, "MB")
print(pd.Series([3, 5, None]).dtype, pd.Series([3, 5, None]).tolist())
print(pd.Series([3, 5, None], dtype="Int64").dtype, pd.Series([3, 5, None], dtype="Int64").tolist())

Output

np.array([3, 5, 8]       ) -> int64    [3 5 8]
np.array([3, 5, 8.5]     ) -> float64  [3.  5.  8.5]
np.array([3, 5, np.nan]  ) -> float64  [ 3.  5. nan]
np.array([3, 5, None]    ) -> object   [3 5 None]
np.array([True, False, 1]) -> int64    [1 0 1]
np.array(["3", 5, 8]     ) -> <U21     ['3' '5' '8']

int64 uses 8 bytes per value; a million values take 8 MB
float64 [3.0, 5.0, nan]
Int64 [3, 5, <NA>]

Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 predict_dtype.py

  • Whole numbers become int64, 8 bytes each, so a million of them take 8 MB.
  • One decimal turns the whole array into float64.
  • np.nan, the usual marker for a missing number, is itself a float, so a missing value turns a column of whole numbers into float64: 3 and 5 become 3.0 and 5.0.
  • None has no number type at all, so the array falls back to object: an array of references to Python objects, which loses the packed numeric representation and its usual speed advantage.
  • True and False become 1 and 0 next to an integer.
  • One string turns every value into a string (<U21 means Unicode text of up to 21 characters), so '3', '5' and '8' are no longer numbers and arithmetic on them fails.

pandas inherits the missing-value rule: the same three values give a float64 column. Its nullable Int64 dtype (with a capital I) keeps the integers and marks the gap as <NA> instead (nullable integers).

Checkpoint

Check: Predict dtype promotion

  1. One question The array includes [3, 5, None]. Which result does the recorded example show?

    Choose one answer.

    Show the answer: One question

    Answer: An object array

    Distinguish the cases: NaN is a float marker, while None leads this construction to an object array.

Widen the operands before a product can overflow

A Python int grows as large as it needs to and never overflows. NumPy’s integers have a fixed width, and when a result does not fit, it wraps around (data types):

When a NumPy integer is too small Python · overflow/int_overflow.py
# Fixed-width integers wrap around when a result does not fit.
import warnings

import numpy as np

small = np.array([120, 127], dtype=np.int8)  # int8 holds -128 to 127
print("int8 array + 1:", small + 1)  # wraps around, no warning

with warnings.catch_warnings(record=True) as caught:
    warnings.simplefilter("always")
    one = np.int8(127) + np.int8(1)  # a single value does warn
print("int8 scalar + 1:", one, "| warning:", caught[0].message)

quantity = np.array([3, 40_000], dtype=np.int32)  # items on each order line
price = np.array([19_900, 75_000], dtype=np.int32)  # price of one item, in paise
print("int32 line totals:", quantity * price)  # 40,000 x 75,000 = 3,000,000,000
print("int64 line totals:", quantity.astype(np.int64) * price)
print("largest int32:", np.iinfo(np.int32).max)
print("summing int32 values accumulates in", np.array([1, 2], dtype=np.int32).sum().dtype)

Output

int8 array + 1: [ 121 -128]
int8 scalar + 1: -128 | warning: overflow encountered in scalar add
int32 line totals: [      59700 -1294967296]
int64 line totals: [     59700 3000000000]
largest int32: 2147483647
summing int32 values accumulates in int64

Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 int_overflow.py

  • An int8 holds −128 to 127, so 127 + 1 in an array becomes −128, with no warning. The same sum on a single NumPy value does warn: overflow encountered in scalar add.
  • The int32 line totals are the dangerous case. 40,000 items at 75,000 paise (or cents) each make 3,000,000,000, more than the largest int32 (2,147,483,647), so the line total comes out as −1,294,967,296. Converting one column to int64 before multiplying gives the right total.
  • sum() is safer: for integer types smaller than the platform’s default integer, NumPy adds them up in that default integer, which is 64-bit on 64-bit computers (numpy.sum). Products and differences of two int32 columns get no such help.

The default integer has not always been 64-bit everywhere: before NumPy 2.0 it was 32-bit on Windows (NumPy 2.0.0 release notes), so the same code could overflow on a colleague’s Windows computer and not on yours. Keep counts and exact monetary amounts in int64 when their bounds fit (amounts as whole paise or cents), check the limits with np.iinfo, and convert before you multiply. Use floating point for approximate measurements with an explicit rounding policy rather than assuming fractional money is exact.

Follow the recorded order line and watch when widening must happen:

Interactive · stage by stage The same positive product in two integer widths Step through the two recorded operands, the narrow result and the multiplication after widening.
The same positive product in two integer widths, step 4 of 4. Showing Positive operands, Narrow limit, Wrapped result, Widen before multiplying. In focus: Widen before multiplying.Positive operands40,000 × 75,000Narrow limitint32 max 2,147,483,647Wrapped result−1,294,967,296Widen before multiplyingint64 gives 3,000,000,000

The drawing shows the whole scene; its 4 steps are listed below.

Every step as text
  1. Positive operands. Both quantity and price are positive int32 arrays. Their mathematical product is 3,000,000,000 paise.
  2. Narrow limit. The exact product exceeds the largest signed int32 value. The array operation keeps that fixed width rather than growing a Python integer.
  3. Wrapped result. The int32 array product wraps to a negative number without a warning. Converting this already wrong result cannot recover the lost value.
  4. Widen before multiplying. Convert quantity to int64 first, then multiply. The wider calculation holds the exact product shown by the example.

The conversion belongs before the arithmetic. Casting the wrapped result afterwards would merely store the wrong number in a wider type.

Key idea

Choose and validate the operand width before arithmetic; a wider result type cannot repair an earlier overflow.

The first matching fee condition wins

A delivery fee rule makes a good test: no fee for orders from 500 up, 25 for orders from 200 to below 500, and 40 for anything smaller. A loop would test each order in turn. NumPy does it with conditions on whole arrays:

Fee bands with np.select, np.where and boolean masks Python · fees/fee_bands.py
# Delivery fees by order value, without a loop: no fee from 500, 25 from 200, 40 below that.
import numpy as np

amount = np.array([120.0, 199.0, 199.5, 200.0, 350.0, 499.0, 500.0, np.nan])  # the last value is missing

fee = np.select([amount >= 500, amount >= 200], [0, 25], default=40)
print("amount:     ", amount)
print("fee:        ", fee)

wrong = np.select([amount >= 200, amount >= 500], [25, 0], default=40)  # the first true condition wins
print("wrong order:", wrong)

safe = np.select([np.isnan(amount), amount >= 500, amount >= 200], [np.nan, 0, 25], default=40)
print("NaN handled:", safe)

big = amount >= 500  # a boolean mask: one True or False per order
print("orders of 500 or more:", big.sum(), "of", amount.size, f"({big.mean():.1%})")
print("their values:", amount[big])
print("np.where, two bands:", np.where(amount >= 500, 0, 40))

Output

amount:      [120.  199.  199.5 200.  350.  499.  500.    nan]
fee:         [40 40 40 25 25 25  0 40]
wrong order: [40 40 40 25 25 25 25 40]
NaN handled: [40. 40. 40. 25. 25. 25.  0. nan]
orders of 500 or more: 1 of 8 (12.5%)
their values: [500.]
np.where, two bands: [40 40 40 40 40 40  0 40]

Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 fee_bands.py

The pieces, in the order the program uses them:

  • np.select(conditions, choices, default) handles a rule with several bands. For each element it takes the choice of the first condition that is true, and the default when none is (numpy.select). Test the exact boundaries: 199.5 pays 40 because the rule says “from 200”, while 200 pays 25 and 500 pays nothing.
  • The order of the conditions is part of the rule. In the “wrong order” line, amount >= 200 comes first and is also true for 500, so that order pays 25 instead of nothing. Put the narrowest band first.
  • A missing amount falls through to the default. The ordered comparisons with nan used here are false, so the order without an amount was charged the highest fee, 40, without any error. Putting np.isnan(amount) first, with np.nan as its choice, keeps a missing value missing.
  • A boolean mask: An array of True and False values, one per element, used to select the elements that satisfy a condition. such as amount >= 500 is an array of True and False, one per order. Its sum() counts the orders that match, its mean() gives their share, and amount[mask] selects them.
  • np.where(condition, if_true, if_false) is the two-band version: one condition, two outcomes (numpy.where).

Walk through the exact boundary from the recorded program; the condition order is part of the business rule:

Interactive · stage by stage One boundary value through the ordered fee rule Step through the order amount 500, then compare what the wrong condition order does.
One boundary value through the ordered fee rule, step 4 of 4. Showing Input amount, Test amount ≥ 500, Wrong order, Missing amount. In focus: Missing amount.Input amount500Test amount ≥ 500True gives fee 0Wrong orderamount ≥ 200 firstMissing amountTest missingness first

The drawing shows the whole scene; its 4 steps are listed below.

Every step as text
  1. Input amount. The amount is exactly 500. The written rule gives no fee for values from 500 upwards.
  2. Test amount ≥ 500. The first condition succeeds, so np.select chooses 0 and does not let a later overlapping band replace it.
  3. Wrong order. With the broader condition first, 500 matches it and receives fee 25. The later condition for 500 is not used.
  4. Missing amount. For NaN, the ordered comparisons used here are false. An explicit first missingness condition keeps its result missing instead of defaulting to fee 40.

The 500 boundary reveals overlapping conditions, and the missing amount reveals an unchecked default. Include both when comparing implementations.

Checkpoint

Check: Condition order and missing values

  1. One question With the intended ordered rule, what fee does amount 500 receive?

    Choose one answer.

    Show the answer: One question

    Answer: 0

    Test inclusivity at the exact boundary, and put the narrow high-amount band before the overlapping lower threshold.

Repeat a random stream with the seed and environment

Simulations, samples and practice tables need random numbers that come out the same every time the analysis runs. Make them from a Generator created with a seed:

A seeded Generator gives the same numbers on every run Python · random/seeded.py
# Random numbers you can repeat: a Generator made from a seed.
import numpy as np

rng = np.random.default_rng(42)
print("five dice, seed 42:   ", rng.integers(1, 7, size=5))
print("then two uniforms:    ", rng.random(2).round(4))

again = np.random.default_rng(42)  # a new Generator with the same seed starts the same stream
print("same seed, same dice: ", again.integers(1, 7, size=5))
print("seed 43:              ", np.random.default_rng(43).integers(1, 7, size=5))

baskets = np.random.default_rng(2026).lognormal(mean=5.5, sigma=0.6, size=1_000).round(2)
print("1,000 practice baskets: min", baskets.min(), "| median", np.median(baskets), "| max", baskets.max())

Output

five dice, seed 42:    [1 5 4 3 3]
then two uniforms:     [0.6974 0.0942]
same seed, same dice:  [1 5 4 3 3]
seed 43:               [4 4 3 1 4]
1,000 practice baskets: min 25.83 | median 250.075 | max 1705.71

Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 seeded.py

np.random.default_rng(42) returns a Generator whose numbers depend only on the seed, so a second Generator made with 42 repeats the first one’s dice exactly, and seed 43 gives different ones. Each Generator is an object that belongs to your code. The older style, np.random.seed(42) followed by functions such as np.random.randint, sets one hidden state that every part of a program shares, so any library that draws a random number shifts your sequence; NumPy keeps it only for backward compatibility (Random Generator).

A seed is not the whole story. NumPy’s guarantee of a repeatable stream holds only while nothing else changes: the NumPy build, the rest of the software around it and the hardware all have to match, and a new feature release is allowed to change what a Generator method returns (compatibility policy). That is one more reason to pin NumPy in the lock file, as the previous lesson did. The last line of the example draws 1,000 skewed values with a log-normal distribution, a shape close to real order values: most are small, a few are large, and the median, 250.075, sits far below the largest, 1,705.71.

Histogram Maker Paste a column of numbers, such as simulated order values, and see how skewed their distribution is. Mock Data Generator Make a practice table with columns you choose when you need more than one array of random numbers.

The printed dice are also a compact trace of stream position. Seed 42 first gives [1, 5, 4, 3, 3]; the following uniforms use later positions in that same stream. Creating another Generator with 42 starts over, while drawing extra values from the existing Generator advances it.

Action Meaning for the stream
Create a new Generator with the same seed and matching environment Start the same demonstrated sequence again.
Keep drawing from the existing Generator Continue from its current position.

Keep both the seed and the sequence of calls with the locked environment.

Practise the decisions

Practice that adapts to you

Practise this lesson

12 questions about 6 ideas of this lesson. Each answer is explained at once, and what you get right is kept in this browser.

  1. Question 1 of 12 What does a numeric array’s dtype specify?

    Choose one answer.

    Show the answer: Question 1 of 12

    Answer: How every stored element is represented

    Shape and dtype answer different questions: where values are arranged and how their bytes are interpreted.

  2. Question 2 of 12 Two million int64 values take how much space in their element buffer?

    Choose one answer.

    Show the answer: Question 2 of 12

    Answer: 16,000,000 bytes

    Buffer bytes equal the value count multiplied by the dtype’s item size.

  3. Question 3 of 12 What does the benchmark do before accepting its timing comparison?

    Choose one answer.

    Show the answer: Question 3 of 12

    Answer: Compare the three output sequences for equality

    Correctness is a precondition of comparing implementations as replacements.

  4. Question 4 of 12 Your rerun gives a different speed-up ratio but the output sequences still agree. What should you report?

    Choose one answer.

    Show the answer: Question 4 of 12

    Answer: The new measured ratio with its environment and workload

    Separate functional equivalence from performance evidence and retain the conditions of each timing.

  5. Question 5 of 12 What dtype does np.array([12, 7, np.nan]) choose in this example?

    Choose one answer.

    Show the answer: Question 5 of 12

    Answer: float64

    One dtype applies to the whole array, so the integers become floating-point values beside NaN.

  6. Question 6 of 12 You need pandas to keep the integer values 3 and 5 while marking the third value missing. Which displayed choice fits?

    Choose one answer.

    Show the answer: Question 6 of 12

    Answer: The nullable Int64 dtype

    The capital-I nullable Int64 type solves a different problem from NumPy’s plain int64 dtype.

  7. Question 7 of 12 What does an int8 array containing 127 produce when 1 is added?

    Choose one answer.

    Show the answer: Question 7 of 12

    Answer: −128 in the recorded array operation

    The dtype limits the result representation even when the mathematical result is straightforward.

  8. Question 8 of 12 quantity * price has already wrapped in int32. Which approach preserves the intended exact product?

    Choose one answer.

    Show the answer: Question 8 of 12

    Answer: Redo the multiplication after converting an operand to int64

    Widen and recompute from the original operands, then compare and validate the results.

  9. Question 9 of 12 When several np.select conditions are true at the same position, which choice is used?

    Choose one answer.

    Show the answer: Question 9 of 12

    Answer: The choice of the first true condition

    Write conditions in the intended priority order and include a documented default.

  10. Question 10 of 12 np.select checks amount >= 200 before amount >= 500, with choices 25 and 0. What fee is selected for amount 650?

    Choose one answer.

    Show the answer: Question 10 of 12

    Answer: 25

    The code’s first match decides the actual result. Compare that result with the written rule before trusting an optimized implementation.

  11. Question 11 of 12 What does creating a second Generator with the same seed do under the same supported conditions?

    Choose one answer.

    Show the answer: Question 11 of 12

    Answer: Start the demonstrated random stream again

    A seed is one part of reproducibility; the build, environment and call sequence still matter.

  12. Question 12 of 12 A teammate uses the same seed but draws two extra values before the dice. Why can the dice differ?

    Choose one answer.

    Show the answer: Question 12 of 12

    Answer: The extra calls advance the Generator’s state

    Reproduce the order and number of draws as well as the seed and environment.

Interview questions

Fresher · Why does a missing value turn a NumPy array of integers into float64?

A NumPy array has one dtype for all its elements, and the integer dtypes have no value left over that could mean “missing”. The usual marker for a missing number, np.nan, is a floating-point value, so an array that must hold 3, 5 and nan needs a type that can store all three, and NumPy picks float64. The whole numbers become 3.0 and 5.0, which looks harmless but changes how they print, compare and join, and whole numbers above 2⁵³ can no longer all be stored exactly. pandas behaves the same way for its default int64 columns, while its nullable Int64 dtype keeps the integers and marks the gap separately, as <NA>.

Model answers open with any pass: ₹79 for 1 week ($2.99), one payment that never renews.

  1. Fresher to mid

    You replaced a Python loop that computes delivery fees with np.select. On 2 million orders the new fee total is 4,320 higher than the old one. How do you find the orders that differ, and how do you decide which version is right?

    Model answer, Pro. Model answers open with any pass; prices are in the notice on this page. First step and follow-ups

  2. Fresher to mid

    Some line totals in a sales report are negative, although every quantity and every price is positive. Both columns are stored as int32. Explain what happened, and how you would guard the pipeline against it.

    Model answer, Pro. First step and follow-ups

Key takeaways

  • An array holds one dtype in one block of memory, so a vectorised operation runs as one compiled loop: in the run above it beat a Python loop and pandas apply by orders of magnitude.
  • NumPy picks a dtype that fits every value: a missing value makes integers float64, None makes object, and one string makes everything text.
  • Fixed-width integers wrap around silently in arrays; multiply in int64 and check limits with np.iinfo.
  • np.where handles two bands and np.select many; the first true condition wins, and nan falls through to the default unless you test for it first.
  • Make random data with np.random.default_rng(seed) and pin the NumPy version to repeat it exactly.

Terms in this lesson

boolean mask
An array of True and False values, one per element, used to select the elements that satisfy a condition.
dtype
The representation used for each element of a NumPy array, including its type and byte size. One array applies that representation to all its elements.
vectorisation
Expressing a calculation as operations on whole arrays so a compiled implementation processes the elements rather than a Python loop handling them one at a time.

Corrections

  • Specified that NaN fails the ordered comparisons used in the fee rule; not every possible comparison with NaN is false.

References

Related tools

Report a problem with this lesson

Quick answers and tool search

Type to search tools or to get a quick answer, for example 18% of 2500. Use the up and down arrow keys to move through the results, Enter to choose, and Escape to close.