From 5c646cba236a1106b8c890bd826bc71504500958 Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Sun, 24 Mar 2024 15:19:11 -0500 Subject: [PATCH] ts resolution --- stac_geoparquet/stac_geoparquet.py | 14 ++++++++++---- tests/test_stac_geoparquet.py | 2 +- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/stac_geoparquet/stac_geoparquet.py b/stac_geoparquet/stac_geoparquet.py index 0974547..2efc299 100644 --- a/stac_geoparquet/stac_geoparquet.py +++ b/stac_geoparquet/stac_geoparquet.py @@ -38,6 +38,7 @@ def to_geodataframe( items: Sequence[dict[str, Any]], add_self_link: bool = False, dtype_backend: DTYPE_BACKEND | None = None, + datetime_precision: str = "us", ) -> geopandas.GeoDataFrame: """ Convert a sequence of STAC items to a :class:`geopandas.GeoDataFrame`. @@ -86,6 +87,10 @@ def to_geodataframe( with fields ``{"href": "a.tif", "title", None}``. pyarrow will infer that the struct field ``asset.title`` is nullable. + datetime_precision: str, default "us" + The precision to use for the datetime columns. For example, + "us" is microsecond and "ns" is nanosecond. + Returns ------- The converted GeoDataFrame. @@ -150,9 +155,8 @@ def to_geodataframe( if dtype_backend == "pyarrow": for k, v in items2.items(): if k in DATETIME_COLUMNS: - items2[k] = pd.arrays.ArrowExtensionArray( - pa.array(pd.to_datetime(v, format="ISO8601")) - ) + dt = pd.to_datetime(v, format="ISO8601").as_unit(datetime_precision) + items2[k] = pd.arrays.ArrowExtensionArray(pa.array(dt)) elif k != "geometry": items2[k] = pd.arrays.ArrowExtensionArray(pa.array(v)) @@ -160,7 +164,9 @@ def to_geodataframe( elif dtype_backend == "numpy_nullable": for k, v in items2.items(): if k in DATETIME_COLUMNS: - items2[k] = pd.to_datetime(v, format="ISO8601") + items2[k] = pd.to_datetime(v, format="ISO8601").as_unit( + datetime_precision + ) if k in {"type", "stac_version", "id", "collection", SELF_LINK_COLUMN}: items2[k] = pd.array(v, dtype="string") diff --git a/tests/test_stac_geoparquet.py b/tests/test_stac_geoparquet.py index 3ddca61..b9e18b7 100644 --- a/tests/test_stac_geoparquet.py +++ b/tests/test_stac_geoparquet.py @@ -227,7 +227,7 @@ def test_assert_equal(): ], "collection": ["naip"], "gsd": [0.6], - "datetime": pd.to_datetime(["2019-08-28 00:00:00+0000"]), + "datetime": pd.to_datetime(["2019-08-28 00:00:00+0000"]).as_unit("us"), "naip:year": ["2019"], "proj:bbox": [[592596.0, 4663966.8, 598495.8, 4671633.0]], "proj:epsg": [26915],