|
54 | 54 | ([[0]], "days since 1000-01-01"),
|
55 | 55 | (np.arange(2), "days since 1000-01-01"),
|
56 | 56 | (np.arange(0, 100000, 20000), "days since 1900-01-01"),
|
| 57 | + (np.arange(0, 100000, 20000), "days since 1-01-01"), |
57 | 58 | (17093352.0, "hours since 1-1-1 00:00:0.0"),
|
58 | 59 | ([0.5, 1.5], "hours since 1900-01-01T00:00:00"),
|
59 | 60 | (0, "milliseconds since 2000-01-01T00:00:00"),
|
@@ -109,20 +110,16 @@ def test_cf_datetime(num_dates, units, calendar):
|
109 | 110 | # https://github.com/Unidata/netcdf4-python/issues/355
|
110 | 111 | assert (abs_diff <= np.timedelta64(1, "s")).all()
|
111 | 112 | encoded, _, _ = coding.times.encode_cf_datetime(actual, units, calendar)
|
112 |
| - if "1-1-1" not in units: |
113 |
| - # pandas parses this date very strangely, so the original |
114 |
| - # units/encoding cannot be preserved in this case: |
115 |
| - # (Pdb) pd.to_datetime('1-1-1 00:00:0.0') |
116 |
| - # Timestamp('2001-01-01 00:00:00') |
| 113 | + |
| 114 | + assert_array_equal(num_dates, np.around(encoded, 1)) |
| 115 | + if hasattr(num_dates, "ndim") and num_dates.ndim == 1 and "1000" not in units: |
| 116 | + # verify that wrapping with a pandas.Index works |
| 117 | + # note that it *does not* currently work to put |
| 118 | + # non-datetime64 compatible dates into a pandas.Index |
| 119 | + encoded, _, _ = coding.times.encode_cf_datetime( |
| 120 | + pd.Index(actual), units, calendar |
| 121 | + ) |
117 | 122 | assert_array_equal(num_dates, np.around(encoded, 1))
|
118 |
| - if hasattr(num_dates, "ndim") and num_dates.ndim == 1 and "1000" not in units: |
119 |
| - # verify that wrapping with a pandas.Index works |
120 |
| - # note that it *does not* currently work to even put |
121 |
| - # non-datetime64 compatible dates into a pandas.Index |
122 |
| - encoded, _, _ = coding.times.encode_cf_datetime( |
123 |
| - pd.Index(actual), units, calendar |
124 |
| - ) |
125 |
| - assert_array_equal(num_dates, np.around(encoded, 1)) |
126 | 123 |
|
127 | 124 |
|
128 | 125 | @requires_cftime
|
@@ -928,3 +925,35 @@ def test_use_cftime_false_non_standard_calendar(calendar, units_year):
|
928 | 925 | units = f"days since {units_year}-01-01"
|
929 | 926 | with pytest.raises(OutOfBoundsDatetime):
|
930 | 927 | decode_cf_datetime(numerical_dates, units, calendar, use_cftime=False)
|
| 928 | + |
| 929 | + |
| 930 | +@requires_cftime |
| 931 | +@pytest.mark.parametrize("calendar", _ALL_CALENDARS) |
| 932 | +def test_decode_ambiguous_time_warns(calendar): |
| 933 | + # GH 4422, 4506 |
| 934 | + from cftime import num2date |
| 935 | + |
| 936 | + # we don't decode non-standard calendards with |
| 937 | + # pandas so expect no warning will be emitted |
| 938 | + is_standard_calendar = calendar in coding.times._STANDARD_CALENDARS |
| 939 | + |
| 940 | + dates = [1, 2, 3] |
| 941 | + units = "days since 1-1-1" |
| 942 | + expected = num2date(dates, units, calendar=calendar, only_use_cftime_datetimes=True) |
| 943 | + |
| 944 | + exp_warn_type = SerializationWarning if is_standard_calendar else None |
| 945 | + |
| 946 | + with pytest.warns(exp_warn_type) as record: |
| 947 | + result = decode_cf_datetime(dates, units, calendar=calendar) |
| 948 | + |
| 949 | + if is_standard_calendar: |
| 950 | + relevant_warnings = [ |
| 951 | + r |
| 952 | + for r in record.list |
| 953 | + if str(r.message).startswith("Ambiguous reference date string: 1-1-1") |
| 954 | + ] |
| 955 | + assert len(relevant_warnings) == 1 |
| 956 | + else: |
| 957 | + assert not record |
| 958 | + |
| 959 | + np.testing.assert_array_equal(result, expected) |
0 commit comments