MCPcopy Index your code
hub / github.com/pydata/xarray / _lazily_encode_cf_datetime

Function _lazily_encode_cf_datetime

xarray/coding/times.py:1204–1240  ·  view source on GitHub ↗
(
    dates: T_ChunkedArray,
    units: str | None = None,
    calendar: str | None = None,
    dtype: np.dtype | None = None,
)

Source from the content-addressed store, hash-verified

1202
1203
1204def _lazily_encode_cf_datetime(
1205 dates: T_ChunkedArray,
1206 units: str | None = None,
1207 calendar: str | None = None,
1208 dtype: np.dtype | None = None,
1209) -> tuple[T_ChunkedArray, str, str]:
1210 if calendar is None:
1211 # This will only trigger minor compute if dates is an object dtype array.
1212 calendar = infer_calendar_name(dates)
1213
1214 if units is None and dtype is None:
1215 if dates.dtype == "O":
1216 units = "microseconds since 1970-01-01"
1217 dtype = np.dtype("int64")
1218 else:
1219 netcdf_unit = _numpy_dtype_to_netcdf_timeunit(dates.dtype)
1220 units = f"{netcdf_unit} since 1970-01-01"
1221 dtype = np.dtype("int64")
1222
1223 if units is None or dtype is None:
1224 raise ValueError(
1225 f"When encoding chunked arrays of datetime values, both the units "
1226 f"and dtype must be prescribed or both must be unprescribed. "
1227 f"Prescribing only one or the other is not currently supported. "
1228 f"Got a units encoding of {units} and a dtype encoding of {dtype}."
1229 )
1230
1231 chunkmanager = get_chunked_array_type(dates)
1232 num = chunkmanager.map_blocks(
1233 _encode_cf_datetime_within_map_blocks,
1234 dates,
1235 units,
1236 calendar,
1237 dtype,
1238 dtype=dtype,
1239 )
1240 return num, units, calendar
1241
1242
1243def encode_cf_timedelta(

Callers 1

encode_cf_datetimeFunction · 0.85

Calls 5

get_chunked_array_typeFunction · 0.90
infer_calendar_nameFunction · 0.85
dtypeMethod · 0.45
map_blocksMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…