Skip to content

Update read_parquet_file_to_pandas to use nested pandas I/O #499

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ dependencies = [
"numba>=0.58",
"numpy<3",
"pandas",
"nested-pandas>=0.3.8,<0.5.0",
"pyarrow>=14.0.1",
"pydantic",
"scipy",
Expand Down
11 changes: 5 additions & 6 deletions src/hats/io/file_io/file_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from collections.abc import Generator
from pathlib import Path

import nested_pandas as npd
import numpy as np
import pandas as pd
import pyarrow.dataset as pds
Expand Down Expand Up @@ -283,7 +284,7 @@
return storage_options


def read_parquet_file_to_pandas(file_pointer: str | Path | UPath, **kwargs) -> pd.DataFrame:
def read_parquet_file_to_pandas(file_pointer: str | Path | UPath, **kwargs) -> npd.NestedFrame:
"""Reads parquet file(s) to a pandas DataFrame

Args:
Expand All @@ -296,17 +297,15 @@
file_pointer = get_upath(file_pointer)
# If we are trying to read a directory over http, we need to send the explicit list of files instead.
# We don't want to get the list unnecessarily because it can be expensive.
if isinstance(file_pointer, upath.implementations.http.HTTPPath) and len(file_pointer.suffixes) == 0:
if isinstance(file_pointer, upath.implementations.http.HTTPPath) and file_pointer.is_dir():
file_pointers = [f for f in file_pointer.iterdir() if f.is_file()]
storage_options = unnest_headers_for_pandas(file_pointer.storage_options)
return pd.read_parquet(
return npd.read_parquet(

Check warning on line 302 in src/hats/io/file_io/file_io.py

View check run for this annotation

Codecov / codecov/patch

src/hats/io/file_io/file_io.py#L302

Added line #L302 was not covered by tests
file_pointers,
storage_options=storage_options,
filesystem=file_pointer.fs,
partitioning=None, # Avoid the ArrowTypeError described in #367
**kwargs,
)
return pd.read_parquet(
return npd.read_parquet(
file_pointer.path,
filesystem=file_pointer.fs,
partitioning=None, # Avoid the ArrowTypeError described in #367
Expand Down
1 change: 1 addition & 0 deletions tests/hats/io/file_io/test_file_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ def test_write_df_to_csv(tmp_path):

def test_read_parquet_data(tmp_path):
random_df = pd.DataFrame(np.random.randint(0, 100, size=(100, 4)), columns=list("ABCD"))
random_df = random_df.convert_dtypes(dtype_backend="pyarrow")
test_file_path = tmp_path / "test.parquet"
random_df.to_parquet(test_file_path)
dataframe = read_parquet_file_to_pandas(test_file_path)
Expand Down