Skip to content

Commit a9b2e42

Browse files
committed
Let Time.masked reflect whether _time.jd1 is Masked
1 parent 609df0c commit a9b2e42

3 files changed

Lines changed: 86 additions & 53 deletions

File tree

‎astropy/time/core.py‎

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -895,26 +895,29 @@ def shape(self, shape):
895895

896896
def _shaped_like_input(self, value):
897897
if self.masked:
898-
# Create new instance even when Masked already, to guarantee
899-
# the right masked array type, and that the mask is copied.
900-
return conf._masked_cls(value, mask=self.mask.copy())
898+
# Ensure the mask is independent.
899+
value = conf._masked_cls(value, mask=self.mask.copy())
900+
# For new-style, we do not treat masked scalars differently from arrays.
901+
if isinstance(value, Masked):
902+
return value
901903

902904
if self._time.jd1.shape:
903905
if isinstance(value, np.ndarray):
904-
if isinstance(value, Masked):
905-
return value.unmasked
906-
else:
907-
return value
906+
return value
908907
else:
909908
raise TypeError(
910909
f"JD is an array ({self._time.jd1!r}) but value is not ({value!r})"
911910
)
912911
else:
913-
# zero-dimensional array, is it safe to unbox?
912+
# zero-dimensional array, is it safe to unbox? The tricky comparison
913+
# of the mask is for the case that value is structured; otherwise, we
914+
# could just use np.ma.is_masked(value).
914915
if (
915916
isinstance(value, np.ndarray)
916917
and not value.shape
917-
and not np.ma.is_masked(value)
918+
and (
919+
(mask := getattr(value, "mask", np.False_)) == np.zeros_like(mask)
920+
).all()
918921
):
919922
if value.dtype.kind == "M":
920923
# existing test doesn't want datetime64 converted
@@ -1049,11 +1052,7 @@ def mask(self):
10491052

10501053
@property
10511054
def masked(self):
1052-
if "masked" not in self.cache:
1053-
self.cache["masked"] = bool(
1054-
(getattr(self._time.jd2, "mask", np.False_)).any()
1055-
)
1056-
return self.cache["masked"]
1055+
return isinstance(self._time.jd1, Masked)
10571056

