HOME


sh-3ll 1.0
DIR:/usr/local/lib64/python3.6/site-packages/pandas/core/__pycache__/
Upload File :
Current File : //usr/local/lib64/python3.6/site-packages/pandas/core/__pycache__/construction.cpython-36.pyc
3

���h�R�
@s�dZddlmZddlmZmZmZmZmZm	Z	ddl
Zddlj
Z
ddlmZddlmZmZddlmZmZmZmZddlmZmZdd	lmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$dd
l%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.ddl/m0Z0m1Z1m2Z2m3Z3ddl4m5Z5ddl6j7j8Z9e�r8dd
l:m;Z;ddl<m=Z=ddl>m?Z?d'eee@efeeeAdd�dd�ZBd(eAd�dd�ZCd)edeeeAeAed�dd�ZDeeeAeAd�dd�ZEeeAd �d!d"�ZFdddddde@feeeedfeeeeGeAeAed#d$�d%d&�ZHdS)*z�
Constructor functions intended to be shared by pd.array, Series.__init__,
and Index.__new__.

These should not depend on core.internals.
�)�abc)�
TYPE_CHECKING�Any�Optional�Sequence�Union�castN)�lib)�IncompatibleFrequency�OutOfBoundsDatetime)�AnyArrayLike�	ArrayLike�Dtype�DtypeObj)�ExtensionDtype�registry)	�"construct_1d_arraylike_from_scalar�"construct_1d_ndarray_preserving_na�'construct_1d_object_array_from_listlike�infer_dtype_from_scalar�maybe_cast_to_datetime�maybe_cast_to_integer_array�maybe_castable�maybe_convert_platform�maybe_upcast)	�is_datetime64_ns_dtype�is_extension_array_dtype�is_float_dtype�is_integer_dtype�is_iterator�is_list_like�is_object_dtype�	is_sparse�is_timedelta64_ns_dtype)�ABCExtensionArray�
ABCIndexClass�ABCPandasArray�	ABCSeries)�isna)�ExtensionArray)�Index)�SeriesTr))�data�dtype�copy�returnc
Csddlm}m}m}m}m}m}m}	m}
t	j
|�rFd|�d�}t|��|dkrdt|t
ttf�rd|j}t|dd�}t|t�r�tj|�p�|}t|�r�tt|�j�}|j|||d�S|dk�r�t	j|dd	�}
|
d
kr�y|
||d�Stk
r�YnXn�|
dk�r"y|||d�Stk
�rYnXn�|
jd
��rVy|j||d�Stk
�rRYnXnb|
jd��rp|	j||d�S|
dk�r�|j||d�S|
dk�r�|j||d�S|
dk�r�|j||d�St|��r�|j|||d�St|��r�|	j|||d�S|j|||d�}|S)a�
    Create an array.

    .. versionadded:: 0.24.0

    Parameters
    ----------
    data : Sequence of objects
        The scalars inside `data` should be instances of the
        scalar type for `dtype`. It's expected that `data`
        represents a 1-dimensional array of data.

        When `data` is an Index or Series, the underlying array
        will be extracted from `data`.

    dtype : str, np.dtype, or ExtensionDtype, optional
        The dtype to use for the array. This may be a NumPy
        dtype or an extension type registered with pandas using
        :meth:`pandas.api.extensions.register_extension_dtype`.

        If not specified, there are two possibilities:

        1. When `data` is a :class:`Series`, :class:`Index`, or
           :class:`ExtensionArray`, the `dtype` will be taken
           from the data.
        2. Otherwise, pandas will attempt to infer the `dtype`
           from the data.

        Note that when `data` is a NumPy array, ``data.dtype`` is
        *not* used for inferring the array type. This is because
        NumPy cannot represent all the types of data that can be
        held in extension arrays.

        Currently, pandas will infer an extension dtype for sequences of

        ============================== =====================================
        Scalar Type                    Array Type
        ============================== =====================================
        :class:`pandas.Interval`       :class:`pandas.arrays.IntervalArray`
        :class:`pandas.Period`         :class:`pandas.arrays.PeriodArray`
        :class:`datetime.datetime`     :class:`pandas.arrays.DatetimeArray`
        :class:`datetime.timedelta`    :class:`pandas.arrays.TimedeltaArray`
        :class:`int`                   :class:`pandas.arrays.IntegerArray`
        :class:`str`                   :class:`pandas.arrays.StringArray`
        :class:`bool`                  :class:`pandas.arrays.BooleanArray`
        ============================== =====================================

        For all other cases, NumPy's usual inference rules will be used.

        .. versionchanged:: 1.0.0

           Pandas infers nullable-integer dtype for integer data,
           string dtype for string data, and nullable-boolean dtype
           for boolean data.

    copy : bool, default True
        Whether to copy the data, even if not necessary. Depending
        on the type of `data`, creating the new array may require
        copying data, even if ``copy=False``.

    Returns
    -------
    ExtensionArray
        The newly created array.

    Raises
    ------
    ValueError
        When `data` is not 1-dimensional.

    See Also
    --------
    numpy.array : Construct a NumPy array.
    Series : Construct a pandas Series.
    Index : Construct a pandas Index.
    arrays.PandasArray : ExtensionArray wrapping a NumPy array.
    Series.array : Extract the array stored within a Series.

    Notes
    -----
    Omitting the `dtype` argument means pandas will attempt to infer the
    best array type from the values in the data. As new array types are
    added by pandas and 3rd party libraries, the "best" array type may
    change. We recommend specifying `dtype` to ensure that

    1. the correct array type for the data is returned
    2. the returned array type doesn't change as new extension types
       are added by pandas and third-party libraries

    Additionally, if the underlying memory representation of the returned
    array matters, we recommend specifying the `dtype` as a concrete object
    rather than a string alias or allowing it to be inferred. For example,
    a future version of pandas or a 3rd-party library may include a
    dedicated ExtensionArray for string data. In this event, the following
    would no longer return a :class:`arrays.PandasArray` backed by a NumPy
    array.

    >>> pd.array(['a', 'b'], dtype=str)
    <PandasArray>
    ['a', 'b']
    Length: 2, dtype: str32

    This would instead return the new ExtensionArray dedicated for string
    data. If you really need the new array to be backed by a  NumPy array,
    specify that in the dtype.

    >>> pd.array(['a', 'b'], dtype=np.dtype("<U1"))
    <PandasArray>
    ['a', 'b']
    Length: 2, dtype: str32

    Finally, Pandas has arrays that mostly overlap with NumPy

      * :class:`arrays.DatetimeArray`
      * :class:`arrays.TimedeltaArray`

    When data with a ``datetime64[ns]`` or ``timedelta64[ns]`` dtype is
    passed, pandas will always return a ``DatetimeArray`` or ``TimedeltaArray``
    rather than a ``PandasArray``. This is for symmetry with the case of
    timezone-aware data, which NumPy does not natively support.

    >>> pd.array(['2015', '2016'], dtype='datetime64[ns]')
    <DatetimeArray>
    ['2015-01-01 00:00:00', '2016-01-01 00:00:00']
    Length: 2, dtype: datetime64[ns]

    >>> pd.array(["1H", "2H"], dtype='timedelta64[ns]')
    <TimedeltaArray>
    ['0 days 01:00:00', '0 days 02:00:00']
    Length: 2, dtype: timedelta64[ns]

    Examples
    --------
    If a dtype is not specified, pandas will infer the best dtype from the values.
    See the description of `dtype` for the types pandas infers for.

    >>> pd.array([1, 2])
    <IntegerArray>
    [1, 2]
    Length: 2, dtype: Int64

    >>> pd.array([1, 2, np.nan])
    <IntegerArray>
    [1, 2, <NA>]
    Length: 3, dtype: Int64

    >>> pd.array(["a", None, "c"])
    <StringArray>
    ['a', <NA>, 'c']
    Length: 3, dtype: string

    >>> pd.array([pd.Period('2000', freq="D"), pd.Period("2000", freq="D")])
    <PeriodArray>
    ['2000-01-01', '2000-01-01']
    Length: 2, dtype: period[D]

    You can use the string alias for `dtype`

    >>> pd.array(['a', 'b', 'a'], dtype='category')
    ['a', 'b', 'a']
    Categories (2, object): ['a', 'b']

    Or specify the actual dtype

    >>> pd.array(['a', 'b', 'a'],
    ...          dtype=pd.CategoricalDtype(['a', 'b', 'c'], ordered=True))
    ['a', 'b', 'a']
    Categories (3, object): ['a' < 'b' < 'c']

    If pandas does not infer a dedicated extension type a
    :class:`arrays.PandasArray` is returned.

    >>> pd.array([1.1, 2.2])
    <PandasArray>
    [1.1, 2.2]
    Length: 2, dtype: float64

    As mentioned in the "Notes" section, new extension types may be added
    in the future (by pandas or 3rd party libraries), causing the return
    value to no longer be a :class:`arrays.PandasArray`. Specify the `dtype`
    as a NumPy dtype if you need to ensure there's no future change in
    behavior.

    >>> pd.array([1, 2], dtype=np.dtype("int32"))
    <PandasArray>
    [1, 2]
    Length: 2, dtype: int32

    `data` must be 1-dimensional. A ValueError is raised when the input
    has the wrong dimensionality.

    >>> pd.array(1)
    Traceback (most recent call last):
      ...
    ValueError: Cannot pass scalar '1' to 'pandas.array'.
    r)�BooleanArray�
