Skip to content

Commit 89cd33c

Browse files
TomAugspurgerPingviinituutti
authored andcommitted
API: added array (pandas-dev#23581)
1 parent 3dfcaec commit 89cd33c

File tree

15 files changed

+595
-16
lines changed

15 files changed

+595
-16
lines changed

doc/source/api.rst

+75
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,19 @@ strings and apply several methods to it. These can be accessed like
720720
Series.dt
721721
Index.str
722722

723+
724+
.. _api.arrays:
725+
726+
Arrays
727+
------
728+
729+
Pandas and third-party libraries can extend NumPy's type system (see :ref:`extending.extension-types`).
730+
731+
.. autosummary::
732+
:toctree: generated/
733+
734+
array
735+
723736
.. _api.categorical:
724737

725738
Categorical
@@ -808,6 +821,65 @@ following usable methods and properties:
808821
Series.cat.as_ordered
809822
Series.cat.as_unordered
810823

824+
.. _api.arrays.integerna:
825+
826+
Integer-NA
827+
~~~~~~~~~~
828+
829+
:class:`arrays.IntegerArray` can hold integer data, potentially with missing
830+
values.
831+
832+
.. autosummary::
833+
:toctree: generated/
834+
835+
arrays.IntegerArray
836+
837+
.. _api.arrays.interval:
838+
839+
Interval
840+
~~~~~~~~
841+
842+
:class:`IntervalArray` is an array for storing data representing intervals.
843+
The scalar type is a :class:`Interval`. These may be stored in a :class:`Series`
844+
or as a :class:`IntervalIndex`. :class:`IntervalArray` can be closed on the
845+
``'left'``, ``'right'``, or ``'both'``, or ``'neither'`` sides.
846+
See :ref:`indexing.intervallindex` for more.
847+
848+
.. currentmodule:: pandas
849+
850+
.. autosummary::
851+
:toctree: generated/
852+
853+
IntervalArray
854+
855+
.. _api.arrays.period:
856+
857+
Period
858+
~~~~~~
859+
860+
Periods represent a span of time (e.g. the year 2000, or the hour from 11:00 to 12:00
861+
on January 1st, 2000). A collection of :class:`Period` objects with a common frequency
862+
can be collected in a :class:`PeriodArray`. See :ref:`timeseries.periods` for more.
863+
864+
.. autosummary::
865+
:toctree: generated/
866+
867+
arrays.PeriodArray
868+
869+
Sparse
870+
~~~~~~
871+
872+
Sparse data may be stored and operated on more efficiently when there is a single value
873+
that's often repeated. :class:`SparseArray` is a container for this type of data.
874+
See :ref:`sparse` for more.
875+
876+
.. _api.arrays.sparse:
877+
878+
.. autosummary::
879+
:toctree: generated/
880+
881+
SparseArray
882+
811883
Plotting
812884
~~~~~~~~
813885

@@ -1701,6 +1773,7 @@ IntervalIndex Components
17011773
IntervalIndex.get_indexer
17021774
IntervalIndex.set_closed
17031775
IntervalIndex.overlaps
1776+
IntervalArray.to_tuples
17041777

17051778

17061779
.. _api.multiindex:
@@ -1933,6 +2006,8 @@ Methods
19332006
PeriodIndex.strftime
19342007
PeriodIndex.to_timestamp
19352008

2009+
.. api.scalars:
2010+
19362011
Scalars
19372012
-------
19382013

doc/source/whatsnew/v0.24.0.rst

+35
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,41 @@ Reduction and groupby operations such as 'sum' work.
161161

162162
The Integer NA support currently uses the captilized dtype version, e.g. ``Int8`` as compared to the traditional ``int8``. This may be changed at a future date.
163163

164+
.. _whatsnew_0240.enhancements.array:
165+
166+
A new top-level method :func:`array` has been added for creating 1-dimensional arrays (:issue:`22860`).
167+
This can be used to create any :ref:`extension array <extending.extension-types>`, including
168+
extension arrays registered by :ref:`3rd party libraries <ecosystem.extensions>`. See
169+
170+
See :ref:`Dtypes <basics.dtypes>` for more on extension arrays.
171+
172+
.. ipython:: python
173+
174+
pd.array([1, 2, np.nan], dtype='Int64')
175+
pd.array(['a', 'b', 'c'], dtype='category')
176+
177+
Passing data for which there isn't dedicated extension type (e.g. float, integer, etc.)
178+
will return a new :class:`arrays.PandasArray`, which is just a thin (no-copy)
179+
wrapper around a :class:`numpy.ndarray` that satisfies the extension array interface.
180+
181+
.. ipython:: python
182+
183+
pd.array([1, 2, 3])
184+
185+
On their own, a :class:`arrays.PandasArray` isn't a very useful object.
186+
But if you need write low-level code that works generically for any
187+
:class:`~pandas.api.extensions.ExtensionArray`, :class:`arrays.PandasArray`
188+
satisfies that need.
189+
190+
Notice that by default, if no ``dtype`` is specified, the dtype of the returned
191+
array is inferred from the data. In particular, note that the first example of
192+
``[1, 2, np.nan]`` would have returned a floating-point array, since ``NaN``
193+
is a float.
194+
195+
.. ipython:: python
196+
197+
pd.array([1, 2, np.nan])
198+
164199
.. _whatsnew_0240.enhancements.read_html:
165200

166201
``read_html`` Enhancements

pandas/arrays/__init__.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,17 @@
33
44
See :ref:`extending.extension-types` for more.
55
"""
6-
from pandas.core.arrays import PandasArray
6+
from pandas.core.arrays import (
7+
IntervalArray, PeriodArray, Categorical, SparseArray, IntegerArray,
8+
PandasArray
9+
)
710

811

912
__all__ = [
10-
'PandasArray'
13+
'Categorical',
14+
'IntegerArray',
15+
'IntervalArray',
16+
'PandasArray',
17+
'PeriodArray',
18+
'SparseArray',
1119
]

pandas/core/api.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,26 @@
44

55
import numpy as np
66

7+
from pandas.core.arrays import IntervalArray
8+
from pandas.core.arrays.integer import (
9+
Int8Dtype,
10+
Int16Dtype,
11+
Int32Dtype,
12+
Int64Dtype,
13+
UInt8Dtype,
14+
UInt16Dtype,
15+
UInt32Dtype,
16+
UInt64Dtype,
17+
)
718
from pandas.core.algorithms import factorize, unique, value_counts
819
from pandas.core.dtypes.missing import isna, isnull, notna, notnull
9-
from pandas.core.arrays import Categorical
20+
from pandas.core.dtypes.dtypes import (
21+
CategoricalDtype,
22+
PeriodDtype,
23+
IntervalDtype,
24+
DatetimeTZDtype,
25+
)
26+
from pandas.core.arrays import Categorical, array
1027
from pandas.core.groupby import Grouper
1128
from pandas.io.formats.format import set_eng_float_format
1229
from pandas.core.index import (Index, CategoricalIndex, Int64Index,

pandas/core/arrays/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from .array_ import array # noqa
12
from .base import (ExtensionArray, # noqa
23
ExtensionOpsMixin,
34
ExtensionScalarOpsMixin)

0 commit comments

Comments
 (0)