10581057
def insert(self, obj, values, axis=0):
10591058
"""

‎astropy/time/tests/test_basic.py‎

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2313,24 +2313,7 @@ def test_datetime64_no_format():
23132313

23142314

23152315
def test_hash_time():
2316-
loc1 = EarthLocation(1 * u.m, 2 * u.m, 3 * u.m)
2317-
for loc in None, loc1:
2318-
t = Time([1, 1, 2, 3], format="cxcsec", location=loc)
2319-
t[3] = np.ma.masked
2320-
h1 = hash(t[0])
2321-
h2 = hash(t[1])
2322-
h3 = hash(t[2])
2323-
assert h1 == h2
2324-
assert h1 != h3
2325-
2326-
with pytest.raises(TypeError) as exc:
2327-
hash(t)
2328-
assert exc.value.args[0] == "unhashable type: 'Time' (must be scalar)"
2329-
2330-
with pytest.raises(TypeError) as exc:
2331-
hash(t[3])
2332-
assert exc.value.args[0] == "unhashable type: 'Time' (value is masked)"
2333-
2316+
loc = EarthLocation(1 * u.m, 2 * u.m, 3 * u.m)
23342317
t = Time(1, format="cxcsec", location=loc)
23352318
t2 = Time(1, format="cxcsec")
23362319

@@ -2342,22 +2325,53 @@ def test_hash_time():
23422325
assert hash(t) != hash(t2)
23432326

23442327

2345-
def test_hash_time_delta():
2328+
@pytest.mark.parametrize("location", [None, EarthLocation(1 * u.m, 2 * u.m, 3 * u.m)])
2329+
@pytest.mark.parametrize("masked_array_type", ["numpy", "astropy"])
2330+
def test_hash_masked_time(location, masked_array_type):
2331+
t = Time([1, 1, 2, 3], format="cxcsec", location=location)
2332+
t[3] = np.ma.masked
2333+
with conf.set_temp("masked_array_type", masked_array_type):
2334+
if masked_array_type == "numpy":
2335+
h1 = hash(t[0])
2336+
h2 = hash(t[1])
2337+
h3 = hash(t[2])
2338+
assert h1 == h2
2339+
assert h1 != h3
2340+
else:
2341+
with pytest.raises(TypeError, match="value is masked"):
2342+
hash(t[0])
2343+
2344+
with pytest.raises(
2345+
TypeError, match=r"unhashable type: 'Time' \(must be scalar\)"
2346+
):
2347+
hash(t)
2348+
2349+
with pytest.raises(
2350+
TypeError, match=r"unhashable type: 'Time' \(value is masked\)"
2351+
):
2352+
hash(t[3])
2353+
2354+
2355+
@pytest.mark.parametrize("masked_array_type", ["numpy", "astropy"])
2356+
def test_hash_time_delta_masked(masked_array_type):
23462357
t = TimeDelta([1, 1, 2, 3], format="sec")
23472358
t[3] = np.ma.masked
2348-
h1 = hash(t[0])
2349-
h2 = hash(t[1])
2350-
h3 = hash(t[2])
2351-
assert h1 == h2
2352-
assert h1 != h3
2353-
2354-
with pytest.raises(TypeError) as exc:
2355-
hash(t)
2356-
assert exc.value.args[0] == "unhashable type: 'TimeDelta' (must be scalar)"
2357-
2358-
with pytest.raises(TypeError) as exc:
2359-
hash(t[3])
2360-
assert exc.value.args[0] == "unhashable type: 'TimeDelta' (value is masked)"
2359+
with conf.set_temp("masked_array_type", masked_array_type):
2360+
if masked_array_type == "numpy":
2361+
h1 = hash(t[0])
2362+
h2 = hash(t[1])
2363+
h3 = hash(t[2])
2364+
assert h1 == h2
2365+
assert h1 != h3
2366+
else:
2367+
with pytest.raises(TypeError, match=r"'TimeDelta' \(value is masked\)"):
2368+
hash(t[0])
2369+
2370+
with pytest.raises(TypeError, match=r"'TimeDelta' \(must be scalar\)"):
2371+
hash(t)
2372+
2373+
with pytest.raises(TypeError, match=r"'TimeDelta' \(value is masked\)"):
2374+
hash(t[3])
23612375

23622376

23632377
def test_get_time_fmt_exception_messages():

‎astropy/time/tests/test_mask.py‎

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from astropy.table import Table
1010
from astropy.time import Time, conf
1111
from astropy.utils import iers
12-
from astropy.utils.compat import NUMPY_LT_1_26, PYTHON_LT_3_11
12+
from astropy.utils.compat import NUMPY_LT_1_25, NUMPY_LT_1_26, PYTHON_LT_3_11
1313
from astropy.utils.compat.optional_deps import HAS_H5PY
1414
from astropy.utils.masked import Masked
1515

@@ -51,10 +51,11 @@ def test_simple():
5151
# Can also unmask.
5252
t[2] = np.ma.nomask
5353
assert np.all(t.mask == [False, False, False])
54-
# Which means one can become unmasked again.
55-
assert not t.masked
56-
assert not hasattr(t.value, "mask")
57-
assert not hasattr(t.unix, "mask")
54+
# But since the internal data are still masked, the instance stays masked too,
55+
# as does any output.
56+
assert t.masked
57+
assert hasattr(t.value, "mask")
58+
assert hasattr(t.unix, "mask")
5859
# Combo just for completeness
5960
t[1:] = np.ma.masked
6061
t[1] = np.ma.nomask
@@ -116,10 +117,10 @@ def test_str():
116117
# the call to replace().
117118
assert repr(t.iso).replace(">U23", "<U23").splitlines() == expected
118119

119-
# Assign value to unmask
120+
# Assign value to unmask, though the instance stays masked.
120121
t[1] = "2000:111"
121122
assert str(t) == "['2000:001:00:00:00.000' '2000:111:00:00:00.000']"
122-
assert t.masked is False
123+
assert t.masked
123124

124125

125126
def test_transform():
@@ -286,6 +287,25 @@ def test_all_formats(format_, masked_cls, masked_array_type):
286287
expected = t_format
287288
assert np.all(tm_format == expected)
288289

290+
# Check masked scalar.
291+
tm0_format = getattr(tm[0], format_)
292+
assert isinstance(tm0_format, out_cls)
293+
if NUMPY_LT_1_25 and tm0_format.dtype.kind == "M":
294+
# Comparison bug in older numpy, just skip it.
295+
return
296+
comparison = tm0_format == tm_format[0]
297+
assert comparison.mask
298+
if out_cls is Masked:
299+
assert comparison.unmasked
300+
elif tm0_format.dtype.names:
301+
assert comparison.data
302+
else:
303+
assert comparison is np.ma.masked
304+
305+
# Check unmasked scalar.
306+
tm1_format = getattr(tm[1], format_)
307+
assert tm1_format == tm_format[1]
308+
289309
# Verify that configuration gets reset to "astropy".
290310
tm_format2 = getattr(tm, format_)
291311
assert isinstance(tm_format2, Masked)

0 commit comments

Comments
 (0)