Introduction to Data Types in Python for Data Science

Datatypes Mar 19, 2026

Introduction to Data Types in Python for Data Science – Complete Guide 2026

Data types are the foundation of every data science project. Choosing the right data type directly impacts memory usage, processing speed, and code reliability. In 2026, mastering Python and pandas data types is one of the quickest ways to make your workflows faster and more efficient.

TL;DR — Why Data Types Matter in Data Science

  • Wrong data types can waste 5x–10x more memory
  • Proper types make operations faster and prevent bugs
  • Modern pandas offers nullable types and category for real-world data

1. Core Python Data Types

# Basic Python types
integer = 42                    # int
floating = 3.14159              # float
text = "Hello Data Science"     # str
boolean = True                  # bool
nothing = None                  # NoneType

# Collections
my_list = [1, 2, 3]             # list
my_tuple = (1, 2, 3)            # tuple
my_dict = {"name": "Alice"}     # dict

2. Pandas & NumPy Data Types (Most Important for Data Science)

import pandas as pd
import numpy as np

df = pd.DataFrame({
    "customer_id": [101, 102, 103],
    "amount": [1250.75, 890.50, 2340.00],
    "region": ["North", "South", "East"],
    "is_high_value": [True, False, True],
    "order_date": pd.date_range("2026-01-01", periods=3)
})

print(df.dtypes)

# Optimized version
df_optimized = df.astype({
    "customer_id": "int32",
    "amount": "float32",
    "region": "category",
    "is_high_value": "boolean"
})

3. Common Data Type Categories in Data Science

**Numeric Types** - `int64` / `int32` / `int16` → whole numbers - `float64` / `float32` → decimal numbers - `Int64` (nullable) → handles missing values safely **Text Types** - `object` → default, slow and memory-heavy - `string` → modern pandas StringDtype (recommended) - `category` → massive memory saver for repeated text **Date & Time** - `datetime64[ns]` → timestamps - `datetime64[ns, tz]` → timezone-aware **Boolean** - `bool` → traditional - `boolean` → nullable boolean (recommended)

4. Best Practices for 2026

  • Always specify `dtypes` when reading CSV files
  • Use `category` for any column with limited unique values
  • Prefer nullable types (`Int64`, `boolean`, `string`) for real-world data
  • Run `df.info(memory_usage="deep")` regularly to check memory usage
  • Downcast numeric types (`float64` → `float32`) when precision allows

Mutable vs Immutable: The Distinction That Matters Most

Python types split into two groups. Immutable types (int, float, str, bool, tuple, frozenset) cannot be changed after creation — operations return new objects. Mutable types (list, dict, set) can be modified in place. This affects how values behave when you pass them to functions and whether they can be dictionary keys: only hashable (typically immutable) values can.

a = [1, 2, 3]
b = a              # b points to the SAME list
b.append(4)
print(a)           # [1, 2, 3, 4]

c = a.copy()       # independent copy
c.append(5)
print(a, c)        # [1, 2, 3, 4] [1, 2, 3, 4, 5]

print(type(42), type(3.14), type("hi"), type(None))
print(isinstance(True, int))   # True: bool is a subclass of int

Worked Example: Choosing Types for One Record

Suppose each order has an id, a price, a list of tags and a fixed (latitude, longitude) location. Picking the right type for each field makes the code self-documenting:

from decimal import Decimal

order = {
    "order_id": 5001,                       # int: whole number identifier
    "price": Decimal("19.99"),              # Decimal: exact money arithmetic
    "tags": ["gift", "express"],            # list: can grow or shrink
    "location": (31.5204, 74.3587),         # tuple: fixed pair, hashable
    "coupon": None,                         # None: explicitly "no value"
}

order["tags"].append("priority")
total = order["price"] * 3
print(total)                 # 59.97 exactly
print(0.1 + 0.2)             # 0.30000000000000004 with float
seen_locations = {order["location"]}   # tuples can go in a set
print(seen_locations)

Common Mistakes

  • Comparing floats with ==: use math.isclose() because of binary rounding
  • Assuming b = a copies a list: it only creates a second name for the same object
  • Using a list as a dict key: raises TypeError: unhashable type; use a tuple
  • Checking for missing values with == None: use is None in plain Python and isna() in pandas

Quick Summary

  • Know the core types and whether each one is mutable or immutable
  • Use Decimal for exact money, tuples for fixed records, lists for changing sequences
  • In pandas, choose dtypes deliberately to save memory and avoid conversion bugs

Conclusion

Data types are not just technical details — they are one of the most powerful optimization tools available to data scientists. In 2026, mastering pandas data types (especially `category`, nullable types, and proper numeric downcasting) can dramatically reduce memory consumption and speed up your entire workflow.

Next steps:

  • Check one of your current DataFrames with df.info(memory_usage="deep") and start optimizing the data types

Related Tutorials

Share:
Last updated: March 2026
Built-in Functions in Python 2026 str() in Python 2026: String Conversion + Modern Formatting & Best Practices memoryview with JAX in Python 2026: Zero-Copy NumPy → JAX Array Interop + Efficient ML Examples memoryview with TensorFlow in Python 2026: Zero-Copy NumPy → Tensor Interop + GPU Pinning & ML Examples memoryview with TensorFlow in Python 2026: Zero-Copy NumPy → Tensor Interop + ML Examples memoryview with NumPy & PyTorch in Python 2026: Zero-Copy Views, Efficient Slicing & ML Interop Examples memoryview with NumPy in Python 2026: Zero-Copy Views, Efficient Slicing & Real ML Examples memoryview() in Python 2026: Zero-Copy Magic for Large Binary Data + Real Examples import() in Python 2026: How It Works, Security Risks & Modern Alternatives (importlib) zip() in Python 2026: How to Use It, strict=True Behavior & Real-World Examples vars() in Python 2026: Accessing Object Namespace + Modern Introspection Patterns type() in Python 2026: Dynamic Type Inspection & Object Creation + Modern Patterns tuple() in Python 2026: Immutable Sequences + Modern Patterns & Best Practices super() in Python 2026: Method Resolution & Modern Inheritance Patterns sum() in Python 2026: Summing Iterables + Modern Numeric Patterns & Best Practices staticmethod() in Python 2026: Static Methods, Utility Functions & Modern Best Practices sorted() in Python 2026: Sorting Iterables + Modern Patterns & Best Practices slice() in Python 2026: Creating Slice Objects + Modern Patterns & Best Practices setattr() in Python 2026: Dynamic Attribute Setting + Modern Patterns & Safety set() in Python 2026: Mutable Sets Creation + Modern Patterns & Best Practices