Skip to content

Commit

Permalink
Convert ref_date to UTC in encode_cf_datetime (#2651)
Browse files Browse the repository at this point in the history
* Convert ref_date to UTC in encode_cf_datetime

* Only convert ref_date if it is not timezone-naive
  • Loading branch information
spencerkclark authored and shoyer committed Jan 24, 2019
1 parent 3cadf4e commit 8d37dc3
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
31 changes: 31 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,37 @@ What's New
import xarray as xr
np.random.seed(123456)
.. _whats-new.0.11.3:

v0.11.3 (unreleased)
--------------------

Breaking changes
~~~~~~~~~~~~~~~~

- Remove support for Python 2. This is the first version of xarray that is
Python 3 only. (:issue:`1876`).
By `Joe Hamman <https://github.com/jhamman>`_.

Enhancements
~~~~~~~~~~~~

- Upsampling an array via interpolation with resample is now dask-compatible,
as long as the array is not chunked along the resampling dimension.
By `Spencer Clark <https://github.com/spencerkclark>`_.

Bug fixes
~~~~~~~~~

- Interpolating via resample now internally specifies ``bounds_error=False``
as an argument to ``scipy.interpolate.interp1d``, allowing for interpolation
from higher frequencies to lower frequencies. Datapoints outside the bounds
of the original time coordinate are now filled with NaN (:issue:`2197`). By
`Spencer Clark <https://github.com/spencerkclark>`_.
- Saving files with times encoded with reference dates with timezones
(e.g. '2000-01-01T00:00:00-05:00') no longer raises an error
(:issue:`2649`). By `Spencer Clark <https://github.com/spencerkclark>`_.

.. _whats-new.0.11.2:

v0.11.2 (2 January 2019)
Expand Down
5 changes: 5 additions & 0 deletions xarray/coding/times.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,11 @@ def encode_cf_datetime(dates, units=None, calendar=None):
time_delta = np.timedelta64(1, delta_units).astype('timedelta64[ns]')
ref_date = pd.Timestamp(ref_date)

# If the ref_date Timestamp is timezone-aware, convert to UTC and
# make it timezone-naive (GH 2649).
if ref_date.tz is not None:
ref_date = ref_date.tz_convert(None)

# Wrap the dates in a DatetimeIndex to do the subtraction to ensure
# an OverflowError is raised if the ref_date is too far away from
# dates to be encoded (GH 2272).
Expand Down
16 changes: 16 additions & 0 deletions xarray/tests/test_coding_times.py
Original file line number Diff line number Diff line change
Expand Up @@ -750,3 +750,19 @@ def test_encode_cf_datetime_pandas_min():
np.testing.assert_array_equal(num, expected_num)
assert units == expected_units
assert calendar == expected_calendar


def test_encode_cf_datetime_units_with_tz():
# Regression test for GH 2649
units = 'days since 2000-01-01T00:00:00-05:00'
calendar = 'proleptic_gregorian'
dates = pd.date_range('2000', periods=3, tz='US/Eastern').values
num, units, calendar = encode_cf_datetime(dates,
units=units,
calendar=calendar)
expected_num = np.array([0, 1, 2])
expected_units = 'days since 2000-01-01T00:00:00-05:00'
expected_calendar = 'proleptic_gregorian'
np.testing.assert_array_equal(num, expected_num)
assert units == expected_units
assert calendar == expected_calendar

0 comments on commit 8d37dc3

Please sign in to comment.