Skip to content

BUG: Categorical columns using the PyArrow backend requires 4x more memory #58062

@adampinky85

Description

@adampinky85

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import os
import tempfile

import numpy as np
import pandas as pd


# create dataframe of categorical data
select = ("apple", "banana", "cherry")
data = np.random.choice(select, size=10**8)
df = pd.DataFrame(data=data, columns=("fruit",), dtype="category")
df.shape  # (100000000, 1)

temp_fd, temp_file = tempfile.mkstemp()

# saves dataframe to parquet file
df.to_parquet(temp_file)

# open dataframe with pyarrow backend
df1 = pd.read_parquet(temp_file, dtype_backend="pyarrow")
df1.fruit.dtype  # dictionary<values=string, indices=int32, ordered=0>[pyarrow]
df1.memory_usage(deep=True).fruit / 1024**2  # 381.4908285140991

# open dataframe with numpy nullable backend
df2 = pd.read_parquet(temp_file, dtype_backend="numpy_nullable")
df2.fruit.dtype  # CategoricalDtype(categories=['apple', 'banana', 'cherry'], ordered=False, categories_dtype=object)
df2.memory_usage(deep=True).fruit / 1024**2  # 95.36769104003906

os.close(temp_fd)

Issue Description

Categorical columns that are loading using the PyArrow dtype backend require 4x the memory consumption of Numpy nullable.

Expected Behavior

The memory consumption should be the same as using categorical fields across both backend types.

Installed Versions

INSTALLED VERSIONS

commit : bdc79c1
python : 3.12.2.final.0
python-bits : 64
OS : Linux
OS-release : 4.14.336-257.566.amzn2.x86_64
Version : #1 SMP Sat Mar 9 09:49:51 UTC 2024
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.2.1
numpy : 1.26.4
pytz : 2023.3
dateutil : 2.9.0.post0
setuptools : 69.2.0
pip : 24.0
Cython : 3.0.9
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : 3.2.0
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.3
IPython : 8.18.0
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.3
bottleneck : 1.3.8
dataframe-api-compat : None
fastparquet : None
fsspec : 2023.6.0
gcsfs : None
matplotlib : 3.8.3
numba : 0.59.1
numexpr : 2.9.0
odfpy : None
openpyxl : 3.1.2
pandas_gbq : None
pyarrow : 15.0.2
pyreadstat : None
python-calamine : None
pyxlsb : 1.0.10
s3fs : None
scipy : 1.12.0
sqlalchemy : None
tables : None
tabulate : None
xarray : 2024.2.0
xlrd : 2.0.1
zstandard : None
tzdata : 2024.1
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugPerformanceMemory or execution speed performancepyarrow dtype retentionop with pyarrow dtype -> expect pyarrow result

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions