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

Enable timezone type for timestamp pyarrow type #888

Merged
merged 2 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 9 additions & 0 deletions src/fondant/core/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,13 @@ def from_dict(cls, json_schema: dict):
fields = [(name, cls.from_dict(prop)) for name, prop in properties.items()]
return cls.struct(fields)

if type_name == "timestamp":
return cls(type_name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to support this as a type, only as a format, right?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, leftover from when I was trying something different. Removed


if isinstance(type_name, str):
type_format = json_schema.get("format", None)
if type_format == "date-time":
return cls(pa.timestamp("us", tz="UTC"))
return cls(type_name)

msg = f"Invalid 'type' value: {type_name}"
Expand All @@ -218,6 +224,9 @@ def to_dict(self) -> dict:
fields = [(field.name, Type(field.type).to_dict()) for field in self.value]
return {"type": "object", "properties": dict(fields)}

elif isinstance(self.value, pa.TimestampType):
return {"type": "string", "format": "date-time"}

type_ = None
for type_name, data_type in _TYPES.items():
if self.value.equals(data_type):
Expand Down
4 changes: 4 additions & 0 deletions src/fondant/core/schemas/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
"type": "string",
"$ref": "#/definitions/subset_data_type"
},
"format": {
"type": "string",
"description": "additional format information for the field"
},
"properties": {
"type": "object",
"properties": {
Expand Down
3 changes: 3 additions & 0 deletions tests/component/examples/component_specs/component.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ produces:
number:
type: int32

date:
type: string
format: date-time

args:
flag:
Expand Down
5 changes: 5 additions & 0 deletions tests/component/test_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ def mocked_load_dataframe(self):
return dd.from_dict(
{
"images_data": [1, 2, 3],
"date": [
"2024-02-29T12:30:45",
"2024-02-29T12:30:45",
"2024-02-29T12:30:45",
],
"element": [
("1", 1),
("2", 2),
Expand Down
7 changes: 7 additions & 0 deletions tests/core/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ def test_valid_type():
assert Type.list(Type("int8")).value == pa.list_(pa.int8())
assert Type.list(Type.list(Type("string"))).value == pa.list_(pa.list_(pa.string()))
assert Type("int8").to_dict() == {"type": "int8"}
assert Type(pa.timestamp("us", tz="UTC")).to_dict() == {
"type": "string",
"format": "date-time",
}
assert Type.list("float32").to_dict() == {
"type": "array",
"items": {"type": "float32"},
Expand Down Expand Up @@ -42,6 +46,9 @@ def test_valid_json_schema():
assert Type.from_dict(
{"type": "array", "items": {"type": "array", "items": {"type": "int8"}}},
).value == pa.list_(pa.list_(pa.int8()))
assert Type.from_dict(
{"type": "string", "format": "date-time"},
).value == pa.timestamp("us", tz="UTC")
assert Type.from_dict(
{
"type": "object",
Expand Down
Loading