Main Ecosystems
HomePython CorePandas ReferenceNumPy ScientificFastAPI & PydanticDjango Enterprise
More Ecosystems
Environment & SetupRequests & HTTPAsyncIO ConcurrencyObject-Oriented OOPPyTorch Deep LearningScikit-Learn MLFlask FrameworkWeb ScrapingDatabase & ORMDevOps & Docker

Pandas MemoryError: Unable to Allocate Array — How to Fix It

Verified FixPython 3.10+Pandas 2.0+Silo: pandas

Quick Fix / Solution Rapide

This error occurs when an operation exceeds available RAM. Process large datasets in chunks with pd.read_csv(chunksize=...) and downcast dtypes.

Root Cause Analysis

This error occurs when Python tries to allocate a large contiguous array or DataFrame in system memory, but the requested allocation exceeds the available free virtual memory or RAM limits of the system.

Cause 1: Loading Oversized Datasets into RAM at Once

Calling pd.read_csv('large_file.csv') on multi-gigabyte files attempts to load the full dataset into memory. Pandas data structures typically require 2 to 5 times more memory than the raw file size on disk.

Cause 2: Inefficient Default Data Types (float64 and int64)

Pandas defaults all integer columns to 64-bit integers and float columns to 64-bit floats. For millions of rows with small numbers (e.g. status codes 1-10), 64-bit allocation consumes 8 bytes per cell unnecessarily.

Cause 3: Cartesian Product Merges (Cross-Joins)

Merging two DataFrames on non-unique keys can accidentally trigger a Cartesian explosion (N x M rows), rapidly exhausting all available system RAM.

Reproduction Code (MCVE)

Example: Bug Reproduction
import sys
# Simulate MemoryError raised on excessive array allocation
raise MemoryError('Unable to allocate 128.0 GiB for an array with shape (17179869184,) and data type float64')

Solution 1: Stream and Process Datasets in Chunks with chunksize

Iterate over the dataset in batches using the chunksize parameter in pd.read_csv(), keeping memory consumption constant regardless of total file size.

Example: Recommended Solution
import io
import pandas as pd

csv_data = 'id,value\n' + '\n'.join(f'{i},{i*1.5}' for i in range(50))
chunks = []
for chunk in pd.read_csv(io.StringIO(csv_data), chunksize=10):
    # Downcast float64 to float32 for memory efficiency
    chunk['value'] = chunk['value'].astype('float32')
    chunks.append(chunk)

combined_df = pd.concat(chunks, ignore_index=True)
print(f'Processed {len(combined_df)} rows in memory-efficient chunks.')

Solution 2: Enforce Explicit Compact Dtypes at Ingestion

Specify compact data types (such as 'int32', 'float32', or 'category') directly in pd.read_csv(dtype=...).

Example: Alternative Solution
import io
import pandas as pd

csv_data = 'id,category\n1,A\n2,B\n3,A'
df = pd.read_csv(io.StringIO(csv_data), dtype={'id': 'int32', 'category': 'category'})
print(f'Memory usage:\n{df.memory_usage(deep=True)}')

Common Mistakes & Edge Cases

1. Physical RAM vs OS/Container Limits

In Docker or Kubernetes environments, containers enforce hard memory cgroups. Exceeding container memory triggers silent OOM (Out of Memory) kills by the Linux kernel with exit code 137 rather than a Python MemoryError.

2. Contrasting MemoryError vs OverflowError

  • MemoryError: System RAM or allocation pool is exhausted.
  • OverflowError: A numeric calculation exceeds the maximum representable scalar range for a fixed-size integer or float.

3. Profile Memory with df.info(memory_usage='deep')

Standard df.info() only estimates shallow container memory. Always pass memory_usage='deep' to measure the true memory footprint of string object columns.

4. Note de Reproductibilité

Le comportement de cette anomalie et la validité des commandes de correction dépendent directement de la configuration locale du système d'exploitation, des autorisations utilisateur et de l'environnement d'exécution.