Data Types & Conversion

Python · Pandas Basics

Theory

Every column has a dtype: int64, float64, bool, object (usually text), datetime64[ns] or category. Wrong types are common after loading - amounts read as text because of a stray comma, dates read as strings - and they break maths, sorting and joins. df.dtypes shows them and df['col'].astype('int64') converts when every value is valid.

For messy data use the forgiving converters. pd.to_numeric(s, errors='coerce') turns anything that is not a number into NaN instead of raising an error, so you can find and fix the bad rows. pd.to_datetime(s, format='%d-%m-%Y') parses dates (errors='coerce' gives NaT for bad ones). astype('category') stores repeated labels like city names once, saving memory and allowing a custom sort order. Integer columns that contain NaN become float64; use the nullable 'Int64' dtype to keep whole numbers with missing values.

Why we use it

Correct types make calculations right and fast: you cannot sum text, sort dates stored as strings correctly or join an int key to a text key. Converting early prevents subtle bugs later.

When to use it

Check dtypes right after loading. Use astype when data is clean, to_numeric/to_datetime with errors='coerce' when it may not be, and category for low-cardinality text columns. Join keys must have the same dtype on both sides.

How it works

  1. Check df.dtypes after loading
  2. Is the column already the right type? Yes: Leave it. No: Pick a converter.
  3. Are all values valid? Yes: Use astype(...). No: Use to_numeric / to_datetime with errors='coerce'.
  4. Inspect rows that became NaN or NaT and fix the source
  5. Correctly typed DataFrame

Syntax

df['col'].astype('int64' | 'float64' | 'string' | 'category')
pd.to_numeric(s, errors='coerce')
pd.to_datetime(s, format='%Y-%m-%d', errors='coerce')

Example

df['amount'] = pd.to_numeric(df['amount'], errors='coerce')
df['order_date'] = pd.to_datetime(df['order_date'])

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