DatetimeArray�IntegerArray�
IntervalArray�PandasArray�StringArray�TimedeltaArray�period_arrayzCannot pass scalar 'z' to 'pandas.array'.NT)�
extract_numpy)r-r.)�skipna�period)r.�interval�datetime�	timedelta�string�integer�boolean)�pandas.core.arraysr0r1r2r3r4r5r6r7r	�	is_scalar�
ValueError�
isinstancer'r%r$r-�
extract_array�strr�findrrr�construct_array_type�_from_sequence�infer_dtyper
�
startswithrr#)r,r-r.r0r1r2r3r4r5r6r7�msg�clsZinferred_dtype�result�rO�:/tmp/pip-build-5_djhm0z/pandas/pandas/core/construction.py�array9sZJ(









rQF)r8cCs.t|ttf�r|j}|r*t|t�r*|j�}|S)a6
    Extract the ndarray or ExtensionArray from a Series or Index.

    For all other types, `obj` is just returned as is.

    Parameters
    ----------
    obj : object
        For Series / Index, the underlying ExtensionArray is unboxed.
        For Numpy-backed ExtensionArrays, the ndarray is extracted.

    extract_numpy : bool, default False
        Whether to extract the ndarray from a PandasArray

    Returns
    -------
    arr : object

    Examples
    --------
    >>> extract_array(pd.Series(['a', 'b', 'c'], dtype='category'))
    ['a', 'b', 'c']
    Categories (3, object): ['a', 'b', 'c']

    Other objects like lists, arrays, and DataFrames are just passed through.

    >>> extract_array([1, 2, 3])
    [1, 2, 3]

    For an ndarray-backed Series / Index a PandasArray is returned.

    >>> extract_array(pd.Series([1, 2, 3]))
    <PandasArray>
    [1, 2, 3]
    Length: 3, dtype: int64

    To extract all the way down to the ndarray, pass ``extract_numpy=True``.

    >>> extract_array(pd.Series([1, 2, 3]), extract_numpy=True)
    array([1, 2, 3])
    )rDr%r'rQr&Zto_numpy)�objr8rOrOrPrESs
