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

Bugfix: support writing NAT in datetime column #146

Merged
merged 9 commits into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Bug fixes

- register GDAL drivers during initial import of pyogrio (#145)
- support writing "not a time" (NaT) values in a datetime column (#146)

## 0.4.1

Expand Down
56 changes: 31 additions & 25 deletions pyogrio/_io.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1407,33 +1407,39 @@ def ogr_write(str path, str layer, str driver, geometry, field_data, fields,
OGR_F_SetFieldDouble(ogr_feature, field_idx, field_value)

elif field_type == OFTDate:
datetime = field_value.item()
OGR_F_SetFieldDateTimeEx(
ogr_feature,
field_idx,
datetime.year,
datetime.month,
datetime.day,
0,
0,
0.0,
0
)
if np.isnat(field_value):
OGR_F_SetFieldNull(ogr_feature, field_idx)
else:
datetime = field_value.item()
OGR_F_SetFieldDateTimeEx(
ogr_feature,
field_idx,
datetime.year,
datetime.month,
datetime.day,
0,
0,
0.0,
0
)

elif field_type == OFTDateTime:
# TODO: add support for timezones
datetime = field_value.astype("datetime64[ms]").item()
OGR_F_SetFieldDateTimeEx(
ogr_feature,
field_idx,
datetime.year,
datetime.month,
datetime.day,
datetime.hour,
datetime.minute,
datetime.second + datetime.microsecond / 10**6,
0
)
if np.isnat(field_value):
OGR_F_SetFieldNull(ogr_feature, field_idx)
else:
# TODO: add support for timezones
datetime = field_value.astype("datetime64[ms]").item()
OGR_F_SetFieldDateTimeEx(
ogr_feature,
field_idx,
datetime.year,
datetime.month,
datetime.day,
datetime.hour,
datetime.minute,
datetime.second + datetime.microsecond / 10**6,
0
)

else:
raise NotImplementedError(f"OGR field type is not supported for writing: {field_type}")
Expand Down
5 changes: 4 additions & 1 deletion pyogrio/tests/test_raw_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,13 +437,16 @@ def test_read_write_datetime(tmp_path):
["2001-01-01T12:00", "2002-02-03T13:56:03.072123456"],
dtype="datetime64[ns]",
),
# Remark: a None value is automatically converted to np.datetime64("NaT")
np.array([np.datetime64("NaT"), None], dtype="datetime64[ms]"),
]
fields = [
"datetime64_d",
"datetime64_s",
"datetime64_ms",
"datetime64_ns",
"datetime64_precise_ns",
"datetime64_ms_nat",
]

# Point(0, 0)
Expand All @@ -460,7 +463,7 @@ def test_read_write_datetime(tmp_path):
# gdal rounds datetimes to ms
assert np.array_equal(result[idx], field_data[idx].astype("datetime64[ms]"))
else:
assert np.array_equal(result[idx], field_data[idx])
assert np.array_equal(result[idx], field_data[idx], equal_nan=True)


def test_read_data_types_numeric_with_null(test_gpkg_nulls):
Expand Down