@@ -387,39 +387,6 @@ def date2num(date, unit, calendar):
387
387
return unit_inst .date2num (date )
388
388
389
389
390
- def _discard_microsecond (date ):
391
- """
392
- Return a date with the microsecond component discarded.
393
-
394
- Works for scalars, sequences and numpy arrays. Returns a scalar
395
- if input is a scalar, else returns a numpy array.
396
-
397
- Args:
398
-
399
- * date (datetime.datetime or cftime.datetime):
400
- Date value/s
401
-
402
- Returns:
403
- datetime, or numpy.ndarray of datetime object.
404
-
405
- """
406
- dates = np .asarray (date )
407
- shape = dates .shape
408
- dates = dates .ravel ()
409
- # Create date objects of the same type returned by cftime.num2date()
410
- # (either datetime.datetime or cftime.datetime), discarding the
411
- # microseconds
412
- dates = np .array (
413
- [
414
- d
415
- and d .__class__ (d .year , d .month , d .day , d .hour , d .minute , d .second )
416
- for d in dates
417
- ]
418
- )
419
- result = dates [0 ] if shape == () else dates .reshape (shape )
420
- return result
421
-
422
-
423
390
def num2date (
424
391
time_value ,
425
392
unit ,
@@ -552,47 +519,16 @@ def _num2date_to_nearest_second(
552
519
datetime, or numpy.ndarray of datetime object.
553
520
554
521
"""
555
- time_values = np .asanyarray (time_value )
556
- shape = time_values .shape
557
- time_values = time_values .ravel ()
558
522
559
- # We account for the edge case where the time is in seconds and has a
560
- # half second: cftime.num2date() may produce a date that would round
561
- # down.
562
- #
563
- # Note that this behaviour is different to the num2date function in version
564
- # 1.1 and earlier of cftime that didn't have microsecond precision. In
565
- # those versions, a half-second value would be rounded up or down
566
- # arbitrarily. It is probably not possible to replicate that behaviour with
567
- # later versions, if one wished to do so for the sake of consistency.
568
523
cftime_unit = unit .cftime_unit
569
- time_units = cftime_unit .split (" " )[0 ]
570
- has_half_seconds = np .logical_and (
571
- time_units == "seconds" , time_values % 1.0 == 0.5
572
- )
524
+
573
525
num2date_kwargs = dict (
574
526
units = cftime_unit ,
575
527
calendar = unit .calendar ,
576
528
only_use_cftime_datetimes = only_use_cftime_datetimes ,
577
529
only_use_python_datetimes = only_use_python_datetimes ,
578
530
)
579
- dates = cftime .num2date (time_values , ** num2date_kwargs )
580
- # try:
581
- # # We can assume all or none of the dates have a microsecond attribute
582
- # microseconds = np.array([d.microsecond if d else 0 for d in dates])
583
- # except AttributeError:
584
- # microseconds = 0
585
- # round_mask = np.logical_or(has_half_seconds, microseconds != 0)
586
- # ceil_mask = np.logical_or(has_half_seconds, microseconds >= 500000)
587
- # if time_values[ceil_mask].size > 0:
588
- # useconds = Unit("second")
589
- # second_frac = useconds.convert(0.75, time_units)
590
- # dates[ceil_mask] = cftime.num2date(
591
- # time_values[ceil_mask] + second_frac, **num2date_kwargs
592
- # )
593
- # dates[round_mask] = _discard_microsecond(dates[round_mask])
594
- result = dates [0 ] if shape == () else dates .reshape (shape )
595
- return result
531
+ return cftime .num2date (time_value , ** num2date_kwargs )
596
532
597
533
598
534
_CACHE = {}
@@ -1971,8 +1907,6 @@ def date2num(self, date):
1971
1907
array([5.5, 6.5])
1972
1908
1973
1909
"""
1974
-
1975
- # date = _discard_microsecond(date)
1976
1910
return cftime .date2num (date , self .cftime_unit , self .calendar )
1977
1911
1978
1912
def num2date (
0 commit comments