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: pd.Series() constructor with 2d NumPy array raises bare Exception #35744

Closed
2 of 3 tasks
micahjsmith opened this issue Aug 15, 2020 · 3 comments · Fixed by #35769
Closed
2 of 3 tasks

BUG: pd.Series() constructor with 2d NumPy array raises bare Exception #35744

micahjsmith opened this issue Aug 15, 2020 · 3 comments · Fixed by #35769
Assignees
Labels
Constructors Series/DataFrame/Index/pd.array Constructors Error Reporting Incorrect or improved errors from pandas Series Series data structure
Milestone

Comments

@micahjsmith
Copy link
Contributor

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

import pandas as pd
import numpy as np
arr = np.random.rand(10, 4)
pd.Series(arr)

I get this output

---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
<ipython-input-4-974eca309da1> in <module>
      2 import numpy as np
      3 arr = np.random.rand(10, 4)
----> 4 pd.Series(arr)

~/.pyenv/versions/3.8.3/envs/census/lib/python3.8/site-packages/pandas/core/series.py in __init__(self, data, index, dtype, name, copy, fastpath)
    325                     data = data.copy()
    326             else:
--> 327                 data = sanitize_array(data, index, dtype, copy, raise_cast_failure=True)
    328 
    329                 data = SingleBlockManager.from_array(data, index)

~/.pyenv/versions/3.8.3/envs/census/lib/python3.8/site-packages/pandas/core/construction.py in sanitize_array(data, index, dtype, copy, raise_cast_failure)
    490     elif subarr.ndim > 1:
    491         if isinstance(data, np.ndarray):
--> 492             raise Exception("Data must be 1-dimensional")
    493         else:
    494             subarr = com.asarray_tuplesafe(data, dtype=dtype)

Exception: Data must be 1-dimensional

Problem description

Yes, creating a series from an array is invalid. But the problem is that a bare Exception is raised for this. A different exception type like ValueError is expected. User code should [mostly] never be catching bare Exceptions, but may reasonable catch ValueErrors or TypeErrors from pandas routines.

I'm not entirely sure of the rationale for raising a bare Exception in the sanitize_array method, I started looking through the git blame but didn't go back that far.

I think a PR that just raises a ValueError in sanitize_array would be an appropriate fix and I can create that if given the thumbs up.

Expected Output

A ValueError is raised, that can be caught be user code.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : d9fff27
python : 3.8.3.final.0
python-bits : 64
OS : Darwin
OS-release : 19.6.0
Version : Darwin Kernel Version 19.6.0: Sun Jul 5 00:43:10 PDT 2020; root:xnu-6153.141.1~9/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.1.0
numpy : 1.19.0
pytz : 2020.1
dateutil : 2.8.1
pip : 20.1.1
setuptools : 49.2.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.16.1
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : 0.8.0
fastparquet : None
gcsfs : None
matplotlib : 3.3.0
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pyxlsb : None
s3fs : None
scipy : 1.5.1
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

@micahjsmith micahjsmith added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 15, 2020
@dsaxton dsaxton added Error Reporting Incorrect or improved errors from pandas and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 16, 2020
@dsaxton
Copy link
Member

dsaxton commented Aug 16, 2020

Thanks @micahjsmith, I agree a ValueError seems appropriate here since the np.ndarray type is valid. PR would be appreciated.

@dsaxton dsaxton added this to the Contributions Welcome milestone Aug 16, 2020
@micahjsmith
Copy link
Contributor Author

take

@simonjayhawkins simonjayhawkins changed the title BUG: sanitize_array raises bare Exception BUG: pd.Series() constructor with 2d NumPy array raises bare Exception Aug 17, 2020
@simonjayhawkins simonjayhawkins added Constructors Series/DataFrame/Index/pd.array Constructors Series Series data structure labels Aug 17, 2020
@simonjayhawkins
Copy link
Member

@micahjsmith the PR needs to be raised against this repository

@jreback jreback modified the milestones: Contributions Welcome, 1.2 Sep 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Constructors Series/DataFrame/Index/pd.array Constructors Error Reporting Incorrect or improved errors from pandas Series Series data structure
Projects
None yet
4 participants