Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use reshape and ravel from duck_array_ops in coding/times.py #9225

Merged
merged 2 commits into from
Jul 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions xarray/coding/times.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
)
from xarray.core import indexing
from xarray.core.common import contains_cftime_datetimes, is_np_datetime_like
from xarray.core.duck_array_ops import asarray
from xarray.core.duck_array_ops import asarray, ravel, reshape
from xarray.core.formatting import first_n_items, format_timestamp, last_item
from xarray.core.pdcompat import nanosecond_precision_timestamp
from xarray.core.utils import emit_user_level_warning
Expand Down Expand Up @@ -315,7 +315,7 @@ def decode_cf_datetime(
cftime.num2date
"""
num_dates = np.asarray(num_dates)
flat_num_dates = num_dates.ravel()
flat_num_dates = ravel(num_dates)
if calendar is None:
calendar = "standard"

Expand Down Expand Up @@ -348,7 +348,7 @@ def decode_cf_datetime(
else:
dates = _decode_datetime_with_pandas(flat_num_dates, units, calendar)

return dates.reshape(num_dates.shape)
return reshape(dates, num_dates.shape)


def to_timedelta_unboxed(value, **kwargs):
Expand All @@ -369,8 +369,8 @@ def decode_cf_timedelta(num_timedeltas, units: str) -> np.ndarray:
"""
num_timedeltas = np.asarray(num_timedeltas)
units = _netcdf_to_numpy_timeunit(units)
result = to_timedelta_unboxed(num_timedeltas.ravel(), unit=units)
return result.reshape(num_timedeltas.shape)
result = to_timedelta_unboxed(ravel(num_timedeltas), unit=units)
return reshape(result, num_timedeltas.shape)


def _unit_timedelta_cftime(units: str) -> timedelta:
Expand Down Expand Up @@ -428,7 +428,7 @@ def infer_datetime_units(dates) -> str:
'hours', 'minutes' or 'seconds' (the first one that can evenly divide all
unique time deltas in `dates`)
"""
dates = np.asarray(dates).ravel()
dates = ravel(np.asarray(dates))
if np.asarray(dates).dtype == "datetime64[ns]":
dates = to_datetime_unboxed(dates)
dates = dates[pd.notnull(dates)]
Expand Down Expand Up @@ -456,7 +456,7 @@ def infer_timedelta_units(deltas) -> str:
{'days', 'hours', 'minutes' 'seconds'} (the first one that can evenly
divide all unique time deltas in `deltas`)
"""
deltas = to_timedelta_unboxed(np.asarray(deltas).ravel())
deltas = to_timedelta_unboxed(ravel(np.asarray(deltas)))
unique_timedeltas = np.unique(deltas[pd.notnull(deltas)])
return _infer_time_units_from_diff(unique_timedeltas)

Expand Down Expand Up @@ -643,7 +643,7 @@ def encode_datetime(d):
except TypeError:
return np.nan if d is None else cftime.date2num(d, units, calendar)

return np.array([encode_datetime(d) for d in dates.ravel()]).reshape(dates.shape)
return reshape(np.array([encode_datetime(d) for d in ravel(dates)]), dates.shape)


def cast_to_int_if_safe(num) -> np.ndarray:
Expand Down Expand Up @@ -753,7 +753,7 @@ def _eagerly_encode_cf_datetime(
# Wrap the dates in a DatetimeIndex to do the subtraction to ensure
# an OverflowError is raised if the ref_date is too far away from
# dates to be encoded (GH 2272).
dates_as_index = pd.DatetimeIndex(dates.ravel())
dates_as_index = pd.DatetimeIndex(ravel(dates))
time_deltas = dates_as_index - ref_date

# retrieve needed units to faithfully encode to int64
Expand Down Expand Up @@ -791,7 +791,7 @@ def _eagerly_encode_cf_datetime(
floor_division = True

num = _division(time_deltas, time_delta, floor_division)
num = num.values.reshape(dates.shape)
num = reshape(num.values, dates.shape)

except (OutOfBoundsDatetime, OverflowError, ValueError):
num = _encode_datetime_with_cftime(dates, units, calendar)
Expand Down Expand Up @@ -879,7 +879,7 @@ def _eagerly_encode_cf_timedelta(
units = data_units

time_delta = _time_units_to_timedelta64(units)
time_deltas = pd.TimedeltaIndex(timedeltas.ravel())
time_deltas = pd.TimedeltaIndex(ravel(timedeltas))

# retrieve needed units to faithfully encode to int64
needed_units = data_units
Expand Down Expand Up @@ -911,7 +911,7 @@ def _eagerly_encode_cf_timedelta(
floor_division = True

num = _division(time_deltas, time_delta, floor_division)
num = num.values.reshape(timedeltas.shape)
num = reshape(num.values, timedeltas.shape)

if dtype is not None:
num = _cast_to_dtype_if_safe(num, dtype)
Expand Down
Loading