Skip to content

convert_to_unit a unyt_array may cause problem in previously created slices. #552

@haoenz

Description

@haoenz
  • unyt version: 3.0.3
  • Python version: 3.12.5
  • Operating System: Debian 6.1.119

Description

If I have a unyt_array and have created some slices from it, modifying the original array with convert_to_unit will cause those slices to produce incorrect results, and vice versa.

From a design perspective, I'm not sure whether them should update accordingly or remain unchanged, but the current behavior is definitely problematic.

What I Did

from unyt import unyt_array as YTArray

arr = YTArray([1,2,3,4],"m")
arr_slice = arr[:2]
print(arr) # [1,2,3,4] m
print(arr_slice) # [1,2] m

arr[0] = 5
print(arr) # [5,2,3,4] m
print(arr_slice) # [5,2] m

arr.convert_to_units("cm") 
print(arr) # [500,200,300,400] cm
print(arr_slice) # [random,numbers] m

arr = YTArray([1,2,3,4],"m")
arr_slice = arr[:2]

arr_slice[0] = 5
print(arr_slice) # [5,2] m
print(arr) # [5,2,3,4] m

arr_slice.convert_to_units("cm") 
print(arr_slice) # [500,200] cm
print(arr) # [random,numbers,3,4] m

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