Skip to content

Commit

Permalink
BUG: Fix conversion to datetime64[ns] for naive datetimes (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
snowman2 authored May 23, 2023
1 parent 92033cb commit c4bb4fa
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/history.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ History

Latest
-------
- BUG: Fix conversion to datetime64[ns] for naive datetimes (pull #145)

0.4.1
-------
Expand Down
12 changes: 7 additions & 5 deletions geocube/vector_to_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,13 @@ def make_geocube(

# convert to datetime
for datetime_measurement in self._datetime_measurements: # type: ignore
vector_data[datetime_measurement] = (
pandas.to_datetime(vector_data[datetime_measurement])
.dt.tz_convert("UTC")
.dt.tz_localize(None)
.astype("datetime64[ns]")
date_data = pandas.to_datetime(vector_data[datetime_measurement])
try:
date_data = date_data.dt.tz_convert("UTC")
except TypeError:
pass
vector_data[datetime_measurement] = date_data.dt.tz_localize(None).astype(
"datetime64[ns]"
)

# get categorical enumerations if they exist
Expand Down
2 changes: 2 additions & 0 deletions test/integration/api/test_core_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ def test_make_geocube__only_resolution(input_geodata, tmpdir):
@pytest.mark.parametrize(
"input_geodata",
[
TEST_INPUT_DATA_DIR / "naive_time_vector_data.geojson",
gpd.read_file(TEST_INPUT_DATA_DIR / "naive_time_vector_data.geojson"),
TEST_INPUT_DATA_DIR / "time_vector_data.geojson",
gpd.read_file(TEST_INPUT_DATA_DIR / "time_vector_data.geojson"),
],
Expand Down
26 changes: 26 additions & 0 deletions test/test_data/input/naive_time_vector_data.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "test_attr": 1.3, "test_str_attr": "dcf86619", "test_time_attr": "2016-05-21 15:09:21"}, "geometry": { "type": "Point", "coordinates": [ -47.266811, 44.219318 ] } },
{ "type": "Feature", "properties": { "test_attr": 1.3, "test_str_attr": "dcf86619", "test_time_attr": "2016-05-21 15:09:21"}, "geometry": { "type": "Point", "coordinates": [ -47.266798, 44.219318 ] } },
{ "type": "Feature", "properties": { "test_attr": 1.9, "test_str_attr": "dcf86619", "test_time_attr": "2016-05-21 15:09:21"}, "geometry": { "type": "Point", "coordinates": [ -47.266807, 44.219318 ] } },
{ "type": "Feature", "properties": { "test_attr": 1.3, "test_str_attr": "dcf86619", "test_time_attr": "2016-05-21 15:09:21"}, "geometry": { "type": "Point", "coordinates": [ -47.266803, 44.219318 ] } },
{ "type": "Feature", "properties": { "test_attr": 1.3, "test_str_attr": "dcf86619", "test_time_attr": "2016-05-21 15:09:21"}, "geometry": { "type": "Point", "coordinates": [ -47.266794, 44.219318 ] } },
{ "type": "Feature", "properties": { "test_attr": 1.3, "test_str_attr": "dcf86619", "test_time_attr": "2016-05-21 15:09:21"}, "geometry": { "type": "Point", "coordinates": [ -47.266789, 45.219318 ] } },
{ "type": "Feature", "properties": { "test_attr": 5.3, "test_str_attr": "dcf86619", "test_time_attr": "2016-05-21 15:09:21"}, "geometry": { "type": "Point", "coordinates": [ -47.266816, 45.219318 ] } },
{ "type": "Feature", "properties": { "test_attr": 1.3, "test_str_attr": "dcf86619", "test_time_attr": "2016-05-21 15:09:21"}, "geometry": { "type": "Point", "coordinates": [ -47.266851, 45.219318 ] } },
{ "type": "Feature", "properties": { "test_attr": 1.3, "test_str_attr": "dcf86619", "test_time_attr": "2016-05-21 15:09:21"}, "geometry": { "type": "Point", "coordinates": [ -47.266842, 45.219318 ] } },
{ "type": "Feature", "properties": { "test_attr": 1.3, "test_str_attr": "dcf86619", "test_time_attr": "2016-05-21 15:09:21"}, "geometry": { "type": "Point", "coordinates": [ -47.266825, 45.219318 ] } },
{ "type": "Feature", "properties": { "test_attr": 1.3, "test_str_attr": "dcf86619", "test_time_attr": "2016-05-22 15:09:22"}, "geometry": { "type": "Point", "coordinates": [ -47.266834, 44.219318 ] } },
{ "type": "Feature", "properties": { "test_attr": 1.3, "test_str_attr": "dcf86619", "test_time_attr": "2016-05-22 15:09:22"}, "geometry": { "type": "Point", "coordinates": [ -47.26682, 44.219318 ] } },
{ "type": "Feature", "properties": { "test_attr": 2.3, "test_str_attr": "dcf86619", "test_time_attr": "2016-05-22 15:09:22"}, "geometry": { "type": "Point", "coordinates": [ -47.266829, 44.219318 ] } },
{ "type": "Feature", "properties": { "test_attr": 1.3, "test_str_attr": "dcf86619", "test_time_attr": "2016-05-22 15:09:22"}, "geometry": { "type": "Point", "coordinates": [ -47.266838, 44.219318 ] } },
{ "type": "Feature", "properties": { "test_attr": 1.3, "test_str_attr": "dcf86619", "test_time_attr": "2016-05-22 15:09:22"}, "geometry": { "type": "Point", "coordinates": [ -47.266856, 44.219318 ] } },
{ "type": "Feature", "properties": { "test_attr": 1.3, "test_str_attr": "dcf86619", "test_time_attr": "2016-05-22 15:09:22"}, "geometry": { "type": "Point", "coordinates": [ -47.266847, 45.219318 ] } },
{ "type": "Feature", "properties": { "test_attr": 1.3, "test_str_attr": "dcf86619", "test_time_attr": "2016-05-22 15:09:22"}, "geometry": { "type": "Point", "coordinates": [ -47.266891, 45.219318 ] } },
{ "type": "Feature", "properties": { "test_attr": 1.2, "test_str_attr": "dcf86619", "test_time_attr": "2016-05-22 15:09:22"}, "geometry": { "type": "Point", "coordinates": [ -47.266865, 45.219318 ] } },
{ "type": "Feature", "properties": { "test_attr": 1.3, "test_str_attr": "dcf86619", "test_time_attr": "2016-05-22 15:09:22"}, "geometry": { "type": "Point", "coordinates": [ -47.266873, 45.219318 ] } },
{ "type": "Feature", "properties": { "test_attr": 1.7, "test_str_attr": "dcf86619", "test_time_attr": "2016-05-22 15:09:22"}, "geometry": { "type": "Point", "coordinates": [ -47.266869, 45.219318 ] } }
]
}

0 comments on commit c4bb4fa

Please sign in to comment.