Skip to content

Cannot cast one-element object array of empty string to string dtype on 1.21.0rc1 #19085

@adeak

Description

@adeak

There's an admittedly edge case that changed behaviour, probably due to the type promotion changes:

>>> import numpy as np
>>> np.array([''], dtype=object).astype('U')

On 1.20.3 this returns

array([''], dtype='<U1')

whereas on the 1.21.0rc1 release candidate this is an error:

>>> np.array([''], dtype=object).astype('U')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Cannot cast array data from dtype('O') to dtype('<U') according to the rule 'unsafe'

I note that casting scalar strings or non-empty arrays still works:

>>> np.array('', dtype=object).astype('U')
array('', dtype='<U1')
>>> np.array(['a'], dtype=object).astype('U')
array(['a'], dtype='<U1')

Is this new error expected?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions