Skip to content

Commit 8d37dc3

Browse files
spencerkclarkshoyer
authored andcommitted
Convert ref_date to UTC in encode_cf_datetime (#2651)
* Convert ref_date to UTC in encode_cf_datetime * Only convert ref_date if it is not timezone-naive
1 parent 3cadf4e commit 8d37dc3

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

doc/whats-new.rst

+31
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,37 @@ What's New
1313
import xarray as xr
1414
np.random.seed(123456)
1515
16+
.. _whats-new.0.11.3:
17+
18+
v0.11.3 (unreleased)
19+
--------------------
20+
21+
Breaking changes
22+
~~~~~~~~~~~~~~~~
23+
24+
- Remove support for Python 2. This is the first version of xarray that is
25+
Python 3 only. (:issue:`1876`).
26+
By `Joe Hamman <https://github.com/jhamman>`_.
27+
28+
Enhancements
29+
~~~~~~~~~~~~
30+
31+
- Upsampling an array via interpolation with resample is now dask-compatible,
32+
as long as the array is not chunked along the resampling dimension.
33+
By `Spencer Clark <https://github.com/spencerkclark>`_.
34+
35+
Bug fixes
36+
~~~~~~~~~
37+
38+
- Interpolating via resample now internally specifies ``bounds_error=False``
39+
as an argument to ``scipy.interpolate.interp1d``, allowing for interpolation
40+
from higher frequencies to lower frequencies. Datapoints outside the bounds
41+
of the original time coordinate are now filled with NaN (:issue:`2197`). By
42+
`Spencer Clark <https://github.com/spencerkclark>`_.
43+
- Saving files with times encoded with reference dates with timezones
44+
(e.g. '2000-01-01T00:00:00-05:00') no longer raises an error
45+
(:issue:`2649`). By `Spencer Clark <https://github.com/spencerkclark>`_.
46+
1647
.. _whats-new.0.11.2:
1748

1849
v0.11.2 (2 January 2019)

xarray/coding/times.py

+5
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,11 @@ def encode_cf_datetime(dates, units=None, calendar=None):
359359
time_delta = np.timedelta64(1, delta_units).astype('timedelta64[ns]')
360360
ref_date = pd.Timestamp(ref_date)
361361

362+
# If the ref_date Timestamp is timezone-aware, convert to UTC and
363+
# make it timezone-naive (GH 2649).
364+
if ref_date.tz is not None:
365+
ref_date = ref_date.tz_convert(None)
366+
362367
# Wrap the dates in a DatetimeIndex to do the subtraction to ensure
363368
# an OverflowError is raised if the ref_date is too far away from
364369
# dates to be encoded (GH 2272).

xarray/tests/test_coding_times.py

+16
Original file line numberDiff line numberDiff line change
@@ -750,3 +750,19 @@ def test_encode_cf_datetime_pandas_min():
750750
np.testing.assert_array_equal(num, expected_num)
751751
assert units == expected_units
752752
assert calendar == expected_calendar
753+
754+
755+
def test_encode_cf_datetime_units_with_tz():
756+
# 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+
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

0 commit comments

Comments
 (0)