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

PandasArray does not support slicing consistent with other array types #32692

Closed
maartenb opened this issue Mar 13, 2020 · 5 comments
Closed
Assignees
Labels
Deprecate Functionality to remove in pandas ExtensionArray Extending pandas with custom dtypes or arrays. Indexing Related to indexing on series/frames, not to indexes themselves

Comments

@maartenb
Copy link

maartenb commented Mar 13, 2020

Code Sample

>>> import pandas as pd
>>> x = pd.Series([1, 2, 3], index=pd.to_datetime(['2020-01-01', '2020-01-02', '2020-01-03']))
>>> x[:,None].ndim
>>> x.to_numpy()[:,None].ndim
>>> x.index.array[:,None].ndim
>>> x.array[:,None].ndim

Problem description

Currently PandasArray behaves differently then either the Series or or np.ndarray.
This causes a problem when passing the PandasArray to matplotlib for example.
PandasArray should implement this consistently like the other cases.

The output from above is:

>>> import pandas as pd
>>> x = pd.Series([1, 2, 3], index=pd.to_datetime(['2020-01-01', '2020-01-02', '2020-01-03']))
>>> x[:,None].ndim
2
>>> x.to_numpy()[:,None].ndim
2
>>> x.index.array[:,None].ndim
2
>>> x.array[:,None].ndim
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/maartenb/anaconda3/envs/Corona/lib/python3.8/site-packages/pandas/core/arrays/numpy_.py", line 240, in __getitem__
    result = type(self)(result)
  File "/Users/maartenb/anaconda3/envs/Corona/lib/python3.8/site-packages/pandas/core/arrays/numpy_.py", line 147, in __init__
    raise ValueError("PandasArray must be 1-dimensional.")
ValueError: PandasArray must be 1-dimensional.

Expected Output

2
2
2
2

Output of pd.show_versions()

Python 3.8.1 (default, Jan  8 2020, 16:15:59) 
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas as pd
>>> pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.8.1.final.0
python-bits : 64
OS : Darwin
OS-release : 18.7.0
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.0.1
numpy : 1.18.1
pytz : 2019.3
dateutil : 2.8.1
pip : 20.0.2
setuptools : 46.0.0.post20200309
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : 3.1.3
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : None

@MillanSharma
Copy link

take

@TomAugspurger
Copy link
Contributor

TomAugspurger commented Mar 14, 2020

This is intentional. Extension arrays are 1D, so slicing with a 2D indexer is invalid.

@maartenb
Copy link
Author

Well, DatetimeArray, IntegerArray are ExtensionArrays too and do accept it.

I get that from a modeling perspective it makes sense, but it breaks matplotlib and possibly other packages relying on this behavior. I guess this originated from numpy.

@jorisvandenbossche
Copy link
Member

On Index it is being deprecated (with your example above):

In [9]: x.index[:,None]    
/home/joris/miniconda3/envs/dev/bin/ipython:1: DeprecationWarning: Support for multi-dimensional indexing (e.g. `index[:, None]`) on an Index is deprecated and will be removed in a future version.  Convert to a numpy array before indexing instead.
  #!/home/joris/miniconda3/envs/dev/bin/python
Out[9]: 
array([['2020-01-01T00:00:00.000000000'],
       ['2020-01-02T00:00:00.000000000'],
       ['2020-01-03T00:00:00.000000000']], dtype='datetime64[ns]')

and the plan is to do the same for the other objects. Series is planned, see #27837, but we might have overlooked the extension arrays, as we should probably do the same for those as well

@jbrockmendel jbrockmendel added the ExtensionArray Extending pandas with custom dtypes or arrays. label Sep 1, 2020
@mroeschke mroeschke added Deprecate Functionality to remove in pandas Indexing Related to indexing on series/frames, not to indexes themselves labels Jul 30, 2021
@jbrockmendel
Copy link
Member

This now works correctly, closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Deprecate Functionality to remove in pandas ExtensionArray Extending pandas with custom dtypes or arrays. Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

No branches or pull requests

6 participants