Skip to content

Commit 7ff5d8d

Browse files
authored
Use reshape and ravel from duck_array_ops in coding/times.py (#9225)
* Use duck_array_ops.ravel * Use duck_array_ops.reshape
1 parent 879b06b commit 7ff5d8d

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

xarray/coding/times.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
)
2323
from xarray.core import indexing
2424
from xarray.core.common import contains_cftime_datetimes, is_np_datetime_like
25-
from xarray.core.duck_array_ops import asarray
25+
from xarray.core.duck_array_ops import asarray, ravel, reshape
2626
from xarray.core.formatting import first_n_items, format_timestamp, last_item
2727
from xarray.core.pdcompat import nanosecond_precision_timestamp
2828
from xarray.core.utils import emit_user_level_warning
@@ -315,7 +315,7 @@ def decode_cf_datetime(
315315
cftime.num2date
316316
"""
317317
num_dates = np.asarray(num_dates)
318-
flat_num_dates = num_dates.ravel()
318+
flat_num_dates = ravel(num_dates)
319319
if calendar is None:
320320
calendar = "standard"
321321

@@ -348,7 +348,7 @@ def decode_cf_datetime(
348348
else:
349349
dates = _decode_datetime_with_pandas(flat_num_dates, units, calendar)
350350

351-
return dates.reshape(num_dates.shape)
351+
return reshape(dates, num_dates.shape)
352352

353353

354354
def to_timedelta_unboxed(value, **kwargs):
@@ -369,8 +369,8 @@ def decode_cf_timedelta(num_timedeltas, units: str) -> np.ndarray:
369369
"""
370370
num_timedeltas = np.asarray(num_timedeltas)
371371
units = _netcdf_to_numpy_timeunit(units)
372-
result = to_timedelta_unboxed(num_timedeltas.ravel(), unit=units)
373-
return result.reshape(num_timedeltas.shape)
372+
result = to_timedelta_unboxed(ravel(num_timedeltas), unit=units)
373+
return reshape(result, num_timedeltas.shape)
374374

375375

376376
def _unit_timedelta_cftime(units: str) -> timedelta:
@@ -428,7 +428,7 @@ def infer_datetime_units(dates) -> str:
428428
'hours', 'minutes' or 'seconds' (the first one that can evenly divide all
429429
unique time deltas in `dates`)
430430
"""
431-
dates = np.asarray(dates).ravel()
431+
dates = ravel(np.asarray(dates))
432432
if np.asarray(dates).dtype == "datetime64[ns]":
433433
dates = to_datetime_unboxed(dates)
434434
dates = dates[pd.notnull(dates)]
@@ -456,7 +456,7 @@ def infer_timedelta_units(deltas) -> str:
456456
{'days', 'hours', 'minutes' 'seconds'} (the first one that can evenly
457457
divide all unique time deltas in `deltas`)
458458
"""
459-
deltas = to_timedelta_unboxed(np.asarray(deltas).ravel())
459+
deltas = to_timedelta_unboxed(ravel(np.asarray(deltas)))
460460
unique_timedeltas = np.unique(deltas[pd.notnull(deltas)])
461461
return _infer_time_units_from_diff(unique_timedeltas)
462462

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

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

648648

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

759759
# retrieve needed units to faithfully encode to int64
@@ -791,7 +791,7 @@ def _eagerly_encode_cf_datetime(
791791
floor_division = True
792792

793793
num = _division(time_deltas, time_delta, floor_division)
794-
num = num.values.reshape(dates.shape)
794+
num = reshape(num.values, dates.shape)
795795

796796
except (OutOfBoundsDatetime, OverflowError, ValueError):
797797
num = _encode_datetime_with_cftime(dates, units, calendar)
@@ -879,7 +879,7 @@ def _eagerly_encode_cf_timedelta(
879879
units = data_units
880880

881881
time_delta = _time_units_to_timedelta64(units)
882-
time_deltas = pd.TimedeltaIndex(timedeltas.ravel())
882+
time_deltas = pd.TimedeltaIndex(ravel(timedeltas))
883883

884884
# retrieve needed units to faithfully encode to int64
885885
needed_units = data_units
@@ -911,7 +911,7 @@ def _eagerly_encode_cf_timedelta(
911911
floor_division = True
912912

913913
num = _division(time_deltas, time_delta, floor_division)
914-
num = num.values.reshape(timedeltas.shape)
914+
num = reshape(num.values, timedeltas.shape)
915915

916916
if dtype is not None:
917917
num = _cast_to_dtype_if_safe(num, dtype)

0 commit comments

Comments
 (0)