Skip to content

Impossible to input np.longdouble into custom time formats #9341

@aarchiba

Description

@aarchiba

I am trying to write a custom time format "mjd_long" which accepts and returns np.longdouble values upon conversion, to make nanosecond accuracy easier. Unfortunately during the preparation stage, before values are handed off to the custom code, they are passed through _make_array, which forcibly converts any non-approved types, including np.longdouble, into np.float64, losing accuracy in the process. As far as I can tell it is a simple matter of allowing higher-precision numerical dtypes (np.float96, np.float128, np.longdouble) through untouched. There is some delicacy about platforms like Windows/MSVC that do not have extended-precision types, where I think some of the type names are not defined. But as far as I can tell this is the only obstacle to writing a custom mjd_long type. (I think with some care plain mjd objects could be made to respect the extra precision of np.longdouble as well, but that's another issue.)

def _make_array(val, copy=False):
    """
    Take ``val`` and convert/reshape to an array.  If ``copy`` is `True`
    then copy input values.

    Returns
    -------
    val : ndarray
        Array version of ``val``.
    """
    val = np.array(val, copy=copy, subok=True)

    # Allow only float64, string or object arrays as input
    # (object is for datetime, maybe add more specific test later?)
    # This also ensures the right byteorder for float64 (closes #2942).
    if not (val.dtype == np.float64 or val.dtype.kind in 'OSUMaV'):
        val = np.asanyarray(val, dtype=np.float64)

    return val

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions