-
-
Notifications
You must be signed in to change notification settings - Fork 19.4k
Description
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 pandas as pd
# Create DataFrame and convert to pyarrow-backed dtypes
df = pd.DataFrame({
'year': [2020, 2021, 2022],
'make': ['Ford', 'Toyota', 'Honda'],
'city08': [20, 30, 25],
'highway08': [30, 40, 35],
}).convert_dtypes(dtype_backend="pyarrow")
print("dtypes:", df.dtypes)
# Row-wise apply (observed problem)
res_apply = df.apply(lambda row: (row['city08'] + row['highway08']) / 2, axis='columns')
print("apply result dtype:", res_apply.dtype, "dtype kind:", type(res_apply.dtype))
# Vectorized / expression using pd.col (expected)
combined = df.assign(combined = pd.col('city08') + pd.col('highway08') / 2).combined
print("assign+pd.col dtype:", combined.dtype, "dtype kind:", type(combined.dtype))Output:
dtypes: year int64[pyarrow]
make string[pyarrow]
city08 int64[pyarrow]
highway08 int64[pyarrow]
dtype: object
apply result dtype: float64 dtype kind: <class 'numpy.dtypes.Float64DType'>
assign+pd.col dtype: double[pyarrow] dtype kind: <class 'pandas.ArrowDtype'>
Issue Description
When a DataFrame is converted with convert_dtypes(dtype_backend='pyarrow'), a row-wise DataFrame.apply(..., axis='columns') that computes a numeric result returns a Series built from Python/numpy scalars and does not preserve a pyarrow-backed extension dtype (e.g., float[pyarrow] or other arrow dtypes). The vectorized expression (e.g., using pd.col or column arithmetic) produces a pyarrow-backed result as expected.
Expected Behavior
Preserve dtype when using apply
Installed Versions
INSTALLED VERSIONS
commit : 1a3230d
python : 3.11.13
python-bits : 64
OS : Darwin
OS-release : 24.6.0
Version : Darwin Kernel Version 24.6.0: Mon Jul 14 11:30:40 PDT 2025; root:xnu-11417.140.69~1/RELEASE_ARM64_T6041
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : None
LOCALE : en_US.UTF-8
pandas : 3.0.0rc0
numpy : 2.4.0rc1
dateutil : 2.9.0.post0
pip : 25.3
Cython : 3.2.2
sphinx : None
IPython : 9.8.0
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.14.3
bottleneck : None
fastparquet : None
fsspec : None
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : 3.1.6
lxml.etree : 6.0.2
matplotlib : 3.10.7
numba : None
numexpr : None
odfpy : None
openpyxl : None
psycopg2 : None
pymysql : None
pyarrow : 22.0.0
pyiceberg : None
pyreadstat : None
pytest : None
python-calamine : None
pytz : 2025.2
pyxlsb : None
s3fs : None
scipy : 1.16.3
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlsxwriter : None
zstandard : None
qtpy : None
pyqt5 : None