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
import pyarrow as pa
import pyarrow.parquet as pq
pd.DataFrame(index=['A', 'B'], columns=['C', 'D']).to_parquet('temp.parquet')
# All correctly print 2
print(len(pd.read_parquet('temp.parquet', columns=['C']).index))
print(len(pd.read_parquet('temp.parquet', columns=[]).index))
print(pq.read_table('temp.parquet', columns=[]).num_rows)
print(len(pq.read_table('temp.parquet', columns=[]).to_pandas().index))
pd.DataFrame(index=pd.RangeIndex(2), columns=['C', 'D']).to_parquet('temp.parquet')
# Correctly prints 2
print(len(pd.read_parquet('temp.parquet', columns=['C']).index))
# BUG: prints 0!
print(len(pd.read_parquet('temp.parquet', columns=[]).index))
# Correctly prints 2
print(pq.read_table('temp.parquet', columns=[]).num_rows)
print(len(pq.read_table('temp.parquet', columns=[]).to_pandas().index))
pd.DataFrame(index=pd.RangeIndex(2), columns=[]).to_parquet('temp.parquet')
# BUG: all incorrectly print 0
print(len(pd.read_parquet('temp.parquet', columns=[]).index))
print(len(pd.read_parquet('temp.parquet', columns=None).index))
print(pq.read_table('temp.parquet', columns=[]).num_rows)
print(len(pq.read_table('temp.parquet', columns=[]).to_pandas().index))
pq.write_table(pa.Table.from_pandas(pd.DataFrame(index=pd.RangeIndex(2), columns=[])), 'temp.parquet')
# BUG: all incorrectly print 0
print(len(pd.read_parquet('temp.parquet', columns=[]).index))
print(len(pd.read_parquet('temp.parquet', columns=None).index))
print(pq.read_table('temp.parquet', columns=[]).num_rows)
print(len(pq.read_table('temp.parquet', columns=[]).to_pandas().index))
Issue Description
If you do pd.read_parquet(columns=[])
the resulting DataFrame should have no columns, but should still have the expected index. However, in the special case where the expected index is a trivial RangeIndex we are instead getting an empty index with entirely the wrong length.
Expected Behavior
If column 'c' exists in a file, it should be the case that pd.read_parquet(path, columns=[c]).index.equals(pd.read_parquet(path, columns=[]).index)
Installed Versions
pandas : 2.2.2
numpy : 1.24.3
pytz : 2023.3
dateutil : 2.8.2
setuptools : 67.7.2
pip : 23.1.2
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 8.13.2
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
bottleneck : 1.3.7
dataframe-api-compat : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : 2.9.0
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 14.0.2
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None