Skip to content

Commit faf114d

Browse files
committed
docs on raising
1 parent bf829c3 commit faf114d

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

doc/source/whatsnew/v0.24.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ Reduction and groupby operations such as 'sum' work.
160160

161161
.. _whatsnew_0240.enhancements.array:
162162

163-
A new top-level method :func:`array` has been added for creating arrays (:issue:`22860`).
163+
A new top-level method :func:`array` has been added for creating 1-dimensional arrays (:issue:`22860`).
164164
This can be used to create any :ref:`extension array <extending.extension-types>`, including
165165
extension arrays registered by :ref:`3rd party libraries <ecosystem.extensions>`, or to
166166
create NumPy arrays.

pandas/core/arrays/array_.py

+17-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ def array(data, # type: Sequence[object]
2323
----------
2424
data : Sequence of objects
2525
The scalars inside `data` should be instances of the
26-
scalar type for `dtype`.
26+
scalar type for `dtype`. It's expected that `data`
27+
represents a 1-dimensional array of data.
2728
2829
When `data` is an Index or Series, the underlying array
2930
will be extracted from `data`.
@@ -81,6 +82,11 @@ def array(data, # type: Sequence[object]
8182
-------
8283
array : Union[numpy.ndarray, ExtensionArray]
8384
85+
Raises
86+
------
87+
ValueError
88+
When `data` is not 1-dimensional.
89+
8490
See Also
8591
--------
8692
numpy.array : Construct a NumPy array.
@@ -134,14 +140,23 @@ def array(data, # type: Sequence[object]
134140
the dtype:
135141
136142
>>> pd.array([1, 2, np.nan], dtype='Int64')
137-
IntegerArray([1, 2, nan], dtype='Int64')
143+
<IntegerArray>
144+
[1, 2, NaN]
145+
Length: 3, dtype: Int64
138146
139147
Pandas will infer an ExtensionArray for some types of data:
140148
141149
>>> pd.array([pd.Period('2000', freq="D"), pd.Period("2000", freq="D")])
142150
<PeriodArray>
143151
['2000-01-01', '2000-01-01']
144152
Length: 2, dtype: period[D]
153+
154+
A ValueError is raised when the input has the wrong dimensionality.
155+
156+
>>> pd.array(1)
157+
Traceback (most recent call last):
158+
...
159+
ValueError: Cannot pass scalar '1' to 'pandas.array'.
145160
"""
146161
from pandas.core.arrays import (
147162
period_array, ExtensionArray, IntervalArray

0 commit comments

Comments
 (0)