preserve_dtype_by_default#
- astropy.units.preserve_dtype_by_default()[source]#
Context in which integer input keeps its dtype when creating a Quantity.
Use this for an operation that should not change the dtype of what it is given. Inside astropy, adding a column to a
QTableand reconstructing a serialized column both do so, which is why an integer column with a unit keeps its dtype.An explicit
dtypeargument is still honored inside the context, and setting thequantity_convert_int_to_floatconfiguration item toalwaysoverrides the context, giving the conversion to float of astropy 7.x and earlier.Examples
>>> import numpy as np >>> import astropy.units as u >>> data = np.array([1, 2], dtype="int64") >>> with u.preserve_dtype_by_default(): ... q = u.Quantity(data, u.ct) >>> q.dtype dtype('int64')