@@ -626,18 +626,16 @@ def _encode_datetime_with_cftime(dates, units: str, calendar: str) -> np.ndarray
626
626
raise ModuleNotFoundError ("No module named 'cftime'" )
627
627
628
628
dates = np .array (dates )
629
-
630
- if dates .shape == ():
631
- dates = dates .reshape (1 )
629
+ original_shape = dates .shape
632
630
633
631
if np .issubdtype (dates .dtype , np .datetime64 ):
634
632
# numpy's broken datetime conversion only works for us precision
635
633
dates = dates .astype ("M8[us]" ).astype (datetime )
636
634
635
+ dates = np .atleast_1d (dates )
636
+
637
637
# Find all the None position
638
638
none_position = np .equal (dates , None )
639
-
640
- # Remove None from the dates and return new array
641
639
filtered_dates = dates [~ none_position ]
642
640
643
641
# Since netCDF files do not support storing float128 values, we ensure
@@ -651,11 +649,14 @@ def _encode_datetime_with_cftime(dates, units: str, calendar: str) -> np.ndarray
651
649
except TypeError :
652
650
encoded_nums = cftime .date2num (filtered_dates , units , calendar )
653
651
652
+ if filtered_dates .size == none_position .size :
653
+ return encoded_nums .reshape (original_shape )
654
+
654
655
# Create a full matrix of NaN
655
656
# And fill the num dates in the not NaN or None position
656
657
result = np .full (dates .shape , np .nan )
657
658
result [np .nonzero (~ none_position )] = encoded_nums
658
- return result
659
+ return result . reshape ( original_shape )
659
660
660
661
661
662
def cast_to_int_if_safe (num ) -> np .ndarray :
0 commit comments