Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert ref_date to UTC in encode_cf_datetime #2651

Merged
merged 2 commits into from
Jan 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ Bug fixes
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>`_.
`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:

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