#27038·numpy

DOC: Improve fromstring documentation & errors/warnings regarding binary mode

Author: e-petCreated Jul 25, 2024Updated Sep 16, 2026
Labels04 - Documentationsustain-2026

Issue with current documentation:

I was trying to convert a string '3' to np.dtypes.Int64DType.

My first attempt was np.dtypes.Int64DType('3'), hoping that the dtype would be callable which it is not.

Then followed these attempts:

python
dt = numpy.dtypes.Int64DType
dt('3')  # I was somewhat hoping this would work but the dtype is not callable
np.fromstring('3', dtype=dt)  # ValueError: string size must be a multiple of element size
np.fromstring('3', dtype=dt, count=1)  # ValueError: string is smaller than requested size
np.fromstring('[3]', dtype=dt, count=1)  # ValueError: Cannot create an object array from a string

For the latter three calls, I also got a <string>:1: DeprecationWarning: The binary mode of fromstring is deprecated, as it behaves surprisingly on unicode inputs. Use frombuffer instead - which I did not understand, because

  • '3' is not a binary string
  • I did not see a "binary mode" flag or anything of the sort

It took me a long time to realize that the deprecated default optional argument sep='' was causing this behavior. I personally find this highly unintuitive and surprising. (I did not look at this argument at all because I was interested in the scalar case.)

Indeed, np.fromstring('3', dtype=int, sep=' ')at least works.

However, np.fromstring('3', dtype=np.dtypes.Int64DType, sep=' ')gives ValueError: Cannot create an object array from a string - which I again find surprising.

Idea or request for content:

At the very least, I would suggest adding a note to the deprecation warning that the binary mode is triggered by the default optional argument sep='', and that passing any other value to sep will fix the issue.

In addition, I would suggest adding some comment to the documentation on why passing things like dtype=numpy.dtypes.Int64DType does not work.

In addition, I would suggest adding a comment that np.array(str, dtype=...) might be what one is looking for.