Skip to content

Commit

Permalink
Update _create_time_bounds()
Browse files Browse the repository at this point in the history
- Add comment about needing to convert dype=timedelta64[ns] to pandas timedelta object
- Ignore C901 flake8 error
  • Loading branch information
tomvothecoder committed Jun 6, 2024
1 parent fb90864 commit 4c290e7
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions xcdat/bounds.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)

Check warning on line 612 in xcdat/bounds.py

View check run for this annotation

Codecov / codecov/patch

xcdat/bounds.py#L612

Added line #L612 was not covered by tests
# `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

Expand Down

0 comments on commit 4c290e7

Please sign in to comment.