Skip to content

TST: Test non-nanosecond datetimes in PyArrow Parquet dataframes #59393

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 10 commits into from
Aug 19, 2024
27 changes: 27 additions & 0 deletions pandas/tests/io/test_parquet.py
Original file line number Diff line number Diff line change
Expand Up @@ -1131,6 +1131,33 @@ def test_infer_string_large_string_type(self, tmp_path, pa):
# assert result["strings"].dtype == "string"
# FIXME: don't leave commented-out

def test_non_nanosecond_timestamps(self, tmp_path, pa):
# GH#49236
#
# pandas 1.x didn't support non-nanosecond datetimes.
# pyarrow.Table.to_pandas supports timestamp_as_object param to solve that issue
# https://arrow.apache.org/docs/python/generated/pyarrow.Table.html#pyarrow.Table.to_pandas
#
# This test tests that the current version of pandas
# supports non-nanosecond (microsecond in this case) datetimes,
# the code example from GH#49236 doesn't fail anymore,
# and timestamp_as_object param is not needed.
import pyarrow as pa
import pyarrow.parquet as pq

path = tmp_path / "non_nanosecond_timestamp.p"

arr = pa.array([datetime.datetime(1600, 1, 1)], type=pa.timestamp("us"))
table = pa.table([arr], names=["timestamp"])
pq.write_table(table, path)

result = read_parquet(path)
expected = pd.DataFrame(
data={"timestamp": [datetime.datetime(1600, 1, 1)]},
dtype="datetime64[us]",
)
tm.assert_frame_equal(result, expected)


class TestParquetFastParquet(Base):
@pytest.mark.xfail(reason="datetime_with_nat gets incorrect values")
Expand Down
Loading