Skip to content

BUG: trim_zeros fails with sequence of axes #29945

@jakevdp

Description

@jakevdp

Describe the issue:

The numpy.trim_zeros documentation states that the axis argument may be an int or a sequence; however passing a sequence leads to an error.

Reproduce the code example:

In [1]: import numpy as np

In [2]: x = np.zeros((4, 4))

In [3]: x[1:3, 1:3] = 1

In [4]: np.trim_zeros(x, axis=(0, 1))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[4], line 1
----> 1 np.trim_zeros(x, axis=(0, 1))

File ~/.local/share/virtualenvs/jax-LBbfM5ix/lib/python3.12/site-packages/numpy/lib/_function_base_impl.py:2002, in trim_zeros(filt, trim, axis)
   1999     sl = tuple(slice(*x) for x in zip(start, stop))
   2000 else:
   2001     # only trim single axis
-> 2002     axis = normalize_axis_index(axis, filt_.ndim)
   2003     sl = (slice(None),) * axis + (slice(start[axis], stop[axis]),) + (...,)
   2005 trimmed = filt[sl]

TypeError: 'tuple' object cannot be interpreted as an integer

Python and NumPy Versions:

>>>  import sys, numpy; print(numpy.__version__); print(sys.version)
2.3.3
3.12.3 (v3.12.3:f6650f9ad7, Apr  9 2024, 08:18:47) [Clang 13.0.0 (clang-1300.0.29.30)]

Metadata

Metadata

Assignees

Labels

00 - BugsprintableIssue fits the time-frame and setting of a sprint

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions