21
21
unpack_for_encoding ,
22
22
)
23
23
from xarray .core import indexing
24
- from xarray .core .array_api_compat import get_array_namespace
25
24
from xarray .core .common import contains_cftime_datetimes , is_np_datetime_like
26
- from xarray .core .duck_array_ops import (
27
- array_all ,
28
- array_any ,
29
- asarray ,
30
- astype ,
31
- concatenate ,
32
- isnull ,
33
- ravel ,
34
- reshape ,
35
- )
25
+ from xarray .core .duck_array_ops import array_all , array_any , asarray , ravel , reshape
36
26
from xarray .core .formatting import first_n_items , format_timestamp , last_item
37
27
from xarray .core .pdcompat import default_precision_timestamp , timestamp_as_unit
38
28
from xarray .core .utils import attempt_import , emit_user_level_warning
39
29
from xarray .core .variable import Variable
40
30
from xarray .namedarray .parallelcompat import T_ChunkedArray , get_chunked_array_type
41
- from xarray .namedarray .pycompat import is_chunked_array , to_duck_array , to_numpy
31
+ from xarray .namedarray .pycompat import is_chunked_array , to_numpy
42
32
from xarray .namedarray .utils import is_duck_dask_array
43
33
44
34
try :
@@ -110,7 +100,7 @@ def _is_numpy_compatible_time_range(times):
110
100
if is_np_datetime_like (times .dtype ):
111
101
return True
112
102
# times array contains cftime objects
113
- times = to_duck_array (times )
103
+ times = np . asarray (times )
114
104
tmin = times .min ()
115
105
tmax = times .max ()
116
106
try :
@@ -319,9 +309,8 @@ def _decode_cf_datetime_dtype(
319
309
# successfully. Otherwise, tracebacks end up swallowed by
320
310
# Dataset.__repr__ when users try to view their lazily decoded array.
321
311
values = indexing .ImplicitToExplicitIndexingAdapter (indexing .as_indexable (data ))
322
- zero = asarray ([0 ], xp = get_array_namespace (values ))
323
- example_value = concatenate (
324
- [first_n_items (values , 1 ) or zero , last_item (values ) or zero ]
312
+ example_value = np .concatenate (
313
+ [to_numpy (first_n_items (values , 1 ) or [0 ]), to_numpy (last_item (values ) or [0 ])]
325
314
)
326
315
327
316
try :
@@ -353,13 +342,7 @@ def _decode_datetime_with_cftime(
353
342
cftime = attempt_import ("cftime" )
354
343
if num_dates .size > 0 :
355
344
return np .asarray (
356
- cftime .num2date (
357
- # cftime uses Cython so we must convert to numpy here.
358
- to_numpy (num_dates ),
359
- units ,
360
- calendar ,
361
- only_use_cftime_datetimes = True ,
362
- )
345
+ cftime .num2date (num_dates , units , calendar , only_use_cftime_datetimes = True )
363
346
)
364
347
else :
365
348
return np .array ([], dtype = object )
@@ -374,7 +357,7 @@ def _check_date_for_units_since_refdate(
374
357
f"Value { date } can't be represented as Datetime/Timedelta."
375
358
)
376
359
delta = date * np .timedelta64 (1 , unit )
377
- if not isnull (delta ):
360
+ if not np . isnan (delta ):
378
361
# this will raise on dtype overflow for integer dtypes
379
362
if date .dtype .kind in "u" and not np .int64 (delta ) == date :
380
363
raise OutOfBoundsTimedelta (
@@ -398,7 +381,7 @@ def _check_timedelta_range(value, data_unit, time_unit):
398
381
"ignore" , "invalid value encountered in multiply" , RuntimeWarning
399
382
)
400
383
delta = value * np .timedelta64 (1 , data_unit )
401
- if not isnull (delta ):
384
+ if not np . isnan (delta ):
402
385
# this will raise on dtype overflow for integer dtypes
403
386
if value .dtype .kind in "u" and not np .int64 (delta ) == value :
404
387
raise OutOfBoundsTimedelta (
@@ -466,9 +449,9 @@ def _decode_datetime_with_pandas(
466
449
# respectively. See https://github.com/pandas-dev/pandas/issues/56996 for
467
450
# more details.
468
451
if flat_num_dates .dtype .kind == "i" :
469
- flat_num_dates = astype (flat_num_dates , np .int64 )
452
+ flat_num_dates = flat_num_dates . astype (np .int64 )
470
453
elif flat_num_dates .dtype .kind == "u" :
471
- flat_num_dates = astype (flat_num_dates , np .uint64 )
454
+ flat_num_dates = flat_num_dates . astype (np .uint64 )
472
455
473
456
try :
474
457
time_unit , ref_date = _unpack_time_unit_and_ref_date (units )
@@ -500,9 +483,9 @@ def _decode_datetime_with_pandas(
500
483
# overflow when converting to np.int64 would not be representable with a
501
484
# timedelta64 value, and therefore would raise an error in the lines above.
502
485
if flat_num_dates .dtype .kind in "iu" :
503
- flat_num_dates = astype (flat_num_dates , np .int64 )
486
+ flat_num_dates = flat_num_dates . astype (np .int64 )
504
487
elif flat_num_dates .dtype .kind in "f" :
505
- flat_num_dates = astype (flat_num_dates , np .float64 )
488
+ flat_num_dates = flat_num_dates . astype (np .float64 )
506
489
507
490
timedeltas = _numbers_to_timedelta (
508
491
flat_num_dates , time_unit , ref_date .unit , "datetime"
@@ -545,12 +528,8 @@ def decode_cf_datetime(
545
528
)
546
529
except (KeyError , OutOfBoundsDatetime , OutOfBoundsTimedelta , OverflowError ):
547
530
dates = _decode_datetime_with_cftime (
548
- astype (flat_num_dates , float ), units , calendar
531
+ flat_num_dates . astype (float ), units , calendar
549
532
)
550
- # This conversion to numpy is only needed for nanarg* below.
551
- # TODO: explore removing it.
552
- # Note that `dates` is already a numpy object array of cftime objects.
553
- num_dates = to_numpy (num_dates )
554
533
# retrieve cftype
555
534
dates_min = dates [np .nanargmin (num_dates )]
556
535
dates_max = dates [np .nanargmax (num_dates )]
@@ -607,16 +586,16 @@ def _numbers_to_timedelta(
607
586
"""Transform numbers to np.timedelta64."""
608
587
# keep NaT/nan mask
609
588
if flat_num .dtype .kind == "f" :
610
- nan = isnull ( flat_num )
589
+ nan = np . asarray ( np . isnan ( flat_num ) )
611
590
elif flat_num .dtype .kind == "i" :
612
- nan = flat_num == np .iinfo (np .int64 ).min
591
+ nan = np . asarray ( flat_num == np .iinfo (np .int64 ).min )
613
592
614
593
# in case we need to change the unit, we fix the numbers here
615
594
# this should be safe, as errors would have been raised above
616
595
ns_time_unit = _NS_PER_TIME_DELTA [time_unit ]
617
596
ns_ref_date_unit = _NS_PER_TIME_DELTA [ref_unit ]
618
597
if ns_time_unit > ns_ref_date_unit :
619
- flat_num = flat_num * np .int64 (ns_time_unit / ns_ref_date_unit )
598
+ flat_num = np . asarray ( flat_num * np .int64 (ns_time_unit / ns_ref_date_unit ) )
620
599
time_unit = ref_unit
621
600
622
601
# estimate fitting resolution for floating point values
@@ -639,12 +618,12 @@ def _numbers_to_timedelta(
639
618
# to prevent casting NaN to int
640
619
with warnings .catch_warnings ():
641
620
warnings .simplefilter ("ignore" , RuntimeWarning )
642
- flat_num = astype (flat_num , np .int64 )
643
- if array_any ( nan ):
621
+ flat_num = flat_num . astype (np .int64 )
622
+ if nan . any ( ):
644
623
flat_num [nan ] = np .iinfo (np .int64 ).min
645
624
646
625
# cast to wanted type
647
- return astype (flat_num , f"timedelta64[{ time_unit } ]" )
626
+ return flat_num . astype (f"timedelta64[{ time_unit } ]" )
648
627
649
628
650
629
def decode_cf_timedelta (
@@ -733,8 +712,8 @@ def infer_datetime_units(dates) -> str:
733
712
'hours', 'minutes' or 'seconds' (the first one that can evenly divide all
734
713
unique time deltas in `dates`)
735
714
"""
736
- dates = ravel (to_duck_array (dates ))
737
- if np .issubdtype (dates .dtype , "datetime64" ):
715
+ dates = ravel (np . asarray (dates ))
716
+ if np .issubdtype (np . asarray ( dates ) .dtype , "datetime64" ):
738
717
dates = to_datetime_unboxed (dates )
739
718
dates = dates [pd .notnull (dates )]
740
719
reference_date = dates [0 ] if len (dates ) > 0 else "1970-01-01"
0 commit comments