Construct ndarray/array from iterator/generator
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 32.8k
- Forks
- 12.8k
- Avg merge
- 1d 7h
- Merged PRs (30d)
- 197
Description
Reproducing code example:
I was trying to calculate a mean from an array. Then I decided to do some filtering, which changes the input array/list to an iterator.
np.mean([0,1,2]) # OK
np.mean(value for value in [0,1,2]if value > 0) # CRASH
because internally mean calls array(sequence). And this happens:
>>> np.array(iter([1,2]))
array(<list_iterator object at 0x000001CFC0F96608>, dtype=object)
>>> np.ndarray(iter([1,2])) # for reference
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: expected sequence object with len >= 0 or a single integer
It seems very weird for generators and iterators to just be placed inside an array, and counter-intuitive compared to many other stdlib functions, like all(), any(), sum(), iter(), list(), etc...
Suggestion: traverse iterators and generators when creating ndarray and array and make the array with its elements.
There might be someone that wants to preserve the old behavior, so one could write array([iterator])
Numpy/Python version information:
1.18.1 3.7.5 (tags/v3.7.5:5c02a39a0b, Oct 15 2019, 00:11:34) [MSC v.1916 64 bit (AMD64)]
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start from the np.array and np.ndarray construction entry points demonstrated in the report, and reproduce the iterator and generator examples on the stated NumPy/Python versions. Investigate how iterator inputs should be distinguished from the existing array([iterator]) behavior. Done means the intended construction semantics are decided and covered for both examples.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- data
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100