ó
šxŠ\c           @   sI  d  Z  d d l Z d d l m Z m Z d d l m Z d d l Z d d l Z d d l	 m
 Z
 m Z d d l j Z d d l m Z m Z m Z d d l m Z d d l m Z m Z m Z m Z d d	 l m Z m Z m Z d d
 l m Z d d l m Z m  Z  m! Z! d e" f d „  ƒ  YZ# d e$ f d „  ƒ  YZ% d „  Z& d „  Z' d „  Z( d „  Z) e
 j* Z* d „  Z+ d „  Z, d „  Z- d „  Z. d „  Z/ d „  Z0 d „  Z1 d „  Z2 d „  Z3 d „  Z4 d d „ Z6 d d „ Z7 d  „  Z8 d! „  Z9 d" „  Z: d# „  Z; d$ „  Z< d% „  Z= d& „  Z> d' „  Z? d( „  Z@ d d) „ ZA d* „  ZB d+ „  ZC d S(,   sh   
Misc tools for implementing data structures

Note: pandas.core.common is *not* part of the public API.
iÿÿÿÿN(   t   datetimet	   timedelta(   t   partial(   t   libt   tslibs(   t   PY36t   OrderedDictt	   iteritems(   t'   construct_1d_object_array_from_listlike(   t   is_array_liket   is_bool_dtypet   is_extension_array_dtypet
   is_integer(   t   ABCIndext   ABCIndexClasst	   ABCSeries(   t   _iterable_not_string(   t   isnat   isnullt   notnullt   SettingWithCopyErrorc           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s1   lib/python2.7/site-packages/pandas/core/common.pyR      s   t   SettingWithCopyWarningc           B   s   e  Z RS(    (   R   R   (    (    (    s1   lib/python2.7/site-packages/pandas/core/common.pyR      s   c         c   sE   x> |  D]6 } t  | ƒ r8 x! t | ƒ D] } | Vq& Wq | Vq Wd S(   sü   Flatten an arbitrarily nested sequence.

    Parameters
    ----------
    l : sequence
        The non string sequence to flatten

    Notes
    -----
    This doesn't consider strings sequences.

    Returns
    -------
    flattened : generator
    N(   R   t   flatten(   t   lt   elt   s(    (    s1   lib/python2.7/site-packages/pandas/core/common.pyR   "   s
    c         C   s\   |  d j  } xH |  d D]< } y | j  | k r9 d  } n  Wq t k
 rS d  } q Xq W| S(   Ni    i   (   t   namet   Nonet
   ValueError(   t   objsR   t   obj(    (    s1   lib/python2.7/site-packages/pandas/core/common.pyt   consensus_name_attr:   s    c         C   s'   t  | t j ƒ r# | |  j | ƒ S| S(   N(   t
   isinstancet   npt   ndarrayt   get_loc(   t   indexert   valuesR    t   key(    (    s1   lib/python2.7/site-packages/pandas/core/common.pyt	   maybe_boxE   s    c         C   sX   t  |  t j t f ƒ r* t j |  ƒ }  n* t  |  t j t f ƒ rT t j |  ƒ }  n  |  S(   N(	   R"   R#   t
   datetime64R    R   t	   Timestampt   timedelta64R   t	   Timedelta(   t   value(    (    s1   lib/python2.7/site-packages/pandas/core/common.pyt   maybe_box_datetimelikeO   s
    c         C   sM  d } t  |  t t j t f ƒ s< t |  ƒ rç t |  j ƒ rç |  j t j k r› t j	 t
 |  ƒ ƒ }  t j |  ƒ s— t |  ƒ j ƒ  r“ t | ƒ ‚ n  t St St |  j ƒ rIt |  j ƒ rà t j |  j ƒ  ƒ rà t | ƒ ‚ qà n  t Snb t  |  t ƒ rIy; t j	 |  ƒ } | j t j k o/t | ƒ t |  ƒ k SWqIt k
 rEt SXn  t S(   s  
    Check whether `key` is a valid boolean indexer.

    Parameters
    ----------
    key : Any
        Only list-likes may be considered boolean indexers.
        All other types are not considered a boolean indexer.
        For array-like input, boolean ndarrays or ExtensionArrays
        with ``_is_boolean`` set are considered boolean indexers.

    Returns
    -------
    bool

    Raises
    ------
    ValueError
        When the array is an object-dtype ndarray or ExtensionArray
        and contains missing values.
    s3   cannot index with vector containing NA / NaN values(   R"   R   R#   R$   R   R	   R   t   dtypet   object_t   asarrayt   values_from_objectR   t   is_bool_arrayR   t   anyR   t   Falset   TrueR
   t   listt   bool_t   lent	   TypeError(   R(   t   na_msgt   arr(    (    s1   lib/python2.7/site-packages/pandas/core/common.pyt   is_bool_indexer]   s,    ,c         C   s/   t  j |  ƒ r+ |  t |  ƒ k r+ t |  ƒ S|  S(   s­   
    To avoid numpy DeprecationWarnings, cast float to integer where valid.

    Parameters
    ----------
    val : scalar

    Returns
    -------
    outval : scalar
    (   R   t   is_floatt   int(   t   val(    (    s1   lib/python2.7/site-packages/pandas/core/common.pyt   cast_scalar_indexer   s    !
c          G   s   d „  |  Dƒ S(   sA   Returns a generator consisting of the arguments that are not Nonec         s   s!   |  ] } | d  k	 r | Vq d  S(   N(   R   (   t   .0t   arg(    (    s1   lib/python2.7/site-packages/pandas/core/common.pys	   <genexpr>¤   s    (    (   t   args(    (    s1   lib/python2.7/site-packages/pandas/core/common.pyt	   _not_none¢   s    c          G   s%   x |  D] } | d k r t Sq Wt S(   s4   Returns a boolean indicating if any argument is NoneN(   R   R7   R6   (   RE   RD   (    (    s1   lib/python2.7/site-packages/pandas/core/common.pyt	   _any_none§   s    c          G   s%   x |  D] } | d k	 r t Sq Wt S(   s6   Returns a boolean indicating if all arguments are NoneN(   R   R6   R7   (   RE   RD   (    (    s1   lib/python2.7/site-packages/pandas/core/common.pyt	   _all_none¯   s    c          G   s%   x |  D] } | d k	 r t Sq Wt S(   s8   Returns a boolean indicating if any argument is not NoneN(   R   R7   R6   (   RE   RD   (    (    s1   lib/python2.7/site-packages/pandas/core/common.pyt   _any_not_none·   s    c          G   s%   x |  D] } | d k r t Sq Wt S(   s:   Returns a boolean indicating if all arguments are not NoneN(   R   R6   R7   (   RE   RD   (    (    s1   lib/python2.7/site-packages/pandas/core/common.pyt   _all_not_none¿   s    c          G   s   t  d „  |  Dƒ ƒ S(   s0   Returns the count of arguments that are not Nonec         s   s   |  ] } | d  k	 Vq d  S(   N(   R   (   RC   t   x(    (    s1   lib/python2.7/site-packages/pandas/core/common.pys	   <genexpr>É   s    (   t   sum(   RE   (    (    s1   lib/python2.7/site-packages/pandas/core/common.pyt   count_not_noneÇ   s    c         C   s3   t  |  ƒ } y t | ƒ SWn t k
 r. | SXd  S(   N(   R8   t   sortedt	   Exception(   t   iterablet   listed(    (    s1   lib/python2.7/site-packages/pandas/core/common.pyt   try_sortÌ   s
    c         C   s:   t  s t |  t ƒ r* t |  j ƒ  ƒ } n t |  ƒ } | S(   N(   R   R"   R   R8   t   keysRR   (   t   mappingRS   (    (    s1   lib/python2.7/site-packages/pandas/core/common.pyt   dict_keys_to_ordered_listÔ   s    c         C   sý   t  |  t t f ƒ p! t |  d ƒ s3 t |  ƒ }  n t  |  t ƒ rI |  j St  |  t ƒ rw | t j t g k rw t	 |  ƒ St j
 |  d | ƒ} t | j j t j ƒ r¼ t j
 |  d t ƒ} n  | j d k rù g  |  D] } t | ƒ ^ qÒ }  t	 |  ƒ } n  | S(   Nt	   __array__R0   i   (   R"   R8   t   tuplet   hasattrR   R'   R#   R1   t   objectR   R2   t
   issubclassR0   t   typet   compatt   string_typest   ndim(   R'   R0   t   resultRK   (    (    s1   lib/python2.7/site-packages/pandas/core/common.pyt   asarray_tuplesafeÞ   s    $$
c         C   s‚   t  |  t j t f ƒ r$ |  g }  n  t  |  t t j f ƒ sl y t |  ƒ }  Wql t k
 rh |  g }  ql Xn  t |  d | ƒ}  |  S(   së   
    Transform label or iterable of labels to array, for use in Index.

    Parameters
    ----------
    dtype : dtype
        If specified, use as dtype of the resulting array, otherwise infer.

    Returns
    -------
    array
    R0   (	   R"   R\   R]   RW   R8   R#   R$   R;   R`   (   t   labelsR0   (    (    s1   lib/python2.7/site-packages/pandas/core/common.pyt   index_labels_to_arrayö   s    c         C   s-   |  d  k	 r) t |  t t f ƒ r) |  g S|  S(   N(   R   R"   RW   R8   (   R    (    (    s1   lib/python2.7/site-packages/pandas/core/common.pyt   maybe_make_list  s    "c         C   s:   t  |  t ƒ o9 |  j d k o9 |  j d k o9 |  j d k S(   s    we have a null slice N(   R"   t   slicet   startR   t   stopt   step(   R    (    (    s1   lib/python2.7/site-packages/pandas/core/common.pyt   is_null_slice  s    c         C   s-   g  |  D]" } t  | t ƒ o& t | ƒ ^ q S(   sU   
    Find non-trivial slices in "l": return a list of booleans with same length.
    (   R"   Rd   Rh   (   R   t   k(    (    s1   lib/python2.7/site-packages/pandas/core/common.pyt   is_true_slices  s    c         C   s:   t  |  t ƒ o9 |  j d k o9 |  j | k o9 |  j d k S(   s    we have a full length slice i    N(   R"   Rd   Re   Rf   Rg   R   (   R    R   (    (    s1   lib/python2.7/site-packages/pandas/core/common.pyt   is_full_slice%  s    -c         C   sU   t  |  d ƒ r t |  d ƒ St |  t ƒ r8 t |  j ƒ St  |  d ƒ rQ |  j j Sd  S(   NR   t   __call__(	   RX   t   getattrR"   R   t   get_callable_namet   funct	   __class__R   R   (   R    (    (    s1   lib/python2.7/site-packages/pandas/core/common.pyRn   +  s    
c         K   s   t  |  ƒ r |  | |  S|  S(   s×   
    Evaluate possibly callable input using obj and kwargs if it is callable,
    otherwise return as it is

    Parameters
    ----------
    maybe_callable : possibly a callable
    obj : NDFrame
    **kwargs
    (   t   callable(   t   maybe_callableR    t   kwargs(    (    s1   lib/python2.7/site-packages/pandas/core/common.pyt   apply_if_callable<  s    c         C   s   d „  t  |  ƒ Dƒ S(   s¯   
    Helper function to convert datetimelike-keyed dicts to Timestamp-keyed dict

    Parameters
    ----------
    d: dict like object

    Returns
    -------
    dict

    c         S   s%   i  |  ] \ } } | t  | ƒ “ q S(    (   R/   (   RC   R(   R.   (    (    s1   lib/python2.7/site-packages/pandas/core/common.pys
   <dictcomp>[  s   	 (   R   (   t   d(    (    s1   lib/python2.7/site-packages/pandas/core/common.pyt   dict_compatN  s    c         C   s’   t  j |  ƒ sC t |  t j ƒ r4 t t j |  j ƒ St |  ƒ }  n  t |  t	 j
 ƒ sp t d j d |  ƒ ƒ ‚ n |  t j k rŽ t d ƒ ‚ n  |  S(   s'  
    Helper function to standardize a supplied mapping.

    .. versionadded:: 0.21.0

    Parameters
    ----------
    into : instance or subclass of collections.Mapping
        Must be a class, an initialized collections.defaultdict,
        or an instance of a collections.Mapping subclass.

    Returns
    -------
    mapping : a collections.Mapping subclass or other constructor
        a callable object that can accept an iterator to create
        the desired Mapping.

    See Also
    --------
    DataFrame.to_dict
    Series.to_dict
    s   unsupported type: {into}t   intos/   to_dict() only accepts initialized defaultdicts(   t   inspectt   isclassR"   t   collectionst   defaultdictR   t   default_factoryR[   RZ   R\   t   MappingR;   t   format(   Rw   (    (    s1   lib/python2.7/site-packages/pandas/core/common.pyt   standardize_mapping^  s    c          C   s   d t  f d „  ƒ  Y}  |  ƒ  S(   Nt   Sentinelc           B   s   e  Z RS(    (   R   R   (    (    (    s1   lib/python2.7/site-packages/pandas/core/common.pyR€   ƒ  s   (   RY   (   R€   (    (    s1   lib/python2.7/site-packages/pandas/core/common.pyt   sentinel_factory‚  s    c         C   sX   t  |  ƒ r t j j |  ƒ St |  t j j ƒ r5 |  S|  d k rH t j St d ƒ ‚ d S(   sÛ  
    Helper function for processing random_state arguments.

    Parameters
    ----------
    state : int, np.random.RandomState, None.
        If receives an int, passes to np.random.RandomState() as seed.
        If receives an np.random.RandomState object, just returns object.
        If receives `None`, returns np.random.
        If receives anything else, raises an informative ValueError.
        Default None.

    Returns
    -------
    np.random.RandomState
    s=   random_state must be an integer, a numpy RandomState, or NoneN(   R   R#   t   randomt   RandomStateR"   R   R   (   t   state(    (    s1   lib/python2.7/site-packages/pandas/core/common.pyt   random_state‰  s    c         O   sk   t  | t ƒ rW | \ } } | | k r@ d | } t | ƒ ‚ n  |  | | <| | | Ž  S| |  | | Ž Sd S(   sr  
    Apply a function ``func`` to object ``obj`` either by passing obj as the
    first argument to the function or, in the case that the func is a tuple,
    interpret the first element of the tuple as a function and pass the obj to
    that function as a keyword argument whose key is the value of the second
    element of the tuple.

    Parameters
    ----------
    func : callable or tuple of (callable, string)
        Function to apply to this object or, alternatively, a
        ``(callable, data_keyword)`` tuple where ``data_keyword`` is a
        string indicating the keyword of `callable`` that expects the
        object.
    args : iterable, optional
        positional arguments passed into ``func``.
    kwargs : dict, optional
        a dictionary of keyword arguments passed into ``func``.

    Returns
    -------
    object : the return type of ``func``.
    s1   %s is both the pipe target and a keyword argumentN(   R"   RW   R   (   R    Ro   RE   Rs   t   targett   msg(    (    s1   lib/python2.7/site-packages/pandas/core/common.pyt   _pipe¦  s    

c            s4   t  ˆ  t j t f ƒ r* ‡  f d †  } n ˆ  } | S(   sv   
    Returns a function that will map names/labels, dependent if mapper
    is a dict, Series or just a function.
    c            s   |  ˆ  k r ˆ  |  S|  Sd  S(   N(    (   RK   (   t   mapper(    s1   lib/python2.7/site-packages/pandas/core/common.pyt   fÐ  s    (   R"   R\   R}   R   (   R‰   RŠ   (    (   R‰   s1   lib/python2.7/site-packages/pandas/core/common.pyt   _get_rename_functionÉ  s    (D   t   __doc__Rz   R    R   t	   functoolsR   Rx   t   numpyR#   t   pandas._libsR   R   t   pandas.compatR\   R   R   R   t   pandas.core.dtypes.castR   t   pandas.core.dtypes.commonR	   R
   R   R   t   pandas.core.dtypes.genericR   R   R   t   pandas.core.dtypes.inferenceR   t   pandas.core.dtypes.missingR   R   R   R   R   t   WarningR   R   R!   R)   R/   R3   R>   RB   RF   RG   RH   RI   RJ   RM   RR   RU   R   R`   Rb   Rc   Rh   Rj   Rk   Rn   Rt   Rv   R   R   R…   Rˆ   R‹   (    (    (    s1   lib/python2.7/site-packages/pandas/core/common.pyt   <module>   sX   "			
			3									
								$		#