Skip to content

np.hstack([ ]) throws IndexError instead of ValueError #8790

Closed

Description

@medakk

Calling np.hstack([ ]) throws an IndexError. It takes a bit more work to find out the root cause of the error, ie: the empty list.

>>> import numpy as np
>>> np.hstack([ ])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/numpy/core/shape_base.py", line 277, in hstack
    if arrs[0].ndim == 1:
IndexError: list index out of range

np.vstack, on the other hand, throws a more appropriate error.

>>> np.vstack([ ])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/numpy/core/shape_base.py", line 230, in vstack
    return _nx.concatenate([atleast_2d(_m) for _m in tup], 0)
ValueError: need at least one array to concatenate

Activity

  1. zixu-zhao commented on Mar 20, 2017

    @zixu-zhao

    I will take this. Thanks!

  2. zixu-zhao commented on Mar 20, 2017

    @zixu-zhao

    @eric-wieser What error message would you prefer to use there?
    How about ValueError: need at least one element to concatenate?

    And if it is acceptable to add
    if len(arrs) == 0: raise ValueError('need at least one element to concatenate')
    before the line if arrs[0].ndim == 1:?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions