Skip to content
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

ARROW-8251, ARROW-7782: [Python] Preserve pandas index and extension dtypes in write_to_dataset roundtrip #7054

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions python/pyarrow/parquet.py
Expand Up @@ -1683,7 +1683,7 @@ def write_to_dataset(table, root_path, partition_cols=None,
metadata_collector = kwargs.pop('metadata_collector', None)

if partition_cols is not None and len(partition_cols) > 0:
df = table.to_pandas(ignore_metadata=True)
df = table.to_pandas()
partition_keys = [df[col] for col in partition_cols]
data_df = df.drop(partition_cols, axis='columns')
data_cols = df.columns.drop(partition_cols)
Expand All @@ -1704,8 +1704,8 @@ def write_to_dataset(table, root_path, partition_cols=None,
subdir = '/'.join(
['{colname}={value}'.format(colname=name, value=val)
for name, val in zip(partition_cols, keys)])
subtable = pa.Table.from_pandas(subgroup, preserve_index=False,
schema=subschema, safe=False)
subtable = pa.Table.from_pandas(subgroup, schema=subschema,
safe=False)
_mkdir_if_not_exists(fs, '/'.join([root_path, subdir]))
if partition_filename_cb:
outfile = partition_filename_cb(keys)
Expand Down
64 changes: 64 additions & 0 deletions python/pyarrow/tests/test_parquet.py
Expand Up @@ -18,6 +18,7 @@
from collections import OrderedDict
import datetime
import decimal
from distutils.version import LooseVersion
import io
import json
import os
Expand Down Expand Up @@ -2827,6 +2828,69 @@ def partition_filename_callback(keys):
assert sorted(expected_basenames) == sorted(output_basenames)


@pytest.mark.pandas
@parametrize_legacy_dataset
def test_write_to_dataset_pandas_preserve_extensiondtypes(
tempdir, use_legacy_dataset
):
# ARROW-8251 - preserve pandas extension dtypes in roundtrip
if LooseVersion(pd.__version__) < "1.0.0":
pytest.skip("__arrow_array__ added to pandas in 1.0.0")

df = pd.DataFrame({'part': 'a', "col": [1, 2, 3]})
df['col'] = df['col'].astype("Int64")
table = pa.table(df)

pq.write_to_dataset(table, str(tempdir / "case1"), partition_cols=['part'])
result = pq.read_table(
str(tempdir / "case1"), use_legacy_dataset=use_legacy_dataset
).to_pandas()
tm.assert_frame_equal(result[["col"]], df[["col"]])

pq.write_to_dataset(table, str(tempdir / "case2"))
result = pq.read_table(
str(tempdir / "case2"), use_legacy_dataset=use_legacy_dataset
).to_pandas()
tm.assert_frame_equal(result[["col"]], df[["col"]])

pq.write_table(table, str(tempdir / "data.parquet"))
result = pq.read_table(
str(tempdir / "data.parquet"), use_legacy_dataset=use_legacy_dataset
).to_pandas()
tm.assert_frame_equal(result[["col"]], df[["col"]])


@pytest.mark.pandas
@parametrize_legacy_dataset
def test_write_to_dataset_pandas_preserve_index(tempdir, use_legacy_dataset):
# ARROW-8251 - preserve pandas index in roundtrip

df = pd.DataFrame({'part': ['a', 'a', 'b'], "col": [1, 2, 3]})
df.index = pd.Index(['a', 'b', 'c'], name="idx")
table = pa.table(df)
df_cat = df[["col", "part"]].copy()
if use_legacy_dataset:
df_cat["part"] = df_cat["part"].astype("category")

pq.write_to_dataset(table, str(tempdir / "case1"), partition_cols=['part'])
result = pq.read_table(
str(tempdir / "case1"), use_legacy_dataset=use_legacy_dataset
).to_pandas()
tm.assert_frame_equal(result, df_cat)

pq.write_to_dataset(table, str(tempdir / "case2"))
result = pq.read_table(
str(tempdir / "case2"), use_legacy_dataset=use_legacy_dataset
).to_pandas()
tm.assert_frame_equal(result, df)

pq.write_table(table, str(tempdir / "data.parquet"))
result = pq.read_table(
str(tempdir / "data.parquet"), use_legacy_dataset=use_legacy_dataset
).to_pandas()
tm.assert_frame_equal(result, df)


@pytest.mark.large_memory
def test_large_table_int32_overflow():
size = np.iinfo('int32').max + 1
Expand Down