iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

dtypes

A NumPy dtype is the storage format and behaviour of an array’s elements. Pandas dtypes layer on top — some are NumPy-native (int64, float64); others are pandas-only (category, string[python], Int64 with nullables).

Inspect, convert, optimise

EXAMPLE
import numpy as np
import pandas as pd

# NumPy
x = np.array([1, 2, 3], dtype=np.int32)
print(x.dtype)               # int32
print(x.astype(np.float64))  # cast

# Pandas
df = pd.DataFrame({'age': [36, 28, None], 'role': ['admin','user','user']})
print(df.dtypes)
#   age     float64   ← None forces float
#   role    object

# Use pandas nullable Int64 to keep ages as integers
df['age']  = df['age'].astype('Int64')
# Categorical — saves memory + speeds up groupby
df['role'] = df['role'].astype('category')
# Modern string dtype
df['role'] = df['role'].astype('string')

print(df.memory_usage(deep=True).sum())

Why it matters

On a 10M-row DataFrame, swapping objectstring[python] + int64Int8 can cut memory 10x. The bigger your data, the more dtype hygiene pays.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
import numpy as np
int_arr   = np.array([1, 2, 3], dtype=np.int32)
float_arr = np.array([1, 2, 3], dtype=np.float64)
print(int_arr.dtype, float_arr.dtype)
Try it Yourself »

Discussion

Loading…