8
8
import numpy as np
9
9
import pandas as pd
10
10
import pytest
11
- from pandas .errors import OutOfBoundsDatetime
11
+ from pandas .errors import OutOfBoundsDatetime , OutOfBoundsTimedelta
12
12
13
13
from xarray import (
14
14
DataArray ,
@@ -1136,11 +1136,16 @@ def test_should_cftime_be_used_target_not_npable():
1136
1136
_should_cftime_be_used (src , "noleap" , False )
1137
1137
1138
1138
1139
- @pytest .mark .parametrize ("dtype" , [np .uint8 , np .uint16 , np .uint32 , np .uint64 ])
1140
- def test_decode_cf_datetime_uint (dtype ):
1139
+ @pytest .mark .parametrize (
1140
+ "dtype" ,
1141
+ [np .int8 , np .int16 , np .int32 , np .int64 , np .uint8 , np .uint16 , np .uint32 , np .uint64 ],
1142
+ )
1143
+ def test_decode_cf_datetime_varied_integer_dtypes (dtype ):
1141
1144
units = "seconds since 2018-08-22T03:23:03Z"
1142
1145
num_dates = dtype (50 )
1143
- result = decode_cf_datetime (num_dates , units )
1146
+ # Set use_cftime=False to ensure we cannot mask a failure by falling back
1147
+ # to cftime.
1148
+ result = decode_cf_datetime (num_dates , units , use_cftime = False )
1144
1149
expected = np .asarray (np .datetime64 ("2018-08-22T03:23:53" , "ns" ))
1145
1150
np .testing .assert_equal (result , expected )
1146
1151
@@ -1154,6 +1159,14 @@ def test_decode_cf_datetime_uint64_with_cftime():
1154
1159
np .testing .assert_equal (result , expected )
1155
1160
1156
1161
1162
+ def test_decode_cf_datetime_uint64_with_pandas_overflow_error ():
1163
+ units = "nanoseconds since 1970-01-01"
1164
+ calendar = "standard"
1165
+ num_dates = np .uint64 (1_000_000 * 86_400 * 360 * 500_000 )
1166
+ with pytest .raises (OutOfBoundsTimedelta ):
1167
+ decode_cf_datetime (num_dates , units , calendar , use_cftime = False )
1168
+
1169
+
1157
1170
@requires_cftime
1158
1171
def test_decode_cf_datetime_uint64_with_cftime_overflow_error ():
1159
1172
units = "microseconds since 1700-01-01"
@@ -1438,10 +1451,8 @@ def test_roundtrip_float_times(fill_value, times, units, encoded_values) -> None
1438
1451
"days since 1700-01-01" ,
1439
1452
np .dtype ("int32" ),
1440
1453
),
1441
- "mixed-cftime-pandas-encoding-with-prescribed-units-and-dtype" : (
1442
- "250YS" ,
1443
- "days since 1700-01-01" ,
1444
- np .dtype ("int32" ),
1454
+ "mixed-cftime-pandas-encoding-with-prescribed-units-and-dtype" : pytest .param (
1455
+ "250YS" , "days since 1700-01-01" , np .dtype ("int32" ), marks = requires_cftime
1445
1456
),
1446
1457
"pandas-encoding-with-default-units-and-dtype" : ("250YS" , None , None ),
1447
1458
}
0 commit comments