Data Science · Level 1 Arrays

NumPy dtypes

Data from CSV files and forms often arrives as text. Convert it to real numbers with astype() before doing any maths.

  • Every array has one dtype
  • Text arrays can't do maths
  • astype(float)

An array holds a single type, its dtype. Numbers read from a file often arrive as text. Trap: sum() on a text array raises an error instead of adding. Convert first with astype.

raw = np.array(["4.5", "10"])
raw.dtype                 # dtype('<U3'): text
raw.astype(float).sum()   # 14.5

Write total_hours(entries) that receives timesheet hours as strings and returns their sum as a float, for example total_hours(["7.5", "8", "6.25"]) gives 21.75.

solution.py
Test Suites Pending
Output