From 107bf96a8b54058179cf18e03a337e4e332b9387 Mon Sep 17 00:00:00 2001 From: Zebedee Nicholls Date: Tue, 13 Oct 2020 16:37:33 +1100 Subject: [PATCH 01/15] Add test of time passing even if '1-1-1' in units --- xarray/tests/test_coding_times.py | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/xarray/tests/test_coding_times.py b/xarray/tests/test_coding_times.py index 5b2d63525fa..86d4c114d88 100644 --- a/xarray/tests/test_coding_times.py +++ b/xarray/tests/test_coding_times.py @@ -54,6 +54,7 @@ ([[0]], "days since 1000-01-01"), (np.arange(2), "days since 1000-01-01"), (np.arange(0, 100000, 20000), "days since 1900-01-01"), + (np.arange(0, 100000, 20000), "days since 1-01-01"), (17093352.0, "hours since 1-1-1 00:00:0.0"), ([0.5, 1.5], "hours since 1900-01-01T00:00:00"), (0, "milliseconds since 2000-01-01T00:00:00"), @@ -109,20 +110,16 @@ def test_cf_datetime(num_dates, units, calendar): # https://github.com/Unidata/netcdf4-python/issues/355 assert (abs_diff <= np.timedelta64(1, "s")).all() encoded, _, _ = coding.times.encode_cf_datetime(actual, units, calendar) - if "1-1-1" not in units: - # pandas parses this date very strangely, so the original - # units/encoding cannot be preserved in this case: - # (Pdb) pd.to_datetime('1-1-1 00:00:0.0') - # Timestamp('2001-01-01 00:00:00') + + assert_array_equal(num_dates, np.around(encoded, 1)) + if hasattr(num_dates, "ndim") and num_dates.ndim == 1 and "1000" not in units: + # verify that wrapping with a pandas.Index works + # note that it *does not* currently work to even put + # non-datetime64 compatible dates into a pandas.Index + encoded, _, _ = coding.times.encode_cf_datetime( + pd.Index(actual), units, calendar + ) assert_array_equal(num_dates, np.around(encoded, 1)) - if hasattr(num_dates, "ndim") and num_dates.ndim == 1 and "1000" not in units: - # verify that wrapping with a pandas.Index works - # note that it *does not* currently work to even put - # non-datetime64 compatible dates into a pandas.Index - encoded, _, _ = coding.times.encode_cf_datetime( - pd.Index(actual), units, calendar - ) - assert_array_equal(num_dates, np.around(encoded, 1)) @requires_cftime From b2a0cd37315f95a994a9acfca7afc70207a9cd0f Mon Sep 17 00:00:00 2001 From: Zebedee Nicholls Date: Tue, 13 Oct 2020 16:37:41 +1100 Subject: [PATCH 02/15] Pass test --- xarray/coding/times.py | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/xarray/coding/times.py b/xarray/coding/times.py index 77b2d2c7937..793f6fe4e81 100644 --- a/xarray/coding/times.py +++ b/xarray/coding/times.py @@ -53,14 +53,38 @@ def _netcdf_to_numpy_timeunit(units): }[units] +def _ensure_padded_year(ref_date): + # Reference dates without a padded year (e.g. since 1-1-1 or since 2-3-4) + # are ambiguous (is it YMD or DMY). This can lead to some very odd + # behaviour e.g. pandas (via dateutil) passes '1-1-1 00:00:0.0' as + # '2001-01-01 00:00:00' (because it assumes a) DMY and b) that year 1 is + # shorthand for 2001 (like 02 would be shorthand for year 2002). + + # Here we ensure that there is always a four-digit year, with the + # assumption being that year comes first if we get something ambiguous. + matches_year = re.match(r".*\d{4}.*", ref_date) + if not matches_year: + # No four-digit strings, assume the first digits are the year and pad + # appropriately + # QUESTION: should we raise a warning or something here that we're doing this padding? + matches_start_digits = re.match(r"(\d+)(.*)", ref_date) + ref_year, everything_else = [s for s in matches_start_digits.groups()] + ref_date = "{:04d}{}".format(int(ref_year), everything_else) + + return ref_date + def _unpack_netcdf_time_units(units): # CF datetime units follow the format: "UNIT since DATE" # this parses out the unit and date allowing for extraneous - # whitespace. - matches = re.match("(.+) since (.+)", units) + # whitespace. It also ensures that the year is padded with zeros + # so it will be correctly understood by pandas (via dateutil). + matches = re.match(r"(.+) since (.+)", units) if not matches: raise ValueError("invalid time units: %s" % units) + delta_units, ref_date = [s.strip() for s in matches.groups()] + ref_date = _ensure_padded_year(ref_date) + return delta_units, ref_date From 9544b2f7265285fbfe079cb25b7ef754c399cfcb Mon Sep 17 00:00:00 2001 From: Zebedee Nicholls Date: Tue, 13 Oct 2020 16:40:38 +1100 Subject: [PATCH 03/15] Format --- xarray/coding/times.py | 1 + 1 file changed, 1 insertion(+) diff --git a/xarray/coding/times.py b/xarray/coding/times.py index 793f6fe4e81..7d47924a275 100644 --- a/xarray/coding/times.py +++ b/xarray/coding/times.py @@ -73,6 +73,7 @@ def _ensure_padded_year(ref_date): return ref_date + def _unpack_netcdf_time_units(units): # CF datetime units follow the format: "UNIT since DATE" # this parses out the unit and date allowing for extraneous From 4e20768e6f13d727e7ece28d794a5a19c36c8536 Mon Sep 17 00:00:00 2001 From: Zebedee Nicholls Date: Tue, 13 Oct 2020 16:43:46 +1100 Subject: [PATCH 04/15] Whatsnew --- doc/whats-new.rst | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index f6f087cce53..08e14200a5c 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -37,6 +37,8 @@ Bug fixes ~~~~~~~~~ - Fix :py:meth:`DataArray.plot.step`. By `Deepak Cherian `_. +- Fix bug where reference times without padded years (e.g. "since 1-1-1") would lose their units when being passed by :py:func:`encode_cf_datetime`. + By `Zebedee Nicholls `_. - Fix bug where reading a scalar value from a NetCDF file opened with the ``h5netcdf`` backend would raise a ``ValueError`` when ``decode_cf=True`` (:issue:`4471`, :pull:`4485`). By `Gerrit Holl `_. - Fix bug where datetime64 times are silently changed to incorrect values if they are outside the valid date range for ns precision when provided in some other units (:issue:`4427`, :pull:`4454`). @@ -79,7 +81,7 @@ v0.16.1 (2020-09-20) This patch release fixes an incompatibility with a recent pandas change, which was causing an issue indexing with a ``datetime64``. It also includes improvements to ``rolling``, ``to_dataframe``, ``cov`` & ``corr`` methods and -bug fixes. Our documentation has a number of improvements, including fixing all +bug fixes. Our documentation has a number of improvements, including fixing all doctests and confirming their accuracy on every commit. Many thanks to the 36 contributors who contributed to this release: @@ -159,7 +161,7 @@ Bug fixes By `Jens Svensmark `_ - Fix incorrect legend labels for :py:meth:`Dataset.plot.scatter` (:issue:`4126`). By `Peter Hausamann `_. -- Fix ``dask.optimize`` on ``DataArray`` producing an invalid Dask task graph (:issue:`3698`) +- Fix ``dask.optimize`` on ``DataArray`` producing an invalid Dask task graph (:issue:`3698`) By `Tom Augspurger `_ - Fix ``pip install .`` when no ``.git`` directory exists; namely when the xarray source directory has been rsync'ed by PyCharm Professional for a remote deployment over SSH. From bf545092873a4731d5e96308f227271f2fc117ae Mon Sep 17 00:00:00 2001 From: Zebedee Nicholls Date: Tue, 13 Oct 2020 17:34:22 +1100 Subject: [PATCH 05/15] Shorten name --- doc/whats-new.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 08e14200a5c..9533d6854f5 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -38,7 +38,7 @@ Bug fixes - Fix :py:meth:`DataArray.plot.step`. By `Deepak Cherian `_. - Fix bug where reference times without padded years (e.g. "since 1-1-1") would lose their units when being passed by :py:func:`encode_cf_datetime`. - By `Zebedee Nicholls `_. + By `Zeb Nicholls `_. - Fix bug where reading a scalar value from a NetCDF file opened with the ``h5netcdf`` backend would raise a ``ValueError`` when ``decode_cf=True`` (:issue:`4471`, :pull:`4485`). By `Gerrit Holl `_. - Fix bug where datetime64 times are silently changed to incorrect values if they are outside the valid date range for ns precision when provided in some other units (:issue:`4427`, :pull:`4454`). From 3352b4ee626c881b062c73ed79cd64068810e7d0 Mon Sep 17 00:00:00 2001 From: Zebedee Nicholls Date: Tue, 13 Oct 2020 17:35:18 +1100 Subject: [PATCH 06/15] Update whatsnew with pull info --- doc/whats-new.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 9533d6854f5..ed1c093fea1 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -38,6 +38,7 @@ Bug fixes - Fix :py:meth:`DataArray.plot.step`. By `Deepak Cherian `_. - Fix bug where reference times without padded years (e.g. "since 1-1-1") would lose their units when being passed by :py:func:`encode_cf_datetime`. +- Fix bug where reference times without padded years (e.g. "since 1-1-1") would lose their units when being passed by :py:func:`encode_cf_datetime` (:pull:`4506`). By `Zeb Nicholls `_. - Fix bug where reading a scalar value from a NetCDF file opened with the ``h5netcdf`` backend would raise a ``ValueError`` when ``decode_cf=True`` (:issue:`4471`, :pull:`4485`). By `Gerrit Holl `_. From e9327dced41c5f198023ceeafe528f9a52b95ec5 Mon Sep 17 00:00:00 2001 From: Zeb Nicholls Date: Wed, 14 Oct 2020 22:27:49 +1100 Subject: [PATCH 07/15] Clarify comment --- xarray/coding/times.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xarray/coding/times.py b/xarray/coding/times.py index 7d47924a275..6e6253ff51f 100644 --- a/xarray/coding/times.py +++ b/xarray/coding/times.py @@ -55,10 +55,10 @@ def _netcdf_to_numpy_timeunit(units): def _ensure_padded_year(ref_date): # Reference dates without a padded year (e.g. since 1-1-1 or since 2-3-4) - # are ambiguous (is it YMD or DMY). This can lead to some very odd + # are ambiguous (is it YMD or DMY?). This can lead to some very odd # behaviour e.g. pandas (via dateutil) passes '1-1-1 00:00:0.0' as # '2001-01-01 00:00:00' (because it assumes a) DMY and b) that year 1 is - # shorthand for 2001 (like 02 would be shorthand for year 2002). + # shorthand for 2001 (like 02 would be shorthand for year 2002)). # Here we ensure that there is always a four-digit year, with the # assumption being that year comes first if we get something ambiguous. From 1c81591b1d6b9b916c0ff9c89e20f91ee7c398e4 Mon Sep 17 00:00:00 2001 From: Zeb Nicholls Date: Tue, 20 Oct 2020 08:53:49 +1100 Subject: [PATCH 08/15] Update doc/whats-new.rst Co-authored-by: Spencer Clark --- doc/whats-new.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index ed1c093fea1..da3e3895178 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -39,6 +39,7 @@ Bug fixes - Fix :py:meth:`DataArray.plot.step`. By `Deepak Cherian `_. - Fix bug where reference times without padded years (e.g. "since 1-1-1") would lose their units when being passed by :py:func:`encode_cf_datetime`. - Fix bug where reference times without padded years (e.g. "since 1-1-1") would lose their units when being passed by :py:func:`encode_cf_datetime` (:pull:`4506`). +- Fix bug where reference times without padded years (e.g. "since 1-1-1") would lose their units when being passed by :py:func:`encode_cf_datetime` (:issue:`4422`, :pull:`4506`). By `Zeb Nicholls `_. - Fix bug where reading a scalar value from a NetCDF file opened with the ``h5netcdf`` backend would raise a ``ValueError`` when ``decode_cf=True`` (:issue:`4471`, :pull:`4485`). By `Gerrit Holl `_. From 8a8a840625267efb3931a583118e7e601c2e4436 Mon Sep 17 00:00:00 2001 From: Zebedee Nicholls Date: Tue, 20 Oct 2020 08:57:52 +1100 Subject: [PATCH 09/15] Add extra note to whatsnew thanks to suggestion from @spencerkclark --- doc/whats-new.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index da3e3895178..a69af87f52e 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -40,6 +40,7 @@ Bug fixes - Fix bug where reference times without padded years (e.g. "since 1-1-1") would lose their units when being passed by :py:func:`encode_cf_datetime`. - Fix bug where reference times without padded years (e.g. "since 1-1-1") would lose their units when being passed by :py:func:`encode_cf_datetime` (:pull:`4506`). - Fix bug where reference times without padded years (e.g. "since 1-1-1") would lose their units when being passed by :py:func:`encode_cf_datetime` (:issue:`4422`, :pull:`4506`). +- Fix bug where reference times without padded years (e.g. "since 1-1-1") would lose their units when being passed by :py:func:`encode_cf_datetime` (:issue:`4422`, :pull:`4506`). Previously, without ``cftime``, such times would be silently parsed incorrectly (at least based on the CF conventions) e.g. "since 1-1-1" would be passed (via ``dateutil``) to "since 2001-1-1". By `Zeb Nicholls `_. - Fix bug where reading a scalar value from a NetCDF file opened with the ``h5netcdf`` backend would raise a ``ValueError`` when ``decode_cf=True`` (:issue:`4471`, :pull:`4485`). By `Gerrit Holl `_. From ea0e92342a5b7fc28fa851ef8d72533d060b57bf Mon Sep 17 00:00:00 2001 From: Zebedee Nicholls Date: Tue, 20 Oct 2020 09:09:49 +1100 Subject: [PATCH 10/15] Add failing test of warning --- xarray/tests/test_coding_times.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/xarray/tests/test_coding_times.py b/xarray/tests/test_coding_times.py index 86d4c114d88..ff931abbbc8 100644 --- a/xarray/tests/test_coding_times.py +++ b/xarray/tests/test_coding_times.py @@ -925,3 +925,22 @@ def test_use_cftime_false_non_standard_calendar(calendar, units_year): units = f"days since {units_year}-01-01" with pytest.raises(OutOfBoundsDatetime): decode_cf_datetime(numerical_dates, units, calendar, use_cftime=False) + + +@requires_cftime +@pytest.mark.parametrize("calendar", _ALL_CALENDARS) +def test_decode_ambiguous_time_warns(calendar): + # GH 4422, 4506 + from cftime import num2date + + dates = [1, 2, 3] + units = "days since 1-1-1" + expected = num2date( + dates, units, calendar=calendar, only_use_cftime_datetimes=True + ) + with pytest.warns(None) as record: + result = decode_cf_datetime(dates, units, calendar=calendar) + + np.testing.assert_array_equal(result, expected) + + assert len(record) == 1 From d9240d4bce20184bd7bd815f7d7841e979b78c1d Mon Sep 17 00:00:00 2001 From: Zebedee Nicholls Date: Tue, 20 Oct 2020 09:30:49 +1100 Subject: [PATCH 11/15] Pass test of warning when padding --- xarray/coding/times.py | 29 ++++++++++++++++++++--------- xarray/tests/test_coding_times.py | 21 ++++++++++++++++++--- 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/xarray/coding/times.py b/xarray/coding/times.py index 6e6253ff51f..0c646932936 100644 --- a/xarray/coding/times.py +++ b/xarray/coding/times.py @@ -63,15 +63,26 @@ def _ensure_padded_year(ref_date): # Here we ensure that there is always a four-digit year, with the # assumption being that year comes first if we get something ambiguous. matches_year = re.match(r".*\d{4}.*", ref_date) - if not matches_year: - # No four-digit strings, assume the first digits are the year and pad - # appropriately - # QUESTION: should we raise a warning or something here that we're doing this padding? - matches_start_digits = re.match(r"(\d+)(.*)", ref_date) - ref_year, everything_else = [s for s in matches_start_digits.groups()] - ref_date = "{:04d}{}".format(int(ref_year), everything_else) - - return ref_date + if matches_year: + # all good, return + return ref_date + + # No four-digit strings, assume the first digits are the year and pad + # appropriately + matches_start_digits = re.match(r"(\d+)(.*)", ref_date) + ref_year, everything_else = [s for s in matches_start_digits.groups()] + ref_date_padded = "{:04d}{}".format(int(ref_year), everything_else) + + warning_msg = ( + f"Ambiguous reference date string: {ref_date}. The first value is " + "assumed to be the year hence will be padded with zeros to remove " + f"the ambiguity (the padded reference date string is: {ref_date_padded}. " + "To remove this message, remove the ambiguity by padding your reference " + "date strings with zeros." + ) + warnings.warn(warning_msg, SerializationWarning) + + return ref_date_padded def _unpack_netcdf_time_units(units): diff --git a/xarray/tests/test_coding_times.py b/xarray/tests/test_coding_times.py index ff931abbbc8..128ce1c82c0 100644 --- a/xarray/tests/test_coding_times.py +++ b/xarray/tests/test_coding_times.py @@ -933,14 +933,29 @@ def test_decode_ambiguous_time_warns(calendar): # GH 4422, 4506 from cftime import num2date + # we don't even attempt to decode non-standard calendards with + # pandas so expect no warning will be emitted + standard_calendar = calendar in coding.times._STANDARD_CALENDARS + dates = [1, 2, 3] units = "days since 1-1-1" expected = num2date( dates, units, calendar=calendar, only_use_cftime_datetimes=True ) - with pytest.warns(None) as record: + + + exp_warn_type = SerializationWarning if standard_calendar else None + + with pytest.warns(exp_warn_type) as record: result = decode_cf_datetime(dates, units, calendar=calendar) - np.testing.assert_array_equal(result, expected) + if standard_calendar: + relevant_warnings = [ + r for r in record.list + if str(r.message).startswith("Ambiguous reference date string: 1-1-1") + ] + assert len(relevant_warnings) == 1 + else: + assert not record - assert len(record) == 1 + np.testing.assert_array_equal(result, expected) From 08f2b89dcd3f20130252524f6f325928cb9673af Mon Sep 17 00:00:00 2001 From: Zebedee Nicholls Date: Tue, 20 Oct 2020 09:33:20 +1100 Subject: [PATCH 12/15] Cleanup after rebase --- doc/whats-new.rst | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index a69af87f52e..d59dac69555 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -36,12 +36,9 @@ New Features Bug fixes ~~~~~~~~~ -- Fix :py:meth:`DataArray.plot.step`. By `Deepak Cherian `_. -- Fix bug where reference times without padded years (e.g. "since 1-1-1") would lose their units when being passed by :py:func:`encode_cf_datetime`. -- Fix bug where reference times without padded years (e.g. "since 1-1-1") would lose their units when being passed by :py:func:`encode_cf_datetime` (:pull:`4506`). -- Fix bug where reference times without padded years (e.g. "since 1-1-1") would lose their units when being passed by :py:func:`encode_cf_datetime` (:issue:`4422`, :pull:`4506`). - Fix bug where reference times without padded years (e.g. "since 1-1-1") would lose their units when being passed by :py:func:`encode_cf_datetime` (:issue:`4422`, :pull:`4506`). Previously, without ``cftime``, such times would be silently parsed incorrectly (at least based on the CF conventions) e.g. "since 1-1-1" would be passed (via ``dateutil``) to "since 2001-1-1". By `Zeb Nicholls `_. +- Fix :py:meth:`DataArray.plot.step`. By `Deepak Cherian `_. - Fix bug where reading a scalar value from a NetCDF file opened with the ``h5netcdf`` backend would raise a ``ValueError`` when ``decode_cf=True`` (:issue:`4471`, :pull:`4485`). By `Gerrit Holl `_. - Fix bug where datetime64 times are silently changed to incorrect values if they are outside the valid date range for ns precision when provided in some other units (:issue:`4427`, :pull:`4454`). From 14c49a6597eff14de077af78f9bc0d265385dd25 Mon Sep 17 00:00:00 2001 From: Zebedee Nicholls Date: Tue, 20 Oct 2020 09:35:50 +1100 Subject: [PATCH 13/15] Format --- xarray/tests/test_coding_times.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/xarray/tests/test_coding_times.py b/xarray/tests/test_coding_times.py index 128ce1c82c0..5e5aec21e27 100644 --- a/xarray/tests/test_coding_times.py +++ b/xarray/tests/test_coding_times.py @@ -939,10 +939,7 @@ def test_decode_ambiguous_time_warns(calendar): dates = [1, 2, 3] units = "days since 1-1-1" - expected = num2date( - dates, units, calendar=calendar, only_use_cftime_datetimes=True - ) - + expected = num2date(dates, units, calendar=calendar, only_use_cftime_datetimes=True) exp_warn_type = SerializationWarning if standard_calendar else None @@ -951,7 +948,8 @@ def test_decode_ambiguous_time_warns(calendar): if standard_calendar: relevant_warnings = [ - r for r in record.list + r + for r in record.list if str(r.message).startswith("Ambiguous reference date string: 1-1-1") ] assert len(relevant_warnings) == 1 From 2769554e0bc1ae97a6aaf75e168a3d34d7df1275 Mon Sep 17 00:00:00 2001 From: Zebedee Nicholls Date: Tue, 20 Oct 2020 09:40:13 +1100 Subject: [PATCH 14/15] Cleanup whatsnew --- doc/whats-new.rst | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index d59dac69555..17bf40b1b9d 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -36,7 +36,13 @@ New Features Bug fixes ~~~~~~~~~ -- Fix bug where reference times without padded years (e.g. "since 1-1-1") would lose their units when being passed by :py:func:`encode_cf_datetime` (:issue:`4422`, :pull:`4506`). Previously, without ``cftime``, such times would be silently parsed incorrectly (at least based on the CF conventions) e.g. "since 1-1-1" would be passed (via ``dateutil``) to "since 2001-1-1". +- Fix bug where reference times without padded years (e.g. "since 1-1-1") would lose their units when + being passed by :py:func:`encode_cf_datetime` (:issue:`4422`, :pull:`4506`). Such units are ambiguous + about which digit represents the years (is it YMD or DMY?). Now, if such formatting is encountered, + it is assumed that the first digit is the years, they are padded appropriately (to e.g. "since 0001-1-1") + and a warning that this assumption is being made is issued. Previously, without ``cftime``, such times + would be silently parsed incorrectly (at least based on the CF conventions) e.g. "since 1-1-1" would + be passed (via``pandas`` and ``dateutil``) to "since 2001-1-1". By `Zeb Nicholls `_. - Fix :py:meth:`DataArray.plot.step`. By `Deepak Cherian `_. - Fix bug where reading a scalar value from a NetCDF file opened with the ``h5netcdf`` backend would raise a ``ValueError`` when ``decode_cf=True`` (:issue:`4471`, :pull:`4485`). From d80d93d9b92f34bfd6a7644b391cc363a218cf2c Mon Sep 17 00:00:00 2001 From: Zeb Nicholls Date: Tue, 20 Oct 2020 10:55:47 +1100 Subject: [PATCH 15/15] Apply suggestions from code review by @mathause Co-authored-by: Mathias Hauser --- doc/whats-new.rst | 2 +- xarray/coding/times.py | 4 ++-- xarray/tests/test_coding_times.py | 10 +++++----- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 17bf40b1b9d..c113b3c59cc 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -42,7 +42,7 @@ Bug fixes it is assumed that the first digit is the years, they are padded appropriately (to e.g. "since 0001-1-1") and a warning that this assumption is being made is issued. Previously, without ``cftime``, such times would be silently parsed incorrectly (at least based on the CF conventions) e.g. "since 1-1-1" would - be passed (via``pandas`` and ``dateutil``) to "since 2001-1-1". + be parsed (via``pandas`` and ``dateutil``) to "since 2001-1-1". By `Zeb Nicholls `_. - Fix :py:meth:`DataArray.plot.step`. By `Deepak Cherian `_. - Fix bug where reading a scalar value from a NetCDF file opened with the ``h5netcdf`` backend would raise a ``ValueError`` when ``decode_cf=True`` (:issue:`4471`, :pull:`4485`). diff --git a/xarray/coding/times.py b/xarray/coding/times.py index 0c646932936..59f8b89743a 100644 --- a/xarray/coding/times.py +++ b/xarray/coding/times.py @@ -76,7 +76,7 @@ def _ensure_padded_year(ref_date): warning_msg = ( f"Ambiguous reference date string: {ref_date}. The first value is " "assumed to be the year hence will be padded with zeros to remove " - f"the ambiguity (the padded reference date string is: {ref_date_padded}. " + f"the ambiguity (the padded reference date string is: {ref_date_padded}). " "To remove this message, remove the ambiguity by padding your reference " "date strings with zeros." ) @@ -92,7 +92,7 @@ def _unpack_netcdf_time_units(units): # so it will be correctly understood by pandas (via dateutil). matches = re.match(r"(.+) since (.+)", units) if not matches: - raise ValueError("invalid time units: %s" % units) + raise ValueError(f"invalid time units: {units}") delta_units, ref_date = [s.strip() for s in matches.groups()] ref_date = _ensure_padded_year(ref_date) diff --git a/xarray/tests/test_coding_times.py b/xarray/tests/test_coding_times.py index 5e5aec21e27..e3d68355ef3 100644 --- a/xarray/tests/test_coding_times.py +++ b/xarray/tests/test_coding_times.py @@ -114,7 +114,7 @@ def test_cf_datetime(num_dates, units, calendar): assert_array_equal(num_dates, np.around(encoded, 1)) if hasattr(num_dates, "ndim") and num_dates.ndim == 1 and "1000" not in units: # verify that wrapping with a pandas.Index works - # note that it *does not* currently work to even put + # note that it *does not* currently work to put # non-datetime64 compatible dates into a pandas.Index encoded, _, _ = coding.times.encode_cf_datetime( pd.Index(actual), units, calendar @@ -933,20 +933,20 @@ def test_decode_ambiguous_time_warns(calendar): # GH 4422, 4506 from cftime import num2date - # we don't even attempt to decode non-standard calendards with + # we don't decode non-standard calendards with # pandas so expect no warning will be emitted - standard_calendar = calendar in coding.times._STANDARD_CALENDARS + is_standard_calendar = calendar in coding.times._STANDARD_CALENDARS dates = [1, 2, 3] units = "days since 1-1-1" expected = num2date(dates, units, calendar=calendar, only_use_cftime_datetimes=True) - exp_warn_type = SerializationWarning if standard_calendar else None + exp_warn_type = SerializationWarning if is_standard_calendar else None with pytest.warns(exp_warn_type) as record: result = decode_cf_datetime(dates, units, calendar=calendar) - if standard_calendar: + if is_standard_calendar: relevant_warnings = [ r for r in record.list