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

Further improvements to datetime roundtripping #225

Merged
merged 6 commits into from
Jan 25, 2021
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
4 changes: 3 additions & 1 deletion .github/workflows/miniconda.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ jobs:
run: |
conda create --name TEST python=${{ matrix.python-version }} --file requirements.txt --file requirements-dev.txt
source activate TEST
CYTHON_COVERAGE=1 pip install -v -e . --no-deps --force-reinstall
# enabling coverage slows down the tests dramaticaly
#CYTHON_COVERAGE=1 pip install -v -e . --no-deps --force-reinstall
pip install -v -e . --no-deps --force-reinstall
conda info --all
conda list

Expand Down
7 changes: 7 additions & 0 deletions Changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
Unreleased
==========
* `cftime.date2num` will now always return an array of integers, if the units
and times allow. Previously this would only be true if the units were
'microseconds' (PR #225). In other circumstances, as before, `cftime.date2num`
will return an array of floats.

version 1.3.1 (release tag v1.3.1rel)
=====================================
* fix for issue #211 (PR #212) bug in masked array handling in date2num)
Expand Down
13 changes: 6 additions & 7 deletions src/cftime/_cftime.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ def date2num(dates,units,calendar=None):
raise ValueError("Unsupported time units provided, {!r}.".format(unit))
if unit in ["months", "month"] and calendar != "360_day":
raise ValueError("Units of months only valid for 360_day calendar.")
factor = UNIT_CONVERSION_FACTORS[unit]
unit_timedelta = timedelta(microseconds=UNIT_CONVERSION_FACTORS[unit])
can_use_python_basedatetime = _can_use_python_datetime(basedate,calendar)

if can_use_python_basedatetime and all_python_datetimes:
Expand All @@ -263,13 +263,12 @@ def date2num(dates,units,calendar=None):
times.append(None)
else:
td = date - basedate
if factor == 1.0:
# units are microseconds, use integer division
times.append(td // timedelta(microseconds=1) )
if td % unit_timedelta == timedelta(0):
# Explicitly cast result to np.int64 for Windows compatibility
quotient = np.int64(td // unit_timedelta)
times.append(quotient)
else:
#times.append( (td / timedelta(microseconds=1)) / factor )
# this appears to be faster.
times.append( (td.total_seconds()*1.e6) / factor )
times.append(td / unit_timedelta)
n += 1
if ismasked: # convert to masked array if input was masked array
times = np.array(times)
Expand Down
41 changes: 41 additions & 0 deletions test/test_cftime.py
Original file line number Diff line number Diff line change
Expand Up @@ -1888,5 +1888,46 @@ def test_num2date_integer_upcast_required():
np.testing.assert_equal(result, expected)


@pytest.mark.parametrize(
"encoding_units",
["microseconds", "milliseconds", "seconds", "minutes", "hours", "days"]
)
@pytest.mark.parametrize(
"freq",
[
timedelta(microseconds=1),
timedelta(microseconds=1000),
timedelta(seconds=1),
timedelta(minutes=1),
timedelta(hours=1),
timedelta(days=1)
],
ids=lambda x: f"{x!r}"
)
def test_date2num_num2date_roundtrip(encoding_units, freq, calendar):
date_type = _EXPECTED_DATE_TYPES[calendar]
lengthy_timedelta = timedelta(days=291000 * 360)
times = np.array(
[
date_type(1, 1, 1),
date_type(1, 1, 1) + lengthy_timedelta,
date_type(1, 1, 1) + lengthy_timedelta + freq
]
)
units = f"{encoding_units} since 0001-01-01"
encoded = date2num(times, units=units, calendar=calendar)
decoded = num2date(encoded, units=units, calendar=calendar)
encoding_units_as_timedelta = timedelta(microseconds=UNIT_CONVERSION_FACTORS[encoding_units])

if freq >= encoding_units_as_timedelta:
assert encoded.dtype == np.int64
np.testing.assert_equal(decoded, times)
else:
assert encoded.dtype == np.float64
tolerance = timedelta(microseconds=2000)
meets_tolerance = np.abs(decoded - times) <= tolerance
assert np.all(meets_tolerance)


if __name__ == '__main__':
unittest.main()