Skip to content

Commit

Permalink
👌 Handle pandas.Series in deltatime_to_utc_time conversion
Browse files Browse the repository at this point in the history
Patches #40. The deltatime_to_utctime converter didn't handle pandas.Series properly, as the start_epoch variable would have an index of 0, and the datetime + timedelta operation would only get applied at index 0 instead of along the whole column. Calling squeeze() converts the pandas.Series to a pandas.Timestamp, so that the addition operation is broadcast to the whole column. This also works on an xarray.DataArray and numpy.array. Doesn't work for a dask.Series, but we can work that out when the need arises.
  • Loading branch information
weiji14 committed Jun 3, 2020
1 parent 8be8687 commit 3e32e22
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
4 changes: 3 additions & 1 deletion deepicedrain/spatiotemporal.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ def deltatime_to_utctime(
Note, does not account for leap seconds! There are none declared since the
last one announced on 31/12/2016, so it should be fine for now as of 2020.
"""
utc_time: xr.DataArray = dataarray.__class__(start_epoch) + dataarray
start_epoch = dataarray.__class__(start_epoch).squeeze()

utc_time: xr.DataArray = start_epoch + dataarray

return utc_time

Expand Down
35 changes: 32 additions & 3 deletions deepicedrain/tests/test_spatiotemporal_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,39 @@
from deepicedrain import catalog, deltatime_to_utctime, lonlat_to_xy


def test_deltatime_to_utctime():
def test_deltatime_to_utctime_pandas_series():
"""
Test that converting from ICESat-2 delta_time to utc_time works,
and that the xarray dimensions are preserved in the process.
Test that converting from ICESat-2 delta_time to utc_time works on a
dask.dataframe.core.Series.
"""
atl11_dataset: xr.Dataset = catalog.test_data.atl11_test_case.to_dask()
atl11_dataframe: pd.DataFrame = atl11_dataset.to_dataframe()

utc_time: pd.Series = deltatime_to_utctime(dataarray=atl11_dataframe.delta_time)

assert utc_time.shape == (2808,)

npt.assert_equal(
actual=utc_time.min(), desired=pd.Timestamp("2019-05-19T20:53:51.039891534"),
)

npt.assert_equal(
actual=utc_time.loc[3].mean(),
desired=pd.Timestamp("2019-05-19 20:54:00.925868800"),
)
npt.assert_equal(
actual=utc_time.loc[4].mean(),
desired=pd.Timestamp("2019-08-18 16:33:47.791226368"),
)
npt.assert_equal(
actual=utc_time.max(), desired=pd.Timestamp("2019-08-18T16:33:57.834610209"),
)


def test_deltatime_to_utctime_xarray_dataarray():
"""
Test that converting from ICESat-2 delta_time to utc_time works on an
xarray.DataArray, and that the dimensions are preserved in the process.
"""
atl11_dataset: xr.Dataset = catalog.test_data.atl11_test_case.to_dask()

Expand Down

0 comments on commit 3e32e22

Please sign in to comment.