Ravindra BagaleCourses & study guides मराठी Track your progress

4. NumPy Essentials

Data Science

4.2 dtypes

dtype Use
int64 / int32 Counts, qty
float64 Amounts, minutes
bool Flags
object Mixed / text (slow)
amount_txt = np.array(["64", "90", "240"])
# wrong for math — convert:
amount_num = amount_txt.astype(float)
print(amount_num.sum())

Ravindra Bagale's Tip

Amounts that come from a CSV often arrive as text, so a sum gives an error or a wrong result. In NumPy, convert them with astype(float) and check the dtype before you add them. The same idea is behind astype and pd.to_numeric() in pandas, so learn dtypes well here.