From 4c290e7435cf6a5832fd5f480548699612a7c5ca Mon Sep 17 00:00:00 2001 From: Tom Vo Date: Thu, 6 Jun 2024 15:38:02 -0700 Subject: [PATCH] Update `_create_time_bounds()` - Add comment about needing to convert dype=timedelta64[ns] to pandas timedelta object - Ignore C901 flake8 error --- xcdat/bounds.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/xcdat/bounds.py b/xcdat/bounds.py index 50b727e1..38e7f6ff 100644 --- a/xcdat/bounds.py +++ b/xcdat/bounds.py @@ -504,7 +504,7 @@ def _get_bounds_keys(self, axis: CFAxisKey) -> List[str]: return list(set(keys)) - def _create_time_bounds( + def _create_time_bounds( # noqa: C901 self, time: xr.DataArray, freq: Optional[Literal["year", "month", "day", "hour"]] = None, @@ -601,14 +601,16 @@ def _create_time_bounds( elif freq == "day": time_bnds = self._create_daily_time_bounds(timesteps, obj_type) elif freq == "hour": - # Determine the daily frequency for generating time bounds. + # Determine the daily frequency for generating time bounds. if daily_subfreq is None: diff = time.values[1] - time.values[0] + + # Arrays with `dtype="timedelta64[ns]"` must be converted to + # pandas timedelta objects in order to access the `.seconds` + # time component. if isinstance(diff, np.timedelta64): diff = pd.to_timedelta(diff) - # `cftime` objects only support arithmetic using `timedelta` objects, so - # the values of `diffs` must be casted from `dtype="timedelta64[ns]"` - # to `timedelta` objects. + hrs = diff.seconds / 3600 daily_subfreq = int(24 / hrs) # type: ignore