@@ -946,41 +946,38 @@ def _encode_datetime_with_cftime(dates, units: str, calendar: str) -> np.ndarray
946
946
else :
947
947
cftime = attempt_import ("cftime" )
948
948
949
+ dates = np .asarray (dates )
950
+ original_shape = dates .shape
951
+
949
952
if np .issubdtype (dates .dtype , np .datetime64 ):
950
953
# numpy's broken datetime conversion only works for us precision
951
954
dates = dates .astype ("M8[us]" ).astype (datetime )
952
955
953
- def wrap_dt (dt ):
954
- # convert to cftime proleptic gregorian in case of datetime.datetime
955
- # needed because of https://github.com/Unidata/cftime/issues/354
956
- if isinstance (dt , datetime ) and not isinstance (dt , cftime .datetime ):
957
- dt = cftime .datetime (
958
- dt .year ,
959
- dt .month ,
960
- dt .day ,
961
- dt .hour ,
962
- dt .minute ,
963
- dt .second ,
964
- dt .microsecond ,
965
- calendar = "proleptic_gregorian" ,
966
- )
967
- return dt
956
+ dates = np .atleast_1d (dates )
968
957
969
- def encode_datetime (d ):
970
- # Since netCDF files do not support storing float128 values, we ensure
971
- # that float64 values are used by setting longdouble=False in num2date.
972
- # This try except logic can be removed when xarray's minimum version of
973
- # cftime is at least 1.6.2.
974
- try :
975
- return (
976
- np .nan
977
- if d is None
978
- else cftime .date2num (wrap_dt (d ), units , calendar , longdouble = False )
979
- )
980
- except TypeError :
981
- return np .nan if d is None else cftime .date2num (wrap_dt (d ), units , calendar )
958
+ # Find all the None position
959
+ none_position = dates == None # noqa: E711
960
+ filtered_dates = dates [~ none_position ]
961
+
962
+ # Since netCDF files do not support storing float128 values, we ensure
963
+ # that float64 values are used by setting longdouble=False in num2date.
964
+ # This try except logic can be removed when xarray's minimum version of
965
+ # cftime is at least 1.6.2.
966
+ try :
967
+ encoded_nums = cftime .date2num (
968
+ filtered_dates , units , calendar , longdouble = False
969
+ )
970
+ except TypeError :
971
+ encoded_nums = cftime .date2num (filtered_dates , units , calendar )
972
+
973
+ if filtered_dates .size == none_position .size :
974
+ return encoded_nums .reshape (original_shape )
982
975
983
- return reshape (np .array ([encode_datetime (d ) for d in ravel (dates )]), dates .shape )
976
+ # Create a full matrix of NaN
977
+ # And fill the num dates in the not NaN or None position
978
+ result = np .full (dates .shape , np .nan )
979
+ result [np .nonzero (~ none_position )] = encoded_nums
980
+ return result .reshape (original_shape )
984
981
985
982
986
983
def cast_to_int_if_safe (num ) -> np .ndarray :
0 commit comments