Skip to content

Commit 5d5c4e0

Browse files
authored
Merge pull request astropy#13885 from mhvk/numpy-ge-1-20
Bump minimum numpy version to 1.20
2 parents 0139d64 + 9724037 commit 5d5c4e0

21 files changed

Lines changed: 89 additions & 241 deletions

astropy/coordinates/angles.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -391,17 +391,15 @@ def _wrap_at(self, wrap_angle):
391391
self_angle = self.view(np.ndarray)
392392
# Do the wrapping, but only if any angles need to be wrapped
393393
#
394-
# This invalid catch block is needed both for the floor division
395-
# and for the comparisons later on (latter not really needed
396-
# any more for >= 1.19 (NUMPY_LT_1_19), but former is).
394+
# Catch any invalid warnings from the floor division.
397395
with np.errstate(invalid='ignore'):
398396
wraps = (self_angle - wrap_angle_floor) // a360
399-
valid = np.isfinite(wraps) & (wraps != 0)
400-
if np.any(valid):
401-
self_angle -= wraps * a360
402-
# Rounding errors can cause problems.
403-
self_angle[self_angle >= wrap_angle] -= a360
404-
self_angle[self_angle < wrap_angle_floor] += a360
397+
valid = np.isfinite(wraps) & (wraps != 0)
398+
if np.any(valid):
399+
self_angle -= wraps * a360
400+
# Rounding errors can cause problems.
401+
self_angle[self_angle >= wrap_angle] -= a360
402+
self_angle[self_angle < wrap_angle_floor] += a360
405403

406404
def wrap_at(self, wrap_angle, inplace=False):
407405
"""
@@ -590,11 +588,8 @@ def _validate_angles(self, angles=None):
590588
else:
591589
limit = u.degree.to(angles.unit, 90.0)
592590

593-
# This invalid catch block can be removed when the minimum numpy
594-
# version is >= 1.19 (NUMPY_LT_1_19)
595-
with np.errstate(invalid='ignore'):
596-
invalid_angles = (np.any(angles.value < -limit) or
597-
np.any(angles.value > limit))
591+
invalid_angles = (np.any(angles.value < -limit)
592+
or np.any(angles.value > limit))
598593
if invalid_angles:
599594
raise ValueError('Latitude angle(s) must be within -90 deg <= angle <= 90 deg, '
600595
'got {}'.format(angles.to(u.degree)))

astropy/coordinates/distances.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,7 @@ def __new__(cls, value=None, unit=None, z=None, cosmology=None,
169169
cls, value, unit, dtype=dtype, copy=copy, order=order,
170170
subok=subok, ndmin=ndmin)
171171

172-
# This invalid catch block can be removed when the minimum numpy
173-
# version is >= 1.19 (NUMPY_LT_1_19)
174-
with np.errstate(invalid='ignore'):
175-
any_negative = np.any(distance.value < 0)
176-
177-
if not allow_negative and any_negative:
172+
if not allow_negative and np.any(distance.value < 0):
178173
raise ValueError("distance must be >= 0. Use the argument "
179174
"`allow_negative=True` to allow negative values.")
180175

astropy/coordinates/representation.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2129,13 +2129,10 @@ def __init__(self, phi, theta=None, r=None, differentials=None, copy=True):
21292129
# Note that _phi already holds our own copy if copy=True.
21302130
self._phi.wrap_at(360 * u.deg, inplace=True)
21312131

2132-
# This invalid catch block can be removed when the minimum numpy
2133-
# version is >= 1.19 (NUMPY_LT_1_19)
2134-
with np.errstate(invalid='ignore'):
2135-
if np.any(self._theta < 0.*u.deg) or np.any(self._theta > 180.*u.deg):
2136-
raise ValueError('Inclination angle(s) must be within '
2137-
'0 deg <= angle <= 180 deg, '
2138-
'got {}'.format(theta.to(u.degree)))
2132+
if np.any(self._theta < 0.*u.deg) or np.any(self._theta > 180.*u.deg):
2133+
raise ValueError('Inclination angle(s) must be within '
2134+
'0 deg <= angle <= 180 deg, '
2135+
'got {}'.format(theta.to(u.degree)))
21392136

21402137
if self._r.unit.physical_type == 'length':
21412138
self._r = self._r.view(Distance)

astropy/coordinates/tests/test_arrays.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from astropy.coordinates import FK4, FK5, ICRS, Angle, CartesianRepresentation, Galactic, SkyCoord
1010
from astropy.tests.helper import assert_quantity_allclose as assert_allclose
1111
from astropy.time import Time
12-
from astropy.utils.compat import NUMPY_LT_1_19, NUMPY_LT_1_24
12+
from astropy.utils.compat import NUMPY_LT_1_24
1313
from astropy.utils.exceptions import AstropyDeprecationWarning
1414

1515

@@ -42,13 +42,9 @@ def test_angle_arrays():
4242
with ExitStack() as stack:
4343
if NUMPY_LT_1_24:
4444
stack.enter_context(pytest.raises(TypeError))
45-
# Arrays where the elements are Angle objects are not supported -- it's
46-
# really tricky to do correctly, if at all, due to the possibility of
47-
# nesting.
48-
if not NUMPY_LT_1_19:
49-
stack.enter_context(
50-
pytest.warns(DeprecationWarning,
51-
match='automatic object dtype is deprecated'))
45+
stack.enter_context(
46+
pytest.warns(DeprecationWarning,
47+
match='automatic object dtype is deprecated'))
5248
else:
5349
stack.enter_context(pytest.raises(ValueError))
5450

astropy/io/ascii/tests/test_ecsv.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
from astropy.table.table_helpers import simple_table
2424
from astropy.units import QuantityInfo
2525
from astropy.units import allclose as quantity_allclose
26-
from astropy.utils.compat import NUMPY_LT_1_19_1
2726

2827
from .common import TEST_DIR
2928

@@ -654,8 +653,6 @@ def test_read_bad_datatype():
654653
assert type(t['a'].dtype) == np.dtype("O")
655654

656655

657-
@pytest.mark.skipif(NUMPY_LT_1_19_1,
658-
reason="numpy cannot parse 'complex' as string until 1.19+")
659656
def test_read_complex():
660657
"""Test an ECSV v1.0 file with a complex column"""
661658
txt = """\

