Sub topic 5
Why object columns of strings use so much memory
This chapter assumes you can read memory_usage(deep=True) output and know that pandas columns have dtypes such as int64 and object. Here you will see why a string column costs far more than a numeric column with the same number of rows.
What an object column stores
A numeric column such as int64 is one contiguous block of memory. Each value takes a fixed 8 bytes. An object column is different. pandas stores an array of 8-byte pointers, and each pointer refers to a separate Python object that lives elsewhere in memory.
For strings, each of those objects is a Python str. Every str carries a header with a reference count, a type pointer, the length, a hash, and flags. On 64-bit CPython, an ASCII string takes about 49 bytes of overhead plus 1 byte per character.
A two-letter country code therefore costs about 8 bytes for the pointer and 51 bytes for the string object. That is roughly 59 bytes for information that fits in 1 byte.
Seeing the gap
The default memory_usage() counts only the pointer array. It never follows the pointers. Passing deep=True asks Python for the size of every object.
import sys
import pandas as pd
print(sys.getsizeof("US"))
s = pd.Series(["US", "DE", "FR", "JP", "BR"] * 20_000, dtype=object)
shallow = s.memory_usage(deep=False, index=False)
deep = s.memory_usage(deep=True, index=False)
print(shallow)
print(deep)
print(deep / shallow)On 64-bit CPython this prints 51, 800000, 5900000, and 7.375. The shallow number is 100,000 rows times 8 bytes. The deep number adds 100,000 times 51 bytes for the string objects. An int64 column of the same length would take 800,000 bytes, so the string column is more than seven times larger.
The dtype=object argument is deliberate. Newer pandas versions can infer a dedicated string dtype for a list of strings, which stores data differently. This chapter describes the classic object layout.
Why repeated values still cost memory
You might expect pandas to store "US" once and point to it from every row. Sometimes that happens, but it is not guaranteed. Two situations are common:
- Data built from Python lists or from NumPy arrays converted to
objectoften ends up with a separate string object for each row. read_csvcan reuse one string object for identical values in a column. The column then uses less real memory than a naive count suggests.
This matters for measurement. memory_usage(deep=True) adds up the size of every element it finds and does not notice that two pointers refer to the same object. When strings are shared, the deep figure overstates actual usage. It is still the right number for comparison, because it shows what the data costs if the strings are not shared. It is also the number you should expect after operations such as apply or str.upper(), which create new string objects.
The same structure slows computation. Operations on an object column follow a pointer for every element and call Python-level code. Pointer chasing also defeats the CPU cache, which works best on contiguous data.
What this means for the next steps
Two costs come from the layout: 8 bytes per row for the pointer, and the full string object for each distinct copy. The category dtype, covered in the next chapter, removes both. It stores each distinct string once and keeps a small integer code per row.
Before converting, check whether the column is worth it. A column with only a few distinct values compared to its row count benefits the most. A column where almost every value is unique gains little, because the unique strings still have to be stored.
Exercise
A DataFrame has one object column of 1,000,000 ASCII strings. Each string is 10 characters long and each is a separate object. Estimate the result of memoryusage(deep=False) and memoryusage(deep=True) for that column, ignoring the index.
Show answer
The shallow figure counts only the pointers: 1,000,000 x 8 = 8,000,000 bytes. Each string object takes about 49 + 10 = 59 bytes, so the objects add about 59,000,000 bytes. The deep figure is about 67,000,000 bytes, roughly 8 times the shallow one.
Exercise
Why can memoryusage(deep=True) overstate the real memory use of a column read with readcsv?
Show answer
It adds up the size of every element separately. If read_csv reuses one string object for identical values, many pointers share the same object, but the deep count still charges for each pointer's target. The result is higher than the memory actually in use.