From 9c602199e1d990c02a467bb35f3a89f89b4b5800 Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Fri, 29 Mar 2024 12:42:11 -0500 Subject: [PATCH] parameter for datetime precision --- stac_geoparquet/stac_geoparquet.py | 4 ++-- tests/test_stac_geoparquet.py | 17 ++++++++++++++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/stac_geoparquet/stac_geoparquet.py b/stac_geoparquet/stac_geoparquet.py index 2efc299..74f1872 100644 --- a/stac_geoparquet/stac_geoparquet.py +++ b/stac_geoparquet/stac_geoparquet.py @@ -38,7 +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", + datetime_precision: str = "ns", ) -> geopandas.GeoDataFrame: """ Convert a sequence of STAC items to a :class:`geopandas.GeoDataFrame`. @@ -87,7 +87,7 @@ 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" + datetime_precision: str, default "ns" The precision to use for the datetime columns. For example, "us" is microsecond and "ns" is nanosecond. diff --git a/tests/test_stac_geoparquet.py b/tests/test_stac_geoparquet.py index b9e18b7..db6b978 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"]).as_unit("us"), + "datetime": pd.to_datetime(["2019-08-28 00:00:00+0000"]).as_unit("ns"), "naip:year": ["2019"], "proj:bbox": [[592596.0, 4663966.8, 598495.8, 4671633.0]], "proj:epsg": [26915], @@ -404,3 +404,18 @@ def test_mixed_date_format(): ] assert result["datetime"].tolist() == expected + + +@pytest.mark.parametrize("datetime_precision", ["us", "ns"]) +def test_datetime_precision(datetime_precision): + item = json.loads((HERE / "sentinel-2-item.json").read_text()) + item["properties"]["datetime"] = "2000-12-10T22:00:00.123456Z" + df = stac_geoparquet.to_geodataframe( + [item], dtype_backend="pyarrow", datetime_precision=datetime_precision + ) + result = df["datetime"].iloc[0] + expected = pd.Timestamp("2000-12-10 22:00:00.123456+0000", tz="UTC").as_unit( + datetime_precision + ) + assert result == expected + assert result.unit == datetime_precision