astropy/stats/biweight.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import numpy as np
88

9-
from .funcs import _expand_dims, median_absolute_deviation
9+
from .funcs import median_absolute_deviation
1010

1111
__all__ = ['biweight_location', 'biweight_scale', 'biweight_midvariance',
1212
'biweight_midcovariance', 'biweight_midcorrelation']
@@ -115,7 +115,7 @@ def biweight_location(data, c=6.0, M=None, axis=None, *, ignore_nan=False):
115115
if M is None:
116116
M = median_func(data, axis=axis)
117117
if axis is not None:
118-
M = _expand_dims(M, axis=axis) # NUMPY_LT_1_18
118+
M = np.expand_dims(M, axis=axis)
119119

120120
# set up the differences
121121
d = data - M
@@ -129,7 +129,7 @@ def biweight_location(data, c=6.0, M=None, axis=None, *, ignore_nan=False):
129129
return M
130130

131131
if axis is not None:
132-
mad = _expand_dims(mad, axis=axis) # NUMPY_LT_1_18
132+
mad = np.expand_dims(mad, axis=axis)
133133

134134
with np.errstate(divide='ignore', invalid='ignore'):
135135
u = d / (c * mad)
@@ -386,7 +386,7 @@ def biweight_midvariance(data, c=9.0, M=None, axis=None,
386386
if M is None:
387387
M = median_func(data, axis=axis)
388388
if axis is not None:
389-
M = _expand_dims(M, axis=axis) # NUMPY_LT_1_18
389+
M = np.expand_dims(M, axis=axis)
390390

391391
# set up the differences
392392
d = data - M
@@ -400,7 +400,7 @@ def biweight_midvariance(data, c=9.0, M=None, axis=None,
400400
if mad == 0. or np.isnan(mad):
401401
return mad ** 2 # variance units
402402
else:
403-
mad = _expand_dims(mad, axis=axis) # NUMPY_LT_1_18
403+
mad = np.expand_dims(mad, axis=axis)
404404

405405
with np.errstate(divide='ignore', invalid='ignore'):
406406
u = d / (c * mad)

astropy/stats/funcs.py

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -42,52 +42,6 @@
4242
"""
4343

4444

45-
# NUMPY_LT_1_18
46-
def _expand_dims(data, axis):
47-
"""
48-
Expand the shape of an array.
49-
50-
Insert a new axis that will appear at the `axis` position in the
51-
expanded array shape.
52-
53-
This function allows for tuple axis arguments.
54-
``numpy.expand_dims`` currently does not allow that, but it will in
55-
numpy v1.18 (https://github.com/numpy/numpy/pull/14051).
56-
``_expand_dims`` can be replaced with ``numpy.expand_dims`` when the
57-
minimum support numpy version is v1.18.
58-
59-
Parameters
60-
----------
61-
data : array-like
62-
Input array.
63-
axis : int or tuple of int
64-
Position in the expanded axes where the new axis (or axes) is
65-
placed. A tuple of axes is now supported. Out of range axes as
66-
described above are now forbidden and raise an `AxisError`.
67-
68-
Returns
69-
-------
70-
result : ndarray
71-
View of ``data`` with the number of dimensions increased.
72-
"""
73-
74-
if isinstance(data, np.matrix):
75-
data = np.asarray(data)
76-
else:
77-
data = np.asanyarray(data)
78-
79-
if not isinstance(axis, (tuple, list)):
80-
axis = (axis,)
81-
82-
out_ndim = len(axis) + data.ndim
83-
axis = np.core.numeric.normalize_axis_tuple(axis, out_ndim)
84-
85-
shape_it = iter(data.shape)
86-
shape = [1 if ax in axis else next(shape_it) for ax in range(out_ndim)]
87-
88-
return data.reshape(shape)
89-
90-
9145
def binom_conf_interval(k, n, confidence_level=0.68269, interval='wilson'):
9246
r"""Binomial proportion confidence interval given k successes,
9347
n trials.
@@ -847,7 +801,7 @@ def median_absolute_deviation(data, axis=None, func=None, ignore_nan=False):
847801

848802
# broadcast the median array before subtraction
849803
if axis is not None:
850-
data_median = _expand_dims(data_median, axis=axis) # NUMPY_LT_1_18
804+
data_median = np.expand_dims(data_median, axis=axis)
851805

852806
result = func(np.abs(data - data_median), axis=axis, overwrite_input=True)
853807
# this conditional can be removed after this PR is merged:

astropy/table/groups.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,6 @@ def keys(self):
238238
return self._keys
239239

240240
def aggregate(self, func):
241-
from astropy.utils.compat import NUMPY_LT_1_20
242-
243241
from .column import Column, MaskedColumn
244242

245243
i0s, i1s = self.indices[:-1], self.indices[1:]
@@ -250,15 +248,6 @@ def aggregate(self, func):
250248
mean_case = func is np.mean
251249
try:
252250
if not masked and (reduceat or sum_case or mean_case):
253-
# For numpy < 1.20 there is a bug where reduceat will fail to
254-
# raise an exception for mixin columns that do not support the
255-
# operation. For details see:
256-
# https://github.com/astropy/astropy/pull/12825#issuecomment-1082412447
257-
# Instead we try the function directly with a 2-element version
258-
# of the column
259-
if NUMPY_LT_1_20 and not isinstance(par_col, Column) and len(par_col) > 0:
260-
func(par_col[[0, 0]])
261-
262251
if mean_case:
263252
vals = np.add.reduceat(par_col, i0s) / np.diff(self.indices)
264253
else:

astropy/table/tests/test_table.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
from astropy.table import Column, MaskedColumn, QTable, Table, TableAttribute, TableReplaceWarning
2121
from astropy.tests.helper import assert_follows_unicode_guidelines
2222
from astropy.time import Time, TimeDelta
23-
from astropy.utils.compat.numpycompat import NUMPY_LT_1_20
2423
from astropy.utils.compat.optional_deps import HAS_PANDAS
2524
from astropy.utils.data import get_pkg_data_filename
2625
from astropy.utils.exceptions import AstropyUserWarning
@@ -1412,7 +1411,6 @@ def test_convert_numpy_object_array(self, table_types):
14121411
assert isinstance(np_d, np.ndarray)
14131412
assert np_d[()] is d
14141413

1415-
@pytest.mark.xfail(NUMPY_LT_1_20, reason="numpy array introspection changed")
14161414
def test_convert_list_numpy_object_array(self, table_types):
14171415
d = table_types.Table([[1, 2], [3, 4]], names=('a', 'b'))
14181416
ds = [d, d, d]

astropy/units/quantity.py

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
# LOCAL
1919
from astropy import config as _config
20-
from astropy.utils.compat import NUMPY_LT_1_20, NUMPY_LT_1_22
20+
from astropy.utils.compat import NUMPY_LT_1_22
2121
from astropy.utils.data_info import ParentDtypeInfo
2222
from astropy.utils.exceptions import AstropyDeprecationWarning, AstropyWarning
2323
from astropy.utils.misc import isiterable
@@ -1838,34 +1838,19 @@ def _wrap_function(self, function, *args, unit=None, out=None, **kwargs):
18381838
def trace(self, offset=0, axis1=0, axis2=1, dtype=None, out=None):
18391839
return self._wrap_function(np.trace, offset, axis1, axis2, dtype,
18401840
out=out)
1841-
if NUMPY_LT_1_20:
1842-
def var(self, axis=None, dtype=None, out=None, ddof=0, keepdims=False):
1843-
return self._wrap_function(np.var, axis, dtype,
1844-
out=out, ddof=ddof, keepdims=keepdims,
1845-
unit=self.unit**2)
1846-
else:
1847-
def var(self, axis=None, dtype=None, out=None, ddof=0, keepdims=False, *, where=True):
1848-
return self._wrap_function(np.var, axis, dtype,
1849-
out=out, ddof=ddof, keepdims=keepdims, where=where,
1850-
unit=self.unit**2)
1851-
1852-
if NUMPY_LT_1_20:
1853-
def std(self, axis=None, dtype=None, out=None, ddof=0, keepdims=False):
1854-
return self._wrap_function(np.std, axis, dtype, out=out, ddof=ddof,
1855-
keepdims=keepdims)
1856-
else:
1857-
def std(self, axis=None, dtype=None, out=None, ddof=0, keepdims=False, *, where=True):
1858-
return self._wrap_function(np.std, axis, dtype, out=out, ddof=ddof,
1859-
keepdims=keepdims, where=where)
1860-
1861-
if NUMPY_LT_1_20:
1862-
def mean(self, axis=None, dtype=None, out=None, keepdims=False):
1863-
return self._wrap_function(np.mean, axis, dtype, out=out,
1864-
keepdims=keepdims)
1865-
else:
1866-
def mean(self, axis=None, dtype=None, out=None, keepdims=False, *, where=True):
1867-
return self._wrap_function(np.mean, axis, dtype, out=out,
1868-
keepdims=keepdims, where=where)
1841+
1842+
def var(self, axis=None, dtype=None, out=None, ddof=0, keepdims=False, *, where=True):
1843+
return self._wrap_function(np.var, axis, dtype,
1844+
out=out, ddof=ddof, keepdims=keepdims, where=where,
1845+
unit=self.unit**2)
1846+
1847+
def std(self, axis=None, dtype=None, out=None, ddof=0, keepdims=False, *, where=True):
1848+
return self._wrap_function(np.std, axis, dtype, out=out, ddof=ddof,
1849+
keepdims=keepdims, where=where)
1850+
1851+
def mean(self, axis=None, dtype=None, out=None, keepdims=False, *, where=True):
1852+
return self._wrap_function(np.mean, axis, dtype, out=out,
1853+
keepdims=keepdims, where=where)
18691854

18701855
def round(self, decimals=0, out=None):
18711856
return self._wrap_function(np.round, decimals, out=out)

0 commit comments

Comments
 (0)