numpy / numpy/numpy

ENH: np.histogram2d() does not support argument 'auto' for `bins`.

Open
#20,215 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

01 - Enhancement component: numpy.lib
Dominant language
Python
Stars
32.8k
Forks
12.8k
Avg merge
1d 7h
Merged PRs (30d)
197

Description

Describe the issue:
  • np.historgram2d() does not support argument 'auto' bin selection
  • and it is not clear from the description what does it use as default
  • while this does work:

x_edges = np.histogram_bin_edges(x, bins='auto')
y_edges = np.histogram_bin_edges(y, bins='auto')
np.histogram2d(x, y, bins=[x_edges, y_edges])

Reproduce the code example:
import numpy as np

np.histogram2d(x, y, bins=['auto','auto'])
or 
np.histogram2d(x, y, bins='auto')
Error message:
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-22-f1386dd21766> in <module>()
----> 1 np.histogram2d(x, y, bins=['auto','auto'])
      2 
      3 # np.histogram_bin_edges(x, bins='auto')

<__array_function__ internals> in histogram2d(*args, **kwargs)

1 frames
<__array_function__ internals> in histogramdd(*args, **kwargs)

/usr/local/lib/python3.7/dist-packages/numpy/lib/histograms.py in histogramdd(sample, bins, range, normed, weights, density)
   1045     for i in _range(D):
   1046         if np.ndim(bins[i]) == 0:
-> 1047             if bins[i] < 1:
   1048                 raise ValueError(
   1049                     '`bins[{}]` must be positive, when an integer'.format(i))

TypeError: '<' not supported between instances of 'str' and 'int'
NumPy/Python version information:

1.21.3 3.7.12 (default, Sep 10 2021, 00:21:48)
[GCC 7.5.0]

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the np.histogram2d entry point and its histogramdd implementation, then compare how histogram_bin_edges handles the 'auto' option. Reproduce the reported TypeError with bins='auto' and ['auto','auto']; done means both forms work and the default bin behavior is clear from the description.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data-visualization
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.