Description
-
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 ValueError
s or TypeError
s 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