|
12 | 12 | _import_cftime, cftime_to_nptime, decode_cf_datetime, encode_cf_datetime)
|
13 | 13 | from xarray.conventions import _update_bounds_attributes
|
14 | 14 | from xarray.core.common import contains_cftime_datetimes
|
| 15 | +from xarray.testing import assert_equal |
15 | 16 |
|
16 | 17 | from . import (
|
17 | 18 | assert_array_equal, has_cftime, has_cftime_or_netCDF4, has_dask,
|
@@ -752,17 +753,28 @@ def test_encode_cf_datetime_pandas_min():
|
752 | 753 | assert calendar == expected_calendar
|
753 | 754 |
|
754 | 755 |
|
755 |
| -def test_encode_cf_datetime_units_with_tz(): |
| 756 | +@pytest.mark.skipif(not has_cftime_or_netCDF4, reason='cftime not installed') |
| 757 | +def test_time_units_with_timezone_roundtrip(calendar): |
756 | 758 | # Regression test for GH 2649
|
757 |
| - units = 'days since 2000-01-01T00:00:00-05:00' |
758 |
| - calendar = 'proleptic_gregorian' |
759 |
| - dates = pd.date_range('2000', periods=3, tz='US/Eastern').values |
760 |
| - num, units, calendar = encode_cf_datetime(dates, |
761 |
| - units=units, |
762 |
| - calendar=calendar) |
763 |
| - expected_num = np.array([0, 1, 2]) |
764 | 759 | expected_units = 'days since 2000-01-01T00:00:00-05:00'
|
765 |
| - expected_calendar = 'proleptic_gregorian' |
766 |
| - np.testing.assert_array_equal(num, expected_num) |
767 |
| - assert units == expected_units |
768 |
| - assert calendar == expected_calendar |
| 760 | + expected_num_dates = np.array([1, 2, 3]) |
| 761 | + dates = decode_cf_datetime(expected_num_dates, expected_units, calendar) |
| 762 | + |
| 763 | + # Check that dates were decoded to UTC; here the hours should all |
| 764 | + # equal 5. |
| 765 | + result_hours = DataArray(dates).dt.hour |
| 766 | + expected_hours = DataArray([5, 5, 5]) |
| 767 | + assert_equal(result_hours, expected_hours) |
| 768 | + |
| 769 | + # Check that the encoded values are accurately roundtripped. |
| 770 | + result_num_dates, result_units, result_calendar = encode_cf_datetime( |
| 771 | + dates, expected_units, calendar) |
| 772 | + |
| 773 | + if calendar in _STANDARD_CALENDARS: |
| 774 | + np.testing.assert_array_equal(result_num_dates, expected_num_dates) |
| 775 | + else: |
| 776 | + # cftime datetime arithmetic is not quite exact. |
| 777 | + np.testing.assert_allclose(result_num_dates, expected_num_dates) |
| 778 | + |
| 779 | + assert result_units == expected_units |
| 780 | + assert result_calendar == calendar |
0 commit comments