*rEr*)�indexr-r.�raise_cast_failurer/c
Cs�t|tj�rHtj|�}|j�r@t|dd�\}}|j�|||<n|j�}t|dd�}t|t	j
�r�|dk	r�t|j�r�t
|�r�yt|||d�}Wq�tk
r�|r�|j�}nt	j|dd�}Yq�Xnt||||�}�n<t|t��r|}|dk	r�|j||d�}n|�r|j�}|St|tttjtjf��r|t|�dk�r|t|t��rFtd��t|�}|dk	�rht||||�}nt|�}t||�}n�t|t��r�t	j|j|j |j!dd	�}t||||�}n^t"j#|��r|dk	�r|dk	�rt||�}t"j#|��s�|d}t$|t|�|�}nt||||�}t%|d
d�dk�r�t|t��r<t	j|t&d	�}nJ|dk	�r~|}	|dk�rbt'|	�\}}	n
t|	|�}	t$|	t|�|�}n|j(�Sn�|j)dk�r�|dk	�rt|�t|�k�rt|�dk�rt$|dt|�|j�}n2|j)dk�rt|t	j
��r�t*d��nt+j,||d	�}t-|j��pt-|��s�t.|jj/t0��rjt"j#|��sjt	j1t2|���sZt	j||dd
�}t	j|t&|d
�}t3|j��r�t3|��r�t"j4|dd�}
|
dk�r�t|�}|S)zw
    Sanitize input data to an ndarray or ExtensionArray, copy if specified,
    coerce to the dtype if specified.
    T)r.)r8NFrzSet type is unorderedZint64)r-�ndim�zData must be 1-dimensional)r-r.)r9r;r:>r;r:)5rD�maZMaskedArrayZgetmaskarray�anyrZsoften_maskr.rE�np�ndarrayrr-r�	_try_castrCrQr$Zastype�list�tupler�Set�
