Data Types and Casting

Python · NumPy Library

Theory

Every NumPy array has one dtype that describes how each element is stored: int64 for whole numbers (int32 on some 32-bit platforms, including Python running in a browser), float64 for decimals, bool for True/False and '<U5' for short strings. NumPy picks the dtype from the data, and mixing types upcasts to the most general one: np.array([1, 2.5]) becomes float64, and adding a string makes everything a string.

You change the type with astype, which always returns a new array. Casting float to int truncates toward zero, so 99.9 becomes 99 and -2.7 becomes -2; use np.round first if you want rounding. Text numbers such as np.array(['10', '20']).astype(int) become real integers. Smaller types like int8 or float32 save memory, but int8 only holds -128 to 127, so values outside that range wrap around silently.

Why we use it

The dtype decides memory use, speed and what maths is allowed. Knowing it explains surprises like floats appearing after you add one decimal, or text columns that refuse to add up until you cast them.

When to use it

Check .dtype after loading data. Cast text digits to numbers before maths, round before casting floats to int, and use float32 or int32 for very large arrays. Do not use small int types for values that can grow past their range.

How it works

  1. Array created or loaded
  2. Check arr.dtype
  3. Are numbers stored as text? Yes: arr.astype(int) or astype(float). No: Keep the numeric dtype.
  4. Converting floats to whole numbers? Yes: np.round(arr) first, then astype(int). No: Skip.
  5. Very large array? Yes: Consider float32 / int32 to save memory. No: Keep 64-bit defaults.
  6. Correct dtype for the maths ahead

Syntax

arr.dtype
arr.astype(int)
arr.astype('float32')
np.array([1, 2], dtype=np.int8)

Example

prices = np.array([99.9, 150.5])
print(prices.astype(int))            # [ 99 150]
print(np.round(prices).astype(int))  # [100 150]

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic