Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: np.min(pd.Index) and np.max(pd.Index) raise a TypeError #26125

Closed
jschendel opened this issue Apr 17, 2019 · 4 comments · Fixed by #26324
Closed

BUG: np.min(pd.Index) and np.max(pd.Index) raise a TypeError #26125

jschendel opened this issue Apr 17, 2019 · 4 comments · Fixed by #26324
Labels
Milestone

Comments

@jschendel
Copy link
Member

Code Sample, a copy-pastable example if possible

In [1]: import numpy as np; import pandas as pd

In [2]: np.__version__, pd.__version__
Out[2]: ('1.16.2', '0.25.0.dev0+421.g8b60ebb359')

In [3]: idx = pd.Index([10, 20, 11]); idx
Out[3]: Int64Index([10, 20, 11], dtype='int64')

In [4]: np.min(idx)
---------------------------------------------------------------------------
TypeError: min() got an unexpected keyword argument 'out'

In [5]: np.max(idx)
---------------------------------------------------------------------------
TypeError: max() got an unexpected keyword argument 'out'

Note that other numpy functions work fine:

In [6]: np.sum(idx), np.prod(idx), np.argmax(idx), np.argmin(idx)
Out[6]: (41, 2200, 1, 0)

And np.min and np.max work on other pandas data structures:

In [7]: s = pd.Series([10, 20, 11])

In [8]: np.min(s), np.max(s)
Out[8]: (10, 20)

In [9]: df = pd.DataFrame({'A': [10, 20, 11], 'B': [3, 1, 4]})

In [10]: np.min(df)
Out[10]:
A    10
B     1
dtype: int64

In [11]: np.max(df)
Out[11]:
A    20
B     4
dtype: int64

Problem description

Both np.min and np.max raise a TypeError when applied to a pd.Index. Using pd.Index.min and pd.Index.max is more idiomatic for pandas, but using the numpy equivalent functions should work, as they do for other pandas data structures.

Expected Output

I'd expect np.min(pd.Index) and np.max(pd.Index) to be equivalent to pd.Index.min and pd.Index.max, as is the case with other pandas data structures.

Output of pd.show_versions()

INSTALLED VERSIONS

commit: 8b60ebb
python: 3.6.8.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 78 Stepping 3, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None

pandas: 0.25.0.dev0+421.g8b60ebb359
pytest: 4.2.0
pip: 19.0.1
setuptools: 40.6.3
Cython: 0.28.2
numpy: 1.16.2
scipy: 1.2.1
pyarrow: 0.6.0
xarray: 0.9.6
IPython: 7.2.0
sphinx: 1.8.2
patsy: 0.4.1
dateutil: 2.6.0
pytz: 2017.2
blosc: None
bottleneck: 1.2.1
tables: 3.4.2
numexpr: 2.6.9
feather: 0.4.0
matplotlib: 2.0.2
openpyxl: 2.4.8
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: 0.9.8
lxml.etree: 3.8.0
bs4: None
html5lib: 0.999
sqlalchemy: 1.1.13
pymysql: None
psycopg2: None
jinja2: 2.9.6
s3fs: None
fastparquet: 0.1.5
pandas_gbq: None
pandas_datareader: None
gcsfs: None

@jschendel jschendel added the Bug label Apr 17, 2019
@jschendel jschendel added this to the Contributions Welcome milestone Apr 17, 2019
@Batalex
Copy link
Contributor

Batalex commented Apr 19, 2019

Related issue: numpy/numpy#13285

@Batalex
Copy link
Contributor

Batalex commented Apr 28, 2019

I have looked a bit into this, but I am not satisfied with what I got. My understanding is that as of today numpy.core.fromnumeric._wrapreduction will first look for <func> in the object's attributes instead of using __array_ufunc__ duck array. This means that we cannot fix this issue for now by implementing __array_ufunc__ in pandas.core.indexes.base.Index.

A dirty fix is to add a kwargs passthrough arg to IndexOpsMixin's min & max implementations like this :

max(self, axis=None, skipna=True, **kwargs)

It does not break tests, but it is a public interface modification 👎

@jreback
Copy link
Contributor

jreback commented Apr 28, 2019

this is what we do for all other method and is acceptable
see pandas.compat.numpy

@Batalex
Copy link
Contributor

Batalex commented Apr 28, 2019

Ok then, I will make a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants