Skip to content

Unexpected truncation when storing float to int array #8733

Closed as not planned
@OliverEvans96

Description

@OliverEvans96

I was caught offguard by the following behavior using numpy 1.12:

Current behavior

In  [1]:  a=array([1,0])
          a[0] = 1 + 1.1  
          print(a.dtype)       
          print(a)
          print(a.dtype)               
Out [1]:  int64
          [ 2  0 ]
          int64 

Expected behavior

In  [2]:  a=array([1,0])
          a[0] = 1 + 1.1  
          print(a.dtype)       
          print(a)
          print(a.dtype)               
Out [2]:  int64
          [ 2.1  0. ]
          float64

It may be that this is intentional behavior, but I would like to suggest automatic conversion of integer arrays to floating point arrays when a float is saved to an element or slice of the array. I'm not sure how straighforward implementation of this feature would be.

At the very least, there ought to be a warning when truncation occurs, especially since I at no point in the above code explicity declare the dtype of A to be int64, so I (and I think most users) would expect it to default to float64.

I'm interested in opinions on the matter.

Thanks,
Oliver

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions