Skip to content

Commit

Permalink
parameter for datetime precision
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Augspurger committed Mar 29, 2024
1 parent 5c646cb commit 9c60219
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
4 changes: 2 additions & 2 deletions stac_geoparquet/stac_geoparquet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -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.
Expand Down
17 changes: 16 additions & 1 deletion tests/test_stac_geoparquet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -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

0 comments on commit 9c60219

Please sign in to comment.