Repository: apache/arrow
File: python/pyarrow/tests/parquet/test_basic.py (Python)

Code before:
        with open(path, 'wb') as f:
            pass
        pq.read_table(path)

    with pytest.raises(pa.ArrowInvalid, match='size is 4 bytes'):
        with open(path, 'wb') as f:
            f.write(b'ffff')
        pq.read_table(path)


@pytest.mark.pandas
@pytest.mark.fastparquet
@pytest.mark.filterwarnings("ignore:RangeIndex:FutureWarning")
@pytest.mark.filterwarnings("ignore:tostring:DeprecationWarning:fastparquet")
@pytest.mark.filterwarnings("ignore:unclosed file:ResourceWarning")
def test_fastparquet_cross_compatibility(tempdir):
    fp = pytest.importorskip('fastparquet')

    df = pd.DataFrame(
        {
            "a": list("abc"),
            "b": list(range(1, 4)),
            "c": np.arange(4.0, 7.0, dtype="float64"),
            "d": [True, False, True],
            "e": pd.date_range("20130101", periods=3),
            "f": pd.Categorical(["a", "b", "c"]),
            # fastparquet writes list as BYTE_ARRAY JSON, so no roundtrip
            # "g": [[1, 2], None, [1, 2, 3]],
        }
    )
    table = pa.table(df)

    # Arrow -> fastparquet
    file_arrow = str(tempdir / "cross_compat_arrow.parquet")
    pq.write_table(table, file_arrow, compression=None)

    fp_file = fp.ParquetFile(file_arrow)
    df_fp = fp_file.to_pandas()
    # pandas 3 defaults to StringDtype for strings, fastparquet still returns object
    # TODO: remove astype casts once fastparquet supports pandas 3 StringDtype
    tm.assert_frame_equal(df_fp, df.astype({"a": object}))

    # Fastparquet -> arrow
    file_fastparquet = str(tempdir / "cross_compat_fastparquet.parquet")
    # fastparquet doesn't support writing pandas 3 StringDtype yet
    fp.write(file_fastparquet, df.astype({"a": object}))

    table_fp = pq.read_pandas(file_fastparquet)
    # for fastparquet written file, categoricals comes back as strings
    # (no arrow schema in parquet metadata)
    tm.assert_frame_equal(table_fp.to_pandas(), df.astype({"f": object}))

Review comment (suggestion):
```suggestion
            "f": pd.Categorical(["a", "b", "a"]),
```

Code after:
        with open(path, 'wb') as f:
            pass
        pq.read_table(path)

    with pytest.raises(pa.ArrowInvalid, match='size is 4 bytes'):
        with open(path, 'wb') as f:
            f.write(b'ffff')
        pq.read_table(path)


@pytest.mark.pandas
@pytest.mark.fastparquet
@pytest.mark.filterwarnings("ignore:RangeIndex:FutureWarning")
@pytest.mark.filterwarnings("ignore:tostring:DeprecationWarning:fastparquet")
@pytest.mark.filterwarnings("ignore:unclosed file:ResourceWarning")
def test_fastparquet_cross_compatibility(tempdir):
    fp = pytest.importorskip('fastparquet')

    df = pd.DataFrame(
        {
            "a": list("abc"),
            "b": list(range(1, 4)),
            "c": np.arange(4.0, 7.0, dtype="float64"),
            "d": [True, False, True],
            "e": pd.date_range("20130101", periods=3),
            "f": pd.Categorical(["a", "b", "a"]),
            # fastparquet writes list as BYTE_ARRAY JSON, so no roundtrip
            # "g": [[1, 2], None, [1, 2, 3]],
        }
    )
    table = pa.table(df)

    # Arrow -> fastparquet
    file_arrow = str(tempdir / "cross_compat_arrow.parquet")
    pq.write_table(table, file_arrow, compression=None)

    fp_file = fp.ParquetFile(file_arrow)
    df_fp = fp_file.to_pandas()
    # pandas 3 defaults to StringDtype for strings, fastparquet still returns object
    # TODO: remove astype casts once fastparquet supports pandas 3 StringDtype
    tm.assert_frame_equal(df_fp, df.astype({"a": object}))

    # Fastparquet -> arrow
    file_fastparquet = str(tempdir / "cross_compat_fastparquet.parquet")
    # fastparquet doesn't support writing pandas 3 StringDtype yet
    fp.write(file_fastparquet, df.astype({"a": object}))

    table_fp = pq.read_pandas(file_fastparquet)
    # for fastparquet written file, categoricals comes back as strings
    # (no arrow schema in parquet metadata)
    tm.assert_frame_equal(table_fp.to_pandas(), df.astype({"f": object}))