-
Notifications
You must be signed in to change notification settings - Fork 415
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Description As described in #686 some pandas datatypes are not converted to a format that is compatible with delta lake. This handles the instance of timestamps, which are stored with `ns` resolution in Pandas. Here, if is a schema is not provided, we specify converting the timestamps to `us` resolution. We also update `python/tests/test_writer.py::test_write_pandas` to reflect this change. # Related Issue(s) #685 Co-authored-by: Will Jones <willjones127@gmail.com>
- Loading branch information
Showing
4 changed files
with
53 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,42 @@ | ||
from typing import Union | ||
from typing import TYPE_CHECKING, Tuple, Union | ||
|
||
import pyarrow as pa | ||
|
||
if TYPE_CHECKING: | ||
import pandas as pd | ||
|
||
from ._internal import ArrayType, Field, MapType, PrimitiveType, Schema, StructType | ||
|
||
# Can't implement inheritance (see note in src/schema.rs), so this is next | ||
# best thing. | ||
DataType = Union["PrimitiveType", "MapType", "StructType", "ArrayType"] | ||
|
||
|
||
def delta_arrow_schema_from_pandas( | ||
data: "pd.DataFrame", | ||
) -> Tuple[pa.Table, pa.Schema]: | ||
""" | ||
Infers the schema for the delta table from the Pandas DataFrame. | ||
Necessary because of issues such as: https://github.com/delta-io/delta-rs/issues/686 | ||
:param data: Data to write. | ||
:return: A PyArrow Table and the inferred schema for the Delta Table | ||
""" | ||
|
||
table = pa.Table.from_pandas(data) | ||
schema = table.schema | ||
schema_out = [] | ||
for field in schema: | ||
if isinstance(field.type, pa.TimestampType): | ||
f = pa.field( | ||
name=field.name, | ||
type=pa.timestamp("us"), | ||
nullable=field.nullable, | ||
metadata=field.metadata, | ||
) | ||
schema_out.append(f) | ||
else: | ||
schema_out.append(field) | ||
schema = pa.schema(schema_out, metadata=schema.metadata) | ||
data = pa.Table.from_pandas(data, schema=schema) | ||
return data, schema |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters