(
timedeltas: T_DuckArray, # type: ignore[misc]
units: str | None = None,
dtype: np.dtype | None = None,
allow_units_modification: bool = True,
)
| 1253 | |
| 1254 | |
| 1255 | def _eagerly_encode_cf_timedelta( |
| 1256 | timedeltas: T_DuckArray, # type: ignore[misc] |
| 1257 | units: str | None = None, |
| 1258 | dtype: np.dtype | None = None, |
| 1259 | allow_units_modification: bool = True, |
| 1260 | ) -> tuple[T_DuckArray, str]: |
| 1261 | data_units = infer_timedelta_units(timedeltas) |
| 1262 | if units is None: |
| 1263 | units = data_units |
| 1264 | # units take precedence in the case of zero-size array |
| 1265 | if timedeltas.size == 0: |
| 1266 | data_units = units |
| 1267 | |
| 1268 | time_delta = _unit_timedelta_numpy(units) |
| 1269 | time_deltas = pd.TimedeltaIndex(ravel(timedeltas)) |
| 1270 | # get resolution of TimedeltaIndex and align time_delta |
| 1271 | deltas_unit = time_deltas.unit |
| 1272 | time_delta = time_delta.astype(f"=m8[{deltas_unit}]") |
| 1273 | |
| 1274 | # retrieve needed units to faithfully encode to int64 |
| 1275 | needed_units = data_units |
| 1276 | if data_units != units: |
| 1277 | needed_units = _infer_time_units_from_diff(np.unique(time_deltas.dropna())) |
| 1278 | |
| 1279 | # needed time delta to encode faithfully to int64 |
| 1280 | needed_time_delta = _unit_timedelta_numpy(needed_units) |
| 1281 | |
| 1282 | floor_division = np.issubdtype(dtype, np.integer) or dtype is None |
| 1283 | if time_delta > needed_time_delta: |
| 1284 | floor_division = False |
| 1285 | if dtype is None: |
| 1286 | emit_user_level_warning( |
| 1287 | f"Timedeltas can't be serialized faithfully to int64 with requested units {units!r}. " |
| 1288 | f"Resolution of {needed_units!r} needed. Serializing timeseries to floating point instead. " |
| 1289 | f"Set encoding['dtype'] to integer dtype to serialize to int64. " |
| 1290 | f"Set encoding['dtype'] to floating point dtype to silence this warning." |
| 1291 | ) |
| 1292 | elif np.issubdtype(dtype, np.integer) and allow_units_modification: |
| 1293 | emit_user_level_warning( |
| 1294 | f"Timedeltas can't be serialized faithfully with requested units {units!r}. " |
| 1295 | f"Serializing with units {needed_units!r} instead. " |
| 1296 | f"Set encoding['dtype'] to floating point dtype to serialize with units {units!r}. " |
| 1297 | f"Set encoding['units'] to {needed_units!r} to silence this warning ." |
| 1298 | ) |
| 1299 | units = needed_units |
| 1300 | time_delta = needed_time_delta |
| 1301 | time_delta = time_delta.astype(f"=m8[{deltas_unit}]") |
| 1302 | floor_division = True |
| 1303 | elif np.issubdtype(dtype, np.integer) and not allow_units_modification: |
| 1304 | raise ValueError( |
| 1305 | f"Timedeltas can't be serialized faithfully to int64 with requested units {units!r}. " |
| 1306 | f"Consider setting encoding['dtype'] to a floating point dtype to serialize with " |
| 1307 | f"units {units!r}. Consider setting encoding['units'] to {needed_units!r} to " |
| 1308 | f"serialize with an integer dtype." |
| 1309 | ) |
| 1310 | |
| 1311 | num = _division(time_deltas, time_delta, floor_division) |
| 1312 | num = reshape(num.values, timedeltas.shape) |
no test coverage detected
searching dependent graphs…