Skip to content

Commit 9073eac

Browse files
authored
fix 575 (#576)
1 parent a7f8e48 commit 9073eac

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

dascore/utils/time.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,11 @@ def _array_to_timedelta64(array: np.ndarray) -> np.datetime64:
203203

204204
assert np.isreal(array[0])
205205
nans = pd.isnull(array)
206-
array[nans] = 0
206+
# Need to make copy to 1) not change original array and 2) handle
207+
# immutable arrays. See #575.
208+
if np.any(nans):
209+
array = np.array(array)
210+
array[nans] = 0
207211
# inf/NaN complain, salience these types of warnings for this block.
208212
with np.errstate(divide="ignore", invalid="ignore"):
209213
# separate seconds and factions, convert fractions to ns precision,

tests/test_utils/test_time.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,19 @@ def test_unsupported_type(self):
173173
with pytest.raises(NotImplementedError):
174174
to_datetime64(Dummy())
175175

176+
def test_immutable_inputs(self):
177+
"""Ensure immutable array inputs work."""
178+
# See #575.
179+
rand = np.random.RandomState(42)
180+
array_no_nan = rand.random(100)
181+
array_nan = rand.random(100)
182+
array_nan[10] = np.nan
183+
184+
for array in [array_no_nan, array_nan]:
185+
array.setflags(write=False)
186+
time = to_datetime64(array_no_nan)
187+
assert np.issubdtype(time.dtype, "M8")
188+
176189

177190
class TestToTimeDelta64:
178191
"""Tests for creating timedeltas."""
@@ -301,6 +314,19 @@ def test_array_of_datetimes(self, random_patch):
301314
out = to_timedelta64(dt_array)
302315
assert np.all(out.astype(np.int64) == dt_array.astype(np.int64))
303316

317+
def test_immutable_inputs(self):
318+
"""Ensure immutable array inputs work."""
319+
# See #575.
320+
rand = np.random.RandomState(42)
321+
array_no_nan = rand.random(100)
322+
array_nan = rand.random(100)
323+
array_nan[10] = np.nan
324+
325+
for array in [array_no_nan, array_nan]:
326+
array.setflags(write=False)
327+
time = to_timedelta64(array_no_nan)
328+
assert np.issubdtype(time.dtype, "m8")
329+
304330

305331
class TestToInt:
306332
"""Tests for converting time-like types to ints, or passing through reals."""

0 commit comments

Comments
 (0)