Better support for numpy types.

Issue #595 resolved
Seshu Yamajala created an issue

I find that certain numpy types work while others don't. For example, doing something like ws['A1'] = np.int32(1) causes bind_value to throw a ValueError exception, while ws['A2'] = np.float(1) works. Similarly, ws['A3'] = np.bool(True) also throws an exception.

Maybe numpy could be an optional dependency, and if it is installed then numpy types could be added to the list of types _bind_value checks against?

Comments (9)

  1. Seshu Yamajala reporter

    The workaround I came up with right now is to convert my numpy types to native python types using np.asscalar, but thats causing a significant slow down. Looking at openpyxl/compat/ it seems that all that needs to be done is wrap numpy import in a try-except and then add the numeric types here to NUMERIC_TYPES.

  2. Seshu Yamajala reporter
        import numpy as np
        NUMPY_NUMERIC_TYPES = (np.int_, np.intc, np.intp,
                               np.int8, np.int16, np.int32, np.int64,
                               np.uint8, np.uint16, np.uint32, np.uint64,
                               np.float_, np.float16, np.float32, np.float64,
                               np.complex_, np.complex64, np.complex128)
    except ImportError:

    Something similar needs to be done for np.bool_

  3. Joseph Fox-Rabinovitz

    @sheshu The reason that your exceptions are happening for some cases and not others is that np.float_ and np.float64 are subclasses of builtin float. Similar for np.int_, np.intp, and one of the other int types. Builtin bool is also a subclass of int.

    I am trying to figure out a way of including the other numpy types via subclassing, without creating the try...except block.

  4. Joseph Fox-Rabinovitz

    At most, you would need to add np.bool_, np.integer, np.floating. If you want complex support (which I don't think is supported ATM), replace np.floating with np.inexact.

  5. CharlieC

    I don't think there's any way around a try…except clause for an optional dependency. But other parts of the system are important such as times and dates, which need to get cast to Excel serials. Then we need to check the string representation: currently "%.16g" is used. There is no need in more precision because Excel doesn't have it.

  6. Log in to comment