Skip to content

Casting to integer on conversion #163

@znicholls

Description

@znicholls

Hi there,

I have a question about behaviour which I was hoping you could help with. It focuses on this comment:

# Preserve the datatype of the input array if it was float32.

My question is: why isn't this also applied to np.float64 datatypes?

More info:

The minimal example below shows how this can cause a np.float64 dtype to be cast to integer even though a np.float32 dtype is not. This seems surprising, but maybe that is the idea? (As a sidenote, this only appears to happen on a linux operating system, it doesn't happen on mac...)

In [1]: import cf_units

In [2]: import numpy as np

In [3]: cf_units.__version__
Out[3]: '2.1.4'

In [4]: start = cf_units.Unit('days since 2100-01-01', calendar='365_day')

In [5]: target = cf_units.Unit('days since 2006-01-01', calendar='365_day')

In [6]: start.convert(np.array([1, 2, 3]).astype(np.float32), target)
Out[6]: array([34311., 34312., 34313.], dtype=float32)

In [7]: start.convert(np.array([1, 2, 3]).astype(np.float32), target).dtype
Out[7]: dtype('float32')  # type preserved

In [8]: start.convert(np.array([1, 2, 3]).astype(np.float64), target)
Out[8]: array([34311, 34312, 34313])

In [9]: start.convert(np.array([1, 2, 3]).astype(np.float64), target).dtype
Out[9]: dtype('int64')  # type not preserved

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions