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

Type preservation #164

Merged
merged 4 commits into from
Jun 15, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 2 additions & 4 deletions cf_units/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1738,10 +1738,8 @@ def convert(self, value, other, ctype=FLOAT64, inplace=False):
result, self.cftime_unit, self.calendar)
result = cftime.date2num(
result_datetimes, other.cftime_unit, other.calendar)
# Preserve the datatype of the input array if it was float32.
if (isinstance(value, np.ndarray) and
value.dtype == np.float32):
result = result.astype(np.float32)
if isinstance(value, np.ndarray) and np.issubsctype(value.dtype, np.floating):
result = result.astype(value.dtype)
else:
try:
ut_converter = _ud.get_converter(self.ut_unit,
Expand Down
13 changes: 7 additions & 6 deletions cf_units/tests/unit/unit/test_Unit.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# (C) British Crown Copyright 2015 - 2020, Met Office
# (C) British Crown Copyright 2015 - 2021, Met Office
#
# This file is part of cf-units.
#
Expand Down Expand Up @@ -75,11 +75,12 @@ def test_gregorian_calendar_conversion_shape(self):
self.assertEqual(expected.shape, result.shape)

def test_non_gregorian_calendar_conversion_dtype(self):
data = np.arange(4, dtype=np.float32)
u1 = Unit('hours since 2000-01-01 00:00:00', calendar='360_day')
u2 = Unit('hours since 2000-01-02 00:00:00', calendar='360_day')
result = u1.convert(data, u2)
self.assertEqual(result.dtype, np.float32)
for start_dtype in [np.float32, np.float64]:
data = np.arange(4, dtype=start_dtype)
u1 = Unit('hours since 2000-01-01 00:00:00', calendar='360_day')
u2 = Unit('hours since 2000-01-02 00:00:00', calendar='360_day')
result = u1.convert(data, u2)
self.assertEqual(result.dtype, start_dtype)


class Test_convert__endianness_time(unittest.TestCase):
Expand Down