ValuesView�len�set�	TypeErrorrr�rangeZarange�start�stop�stepr	rBr�getattr�objectr�itemrU�	Exception�comZasarray_tuplesafer�
issubclass�typerF�allr(r!rJ)r,rSr-r.rT�maskZ
fill_value�subarr�arr�value�inferredrOrOrP�sanitize_array�s�


&
 





 

rt)r-r.rTcCs"t|tj�r&t|�r&|r&|dkr&|St|t�r^|jdksBt|�r^|j�j}||||d�}|Sylt	|�rxt
||�|}n
t||�}t|�r�t
|�r�t|�p�t|tj�r�t|�}nt|�s�t|||d�}WnRtk
r��Yn>ttfk
�r|dk	�r|�r�ntj|t|d�}YnX|S)a�
    Convert input to numpy ndarray and optionally cast to a given dtype.

    Parameters
    ----------
    arr : ndarray, scalar, list, tuple, iterator (catchall)
        Excludes: ExtensionArray, Series, Index.
    dtype : np.dtype, ExtensionDtype or None
    copy : bool
        If False, don't copy the data if not needed.
    raise_cast_failure : bool
        If True, and if a dtype is specified, raise errors during casting.
        Otherwise an object array is returned.
    N�M)r-r.)r.)rDrYrZrr�kindr"rHrIrrrr!r rrrrrrCrbrQrh)rqr-r.rTZ
array_typerprOrOrPr[s2



r[)r,r/cCs.|dk}t|�ot|d�}|o$|}|p,|S)a
    Utility to check if a Series is instantiated with empty data,
    which does not contain dtype information.

    Parameters
    ----------
    data : array-like, Iterable, dict, or scalar value
        Contains data stored in Series.

    Returns
    -------
    bool
    Nr-)r �hasattr)r,Zis_noneZis_list_like_without_dtypeZis_simple_emptyrOrOrP�
is_empty_dataDs
rxr+)r,rSr-�namer.�fastpath�dtype_if_emptyr/cCs4ddlm}t|�r |dkr |}|||||||d�S)ag
    Helper to pass an explicit dtype when instantiating an empty Series.

    This silences a DeprecationWarning described in GitHub-17261.

    Parameters
    ----------
    data : Mirrored from Series.__init__
    index : Mirrored from Series.__init__
    dtype : Mirrored from Series.__init__
    name : Mirrored from Series.__init__
    copy : Mirrored from Series.__init__
    fastpath : Mirrored from Series.__init__
    dtype_if_empty : str, numpy.dtype, or ExtensionDtype
        This dtype will be passed explicitly if an empty Series will
        be instantiated.

    Returns
    -------
    Series
    r)r+N)r,rSr-ryr.rz)�pandas.core.seriesr+rx)r,rSr-ryr.rzr{r+rOrOrP�!create_series_with_explicit_dtypeXs
r})NT)F)NFF)I�__doc__�collectionsrZtypingrrrrrrZnumpyrYZnumpy.marWZpandas._libsr	Zpandas._libs.tslibsr
rZpandas._typingrr
rrZpandas.core.dtypes.baserrZpandas.core.dtypes.castrrrrrrrrrZpandas.core.dtypes.commonrrrrrr r!r"r#Zpandas.core.dtypes.genericr$r%r&r'Zpandas.core.dtypes.missingr(Zpandas.core.common�core�commonrkrAr)Zpandas.core.indexes.apir*r|r+rh�boolrQrErtr[rxrFr}rOrOrOrP�<module>sJ 
,,6};&