@@ -378,33 +378,6 @@ def date2num(date, unit, calendar):
378
378
return unit_inst .date2num (date )
379
379
380
380
381
- def _discard_microsecond (date ):
382
- """
383
- Return a date with the microsecond component discarded.
384
-
385
- Works for scalars, sequences and numpy arrays. Returns a scalar
386
- if input is a scalar, else returns a numpy array.
387
-
388
- Args:
389
-
390
- * date (datetime.datetime or cftime.datetime):
391
- Date value/s
392
-
393
- Returns:
394
- datetime, or numpy.ndarray of datetime object.
395
-
396
- """
397
- dates = np .asanyarray (date )
398
- shape = dates .shape
399
- dates = dates .ravel ()
400
-
401
- # using the "and" pattern to support masked arrays of datetimes
402
- dates = np .array ([dt and dt .replace (microsecond = 0 ) for dt in dates ])
403
- result = dates [0 ] if shape == () else dates .reshape (shape )
404
-
405
- return result
406
-
407
-
408
381
def num2date (
409
382
time_value ,
410
383
unit ,
@@ -537,47 +510,16 @@ def _num2date_to_nearest_second(
537
510
datetime, or numpy.ndarray of datetime object.
538
511
539
512
"""
540
- time_values = np .asanyarray (time_value )
541
- shape = time_values .shape
542
- time_values = time_values .ravel ()
543
513
544
- # We account for the edge case where the time is in seconds and has a
545
- # half second: cftime.num2date() may produce a date that would round
546
- # down.
547
- #
548
- # Note that this behaviour is different to the num2date function in version
549
- # 1.1 and earlier of cftime that didn't have microsecond precision. In
550
- # those versions, a half-second value would be rounded up or down
551
- # arbitrarily. It is probably not possible to replicate that behaviour with
552
- # later versions, if one wished to do so for the sake of consistency.
553
514
cftime_unit = unit .cftime_unit
554
- time_units = cftime_unit .split (" " )[0 ]
555
- has_half_seconds = np .logical_and (
556
- time_units == "seconds" , time_values % 1.0 == 0.5
557
- )
515
+
558
516
num2date_kwargs = dict (
559
517
units = cftime_unit ,
560
518
calendar = unit .calendar ,
561
519
only_use_cftime_datetimes = only_use_cftime_datetimes ,
562
520
only_use_python_datetimes = only_use_python_datetimes ,
563
521
)
564
- dates = cftime .num2date (time_values , ** num2date_kwargs )
565
- # try:
566
- # # We can assume all or none of the dates have a microsecond attribute
567
- # microseconds = np.array([d.microsecond if d else 0 for d in dates])
568
- # except AttributeError:
569
- # microseconds = 0
570
- # round_mask = np.logical_or(has_half_seconds, microseconds != 0)
571
- # ceil_mask = np.logical_or(has_half_seconds, microseconds >= 500000)
572
- # if time_values[ceil_mask].size > 0:
573
- # useconds = Unit("second")
574
- # second_frac = useconds.convert(0.75, time_units)
575
- # dates[ceil_mask] = cftime.num2date(
576
- # time_values[ceil_mask] + second_frac, **num2date_kwargs
577
- # )
578
- # dates[round_mask] = _discard_microsecond(dates[round_mask])
579
- result = dates [0 ] if shape == () else dates .reshape (shape )
580
- return result
522
+ return cftime .num2date (time_value , ** num2date_kwargs )
581
523
582
524
583
525
_CACHE = {}
@@ -1978,8 +1920,6 @@ def date2num(self, date):
1978
1920
array([5.5, 6.5])
1979
1921
1980
1922
"""
1981
-
1982
- # date = _discard_microsecond(date)
1983
1923
return cftime .date2num (date , self .cftime_unit , self .calendar )
1984
1924
1985
1925
def num2date (
0 commit comments