(
dates: T_DuckArray, # type: ignore[misc]
units: str | None = None,
calendar: str | None = None,
dtype: np.dtype | None = None,
allow_units_modification: bool = True,
)
| 1062 | |
| 1063 | |
| 1064 | def _eagerly_encode_cf_datetime( |
| 1065 | dates: T_DuckArray, # type: ignore[misc] |
| 1066 | units: str | None = None, |
| 1067 | calendar: str | None = None, |
| 1068 | dtype: np.dtype | None = None, |
| 1069 | allow_units_modification: bool = True, |
| 1070 | ) -> tuple[T_DuckArray, str, str]: |
| 1071 | dates = asarray(dates) |
| 1072 | data_units = infer_datetime_units(dates) |
| 1073 | if units is None: |
| 1074 | units = data_units |
| 1075 | else: |
| 1076 | units = _cleanup_netcdf_time_units(units) |
| 1077 | |
| 1078 | if calendar is None: |
| 1079 | calendar = infer_calendar_name(dates) |
| 1080 | |
| 1081 | raise_incompatible_units_error = False |
| 1082 | raise_gregorian_proleptic_gregorian_mismatch_error = False |
| 1083 | try: |
| 1084 | if not _is_standard_calendar(calendar) or dates.dtype.kind == "O": |
| 1085 | # parse with cftime instead |
| 1086 | raise OutOfBoundsDatetime |
| 1087 | assert np.issubdtype(dates.dtype, "datetime64") |
| 1088 | if ( |
| 1089 | calendar in ["standard", "gregorian"] |
| 1090 | and dates.size > 0 |
| 1091 | and np.nanmin(dates).astype("=M8[us]").astype(datetime) |
| 1092 | < datetime(1582, 10, 15) |
| 1093 | ): |
| 1094 | raise_gregorian_proleptic_gregorian_mismatch_error = True |
| 1095 | |
| 1096 | time_unit, ref_date = _unpack_time_unit_and_ref_date(units) |
| 1097 | # calendar equivalence only for days after the reform |
| 1098 | _check_date_is_after_shift(ref_date, calendar) |
| 1099 | time_delta = np.timedelta64(1, time_unit) |
| 1100 | |
| 1101 | # Wrap the dates in a DatetimeIndex to do the subtraction to ensure |
| 1102 | # an OverflowError is raised if the ref_date is too far away from |
| 1103 | # dates to be encoded (GH 2272). |
| 1104 | # DatetimeIndex will convert to units of ["s", "ms", "us", "ns"] |
| 1105 | dates_as_index = pd.DatetimeIndex(ravel(dates)) |
| 1106 | time_deltas = dates_as_index - ref_date |
| 1107 | |
| 1108 | # retrieve needed units to faithfully encode to int64 |
| 1109 | needed_units = _infer_needed_units_numpy(ref_date, data_units) |
| 1110 | needed_time_delta = _unit_timedelta_numpy(needed_units) |
| 1111 | |
| 1112 | floor_division = np.issubdtype(dtype, np.integer) or dtype is None |
| 1113 | if time_delta > needed_time_delta: |
| 1114 | floor_division = False |
| 1115 | if dtype is None: |
| 1116 | emit_user_level_warning( |
| 1117 | f"Times can't be serialized faithfully to int64 with requested units {units!r}. " |
| 1118 | f"Resolution of {needed_units!r} needed. Serializing times to floating point instead. " |
| 1119 | f"Set encoding['dtype'] to integer dtype to serialize to int64. " |
| 1120 | f"Set encoding['dtype'] to floating point dtype to silence this warning." |
| 1121 | ) |
no test coverage detected
searching dependent graphs…