
x\c           @   s  d  Z  d d l m Z d d l m Z d d l Z d d l m Z d d l m	 Z	 d d l
 m Z d d l Z d d l m Z d d	 l m Z d d
 l m Z m Z d d l m Z d d l Z d d l m Z m Z d d l j j Z d d l  m! Z! d d l" m# Z# d d l$ m% Z% m& Z& y& d d l' m( Z( m) Z) m* Z* m+ Z+ Wn e, k
 rce, d   n Xy) d d l- j. Z/ d d l0 m1 Z1 e2 Z3 Wn e, k
 re4 Z3 d Z5 n Xe d    Z6 d e7 f d     YZ8 d   Z9 e: d  Z; d   Z< d S(   sF   
Module for applying conditional formatting to
DataFrames and Series.
i(   t   defaultdict(   t   contextmanagerN(   t   partial(   t   product(   t   uuid1(   t   range(   t   Appender(   t   is_floatt   is_string_like(   t	   ABCSeries(   t   is_dict_liket   is_list_like(   t
   get_option(   t   _shared_docs(   t   _maybe_numeric_slicet   _non_reducing_slice(   t   PackageLoadert   Environmentt   ChoiceLoadert   FileSystemLoadersa   pandas.Styler requires jinja2. Please install with `conda install Jinja2`
or `pip install Jinja2`(   t   colorss   {0} requires matplotlib.c         c   s0   t  r t t f Vn t t j |  j    d  S(   N(   t   has_mplt   pltR   t   ImportErrort   no_mpl_messaget   formatt   __name__(   t   func(    (    s6   lib/python2.7/site-packages/pandas/io/formats/style.pyt   _mpl/   s    t   Stylerc           B   s  e  Z d  Z e d d  Z e d e d e  Z e j d  Z	 dC dC dC dC dC e d  Z d   Z e e d e d	 d
 d d d d d d d d   d d dC dC e e dC d d dC e dC d e dC d   Z d   Z dC d  Z d   Z d   Z e d  Z d   Z d   Z d   Z d    Z d dC d!  Z d dC d"  Z dC d#  Z dC d$  Z dC dC d%  Z d&   Z  d'   Z! d(   Z" d)   Z# d*   Z$ d+   Z% d,   Z& d-   Z' d.   Z( e) d/    Z* d0 d1  Z+ d2 d d d dC d3 d4  Z, e) d2 d d d3 d5   Z- dC d6  Z. e) d7 dC dC d8   Z/ dC d d9 d7 d: dC dC d;  Z0 dC d< d d=  Z1 dC d< d d>  Z2 dC d< dC e d?  Z3 e) d< e d@   Z4 e5 dA    Z6 dB   Z7 RS(D   s  
    Helps style a DataFrame or Series according to the data with HTML and CSS.

    Parameters
    ----------
    data : Series or DataFrame
    precision : int
        precision to round floats to, defaults to pd.options.display.precision
    table_styles : list-like, default None
        list of {selector: (attr, value)} dicts; see Notes
    uuid : str, default None
        a unique identifier to avoid CSS collisions; generated automatically
    caption : str, default None
        caption to attach to the table
    cell_ids : bool, default True
        If True, each cell will have an ``id`` attribute in their HTML tag.
        The ``id`` takes the form ``T_<uuid>_row<num_row>_col<num_col>``
        where ``<uuid>`` is the unique identifier, ``<num_row>`` is the row
        number and ``<num_col>`` is the column number.

    Attributes
    ----------
    env : Jinja2 Environment
    template : Jinja2 Template
    loader : Jinja2 Loader

    See Also
    --------
    pandas.DataFrame.style

    Notes
    -----
    Most styling will be done by passing style functions into
    ``Styler.apply`` or ``Styler.applymap``. Style functions should
    return values with strings containing CSS ``'attr: value'`` that will
    be applied to the indicated cells.

    If using in the Jupyter notebook, Styler has defined a ``_repr_html_``
    to automatically render itself. Otherwise call Styler.render to get
    the generated HTML.

    CSS classes are attached to the generated HTML

    * Index and Column names include ``index_name`` and ``level<k>``
      where `k` is its level in a MultiIndex
    * Index label cells include

      * ``row_heading``
      * ``row<n>`` where `n` is the numeric position of the row
      * ``level<k>`` where `k` is the level in a MultiIndex

    * Column label cells include
      * ``col_heading``
      * ``col<n>`` where `n` is the numeric position of the column
      * ``evel<k>`` where `k` is the level in a MultiIndex

    * Blank cells include ``blank``
    * Data cells include ``data``
    t   pandass   io/formats/templatest   loadert   trim_blockss   html.tplc            s8  t  t   _ g   _ t | t j t j f  sB t d   n  | j	 d k r` | j
   } n  | j j sz | j j r t d   n  |  _ | j  _ | j  _ |  _ |  _ |  _ | d  k r t d  } n  |  _ |  _ t  _ g   _ |  _  f d     t    f d     _ d  S(   Ns&   ``data`` must be a Series or DataFramei   s.   style is not supported for non-unique indices.s   display.precisionc            s*   t  |   r" d j |  d   j S|  Sd  S(   Ns   {:>.{precision}g}t	   precision(   R   R   R!   (   t   x(   t   self(    s6   lib/python2.7/site-packages/pandas/io/formats/style.pyt   default_display_func   s    c              s     S(   N(    (    (   R$   (    s6   lib/python2.7/site-packages/pandas/io/formats/style.pyt   <lambda>   s    (   R    t   listt   ctxt   _todot
   isinstancet   pdt   Seriest	   DataFramet	   TypeErrort   ndimt   to_framet   indext	   is_uniquet   columnst
   ValueErrort   datat   uuidt   table_stylest   captiont   NoneR   R!   t   table_attributest   Falset   hidden_indext   hidden_columnst   cell_idst   _display_funcs(   R#   R4   R!   R6   R5   R7   R9   R=   (    (   R$   R#   s6   lib/python2.7/site-packages/pandas/io/formats/style.pyt   __init__z   s.    										c         C   s
   |  j    S(   sB   
        Hooks into Jupyter notebook rich display system.
        (   t   render(   R#   (    (    s6   lib/python2.7/site-packages/pandas/io/formats/style.pyt   _repr_html_   s    t   to_excelt   axess   index, columnst   klassR   t   axes_single_args   {0 or 'index', 1 or 'columns'}t   optional_bysi   
            by : str or list of str
                Name or list of names which refer to the axis items.t   versionadded_to_excels   
    .. versionadded:: 0.20t   Sheet1t    i    t   infc         C   s{   d d l  m } | |  d | d | d | d | d | d | d	 | d
 | } | j | d | d |	 d |
 d | d | d  S(   Ni(   t   ExcelFormattert   na_rept   colst   headert   float_formatR0   t   index_labelt   merge_cellst   inf_rept
   sheet_namet   startrowt   startcolt   freeze_panest   engine(   t   pandas.io.formats.excelRK   t   write(   R#   t   excel_writerRS   RL   RO   R2   RN   R0   RP   RT   RU   RW   RQ   t   encodingRR   t   verboseRV   RK   t	   formatter(    (    s6   lib/python2.7/site-packages/pandas/io/formats/style.pyRB      s    	c   ,      C   s  |  j  p g  } |  j } |  j } |  j } |  j } |  j } |  j p] t t    j	 d d  } d } d }	 d }
 d } d } d } d	   } t
 |  j  } t
 |  j |  } t   } |  j j j } |  j j j } |  j j j   } |  j j j   } | d
 k r$g  | D] } | g ^ q} n  | d
 k rOg  | D] } | g ^ q7} n  t t |    } g  } g  } xt |  D]} i d d 6| d 6| d 6| d 6d j | g  d 6g | d
 } |  j j j | } | d0 k r| n |
 d j d |  g } | d0 k r| n | } | j i d d 6| d 6| d 6d j |  d 6| d 6 | rzx	t | |  D] \ } } |	 d j d |  d j d |  g } | j | j d i   j | i   j | g    i d d 6| d 6| d 6d j |  d 6t | | |  d 6} | j | | f d  }  |  d
 k rQ| i d d 6|  d 6 g | d <n  | j |  qgW| j |  qzqzW|  j j j rt j |  j j j   r| rg  }! x~ t |  j j j  D]g \ } } |
 d j d |  g } | d0 k rd n | } |! j i d d 6| d 6d j |  d 6 qW|! j i d d 6| d 6d j | g  d 6g t | d  t |   | j |!  n  g  }" xt |  j j  D]\ } }# g  } x t | |  D] \ } } | d j d |  d j d |  g }$ i d d 6t | | |  o| d 6| d 6| d 6d j |$ d
  d 6d j |$  d 6} | j | | f d  }% |% d
 k r| i d d 6|% d 6 g | d <n  | j |  qWxt |  j j  D]\ } }& | d j d |  d j d |  g } | j | j d i   j | i   j | g    |  j  | | f }' |  j j! | | f } i d d 6| d 6d j |  d 6|' |  d 6| | k d 6}( |  j" st | | | f  d
 k o| | | f d d k rd j | d
  |( d <n  | j |(  g  }) xS | | | f D]A } | j# d   r/|) j | j$ d    q|) j d d g  qW| j i |) d! 6d" j d | d |  d# 6 qW|" j |  qW|  j% }* t& d$  }+ |+ s|* pd }* d% |* k r|* j	 d% d&  }* q|* d' 7}* n  t d( | d) | d* |" d+ | d, | d- | d. | d/ |*  S(1   s   
        Convert the DataFrame in `self.data` and the attrs from `_build_styles`
        into a dictionary of {head, body, uuid, cellstyle}.
        t   -t   _t   row_headingt   col_headingt
   index_nameR4   t   blankRI   c         S   s   d j  |    S(   Ns   {key}={value}(   R   (   t   pair(    (    s6   lib/python2.7/site-packages/pandas/io/formats/style.pyt   format_attr   s    i   t   tht   typet   valuet   display_valuet
   is_visiblet    t   classs
   level{lvl}t   lvls   col{col}t   colt   col_headingsi    t   colspant   keyt
   attributess   row{row}t   rowt   idt   rowspant   tdt   :t   propss   row{row}_col{col}t   selectors   display.html.use_mathjaxs   class="s   class="tex2jax_ignore s    class="tex2jax_ignore"t   headt	   cellstylet   bodyR5   R!   R6   R7   R9   N('   R6   R7   R'   R!   R;   R<   R5   t   strR   t   replacet   _get_level_lengthsR0   R2   t   dictR4   t   nlevelst   tolistR&   t   zipR   t   joint   namesR8   R   t   appendt	   enumeratet   extendt   gett   _is_visiblet   comt   _any_not_nonet   lenR>   t   ilocR=   t   countt   splitR9   R   (,   R#   R6   R7   R'   R!   R;   R<   R5   t   ROW_HEADING_CLASSt   COL_HEADING_CLASSt   INDEX_NAME_CLASSt
   DATA_CLASSt   BLANK_CLASSt   BLANK_VALUERe   t   idx_lengthst   col_lengthst   cell_contextt   n_rlvlst   n_clvlst   rlabelst   clabelsR"   R{   Rz   t   rt   row_est   namet   cst   cRh   t   esRp   t   index_header_rowR|   t   idxt   ridRu   Rn   R]   t   row_dictRx   t
   table_attrt   use_mathjax(    (    s6   lib/python2.7/site-packages/pandas/io/formats/style.pyt
   _translate   s   					$		
!%$"
$1
	7			c         C   ss  | d k r< t t |  j   } t t |  j j   } ns t |  } t |  d k ro | |  j j f } n  |  j j | } |  j j j | j  } |  j j j | j  } t	 |  r'x | j
   D]X \ } } t |  } |  j j j | g  d } x! | D] }	 | |  j |	 | f <qWq WnH t | | f   }
 x3 |
 D]+ \ } } t |  } | |  j | | f <q@W|  S(   s  
        Format the text display value of cells.

        .. versionadded:: 0.18.0

        Parameters
        ----------
        formatter : str, callable, or dict
        subset : IndexSlice
            An argument to ``DataFrame.loc`` that restricts which elements
            ``formatter`` is applied to.

        Returns
        -------
        self : Styler

        Notes
        -----

        ``formatter`` is either an ``a`` or a dict ``{column name: a}`` where
        ``a`` is one of

        - str: this will be wrapped in: ``a.format(x)``
        - callable: called with the value of an individual cell

        The default display value for numeric values is the "general" (``g``)
        format with ``pd.options.display.precision`` precision.

        Examples
        --------

        >>> df = pd.DataFrame(np.random.randn(4, 2), columns=['a', 'b'])
        >>> df.style.format("{:.2%}")
        >>> df['c'] = ['a', 'b', 'c', 'd']
        >>> df.style.format({'c': str.upper})
        i   i    N(   R8   R   R   R4   R2   R   t   locR0   t   get_indexer_forR
   t   itemst   _maybe_wrap_formatterR>   R   (   R#   R]   t   subsett   row_locst   col_locst   sub_dfRn   t   col_formattert   col_numt   row_numt   locst   it   j(    (    s6   lib/python2.7/site-packages/pandas/io/formats/style.pyR   d  s(    %c         K   st   |  j    |  j   } g  | d D]& } t d   | d D  r! | ^ q! } | | d <| j |  |  j j |   S(   s:  
        Render the built up styles to HTML.

        Parameters
        ----------
        `**kwargs` : Any additional keyword arguments are passed through
        to ``self.template.render``. This is useful when you need to provide
        additional variables for a custom template.

            .. versionadded:: 0.20

        Returns
        -------
        rendered : str
            the rendered HTML

        Notes
        -----
        ``Styler`` objects have defined the ``_repr_html_`` method
        which automatically calls ``self.render()`` when it's the
        last item in a Notebook cell. When calling ``Styler.render()``
        directly, wrap the result in ``IPython.display.HTML`` to view
        the rendered HTML in the notebook.

        Pandas uses the following keys in render. Arguments passed
        in ``**kwargs`` take precedence, so think carefully if you want
        to override them:

        * head
        * cellstyle
        * body
        * uuid
        * precision
        * table_styles
        * caption
        * table_attributes
        R{   c         s   s   |  ] } t  |  Vq d  S(   N(   t   any(   t   .0t   y(    (    s6   lib/python2.7/site-packages/pandas/io/formats/style.pys	   <genexpr>  s    Rx   (   t   _computeR   R   t   updatet   templateR@   (   R#   t   kwargst   dR"   t   trimmed(    (    s6   lib/python2.7/site-packages/pandas/io/formats/style.pyR@     s    &
&
c   	      C   s   x | j    D] \ } } x | j   D]{ \ } } |  j j | g  d } |  j j | g  d } x: | j d  j d  D]  } |  j | | f j |  q} Wq& Wq Wd S(   s>  
        Update the state of the Styler.

        Collects a mapping of {index_label: ['<property>: <value>']}.

        attrs : Series or DataFrame
        should contain strings of '<property>: <value>;<prop2>: <val2>'
        Whitespace shouldn't matter and the final trailing ';' shouldn't
        matter.
        i    t   ;N(	   t   iterrowst	   iteritemsR0   t   get_indexerR2   t   rstripR   R'   R   (	   R#   t   attrst	   row_labelt   vt	   col_labelRn   R   R   Rd   (    (    s6   lib/python2.7/site-packages/pandas/io/formats/style.pyt   _update_ctx  s    c      
   C   s   t  |  j d |  j d |  j d |  j d |  j } | rf t j |  j  | _ t j |  j	  | _	 n |  j | _ |  j	 | _	 | S(   NR!   R7   R5   R6   (
   R   R4   R!   R7   R5   R6   t   copyt   deepcopyR'   R(   (   R#   R   t   styler(    (    s6   lib/python2.7/site-packages/pandas/io/formats/style.pyt   _copy  s    c         C   s   |  j  d t  S(   s'   
        Deep copy by default.
        R   (   R   R:   (   R#   (    (    s6   lib/python2.7/site-packages/pandas/io/formats/style.pyt   __copy__  s    c         C   s   |  j  d t  S(   NR   (   R   t   True(   R#   t   memo(    (    s6   lib/python2.7/site-packages/pandas/io/formats/style.pyt   __deepcopy__  s    c         C   s   |  j  j   g  |  _ d S(   sa   
        Reset the styler, removing any previously applied styles.
        Returns None.
        N(   R'   t   clearR(   (   R#   (    (    s6   lib/python2.7/site-packages/pandas/io/formats/style.pyR     s    c         C   s<   |  } x/ |  j  D]$ \ } } } | |   | |   } q W| S(   s  
        Execute the style functions built up in `self._todo`.

        Relies on the conventions that all style functions go through
        .apply or .applymap. The append styles to apply as tuples of

        (application method, *args, **kwargs)
        (   R(   (   R#   R   R   t   argsR   (    (    s6   lib/python2.7/site-packages/pandas/io/formats/style.pyR     s    	c   
      K   sc  | d  k r t d   n | } t |  } |  j j | } | d  k	 rs | j | d | d d | } | j | _ n | | |  } t | t j	  s t
 d j d |    n  | j j | j  o | j j | j  s d j d |  } t |   n  | j } |  j j | j }	 | |	 k rRd j d | d | j d	 |	  } t |   n  |  j |  |  S(
   Nt   axist   result_typet   expandsV   Function {func!r} must return a DataFrame when passed to `Styler.apply` with axis=NoneR   sE   Result of {func!r} must have identical index and columns as the inputs^   Function {func!r} returned the wrong shape.
Result has shape: {res}
Expected shape:   {expect}t   rest   expect(   R8   t   sliceR   R4   R   t   applyR2   R)   R*   R,   R-   R   R0   t   equalsR3   t   shapeR   (
   R#   R   R   R   R   R4   t   resultt   msgt   result_shapet   expected_shape(    (    s6   lib/python2.7/site-packages/pandas/io/formats/style.pyt   _apply  s6    							c         K   s)   |  j  j d   | | | f | f  |  S(   sP  
        Apply a function column-wise, row-wise, or table-wise,
        updating the HTML representation with the result.

        Parameters
        ----------
        func : function
            ``func`` should take a Series or DataFrame (depending
            on ``axis``), and return an object with the same shape.
            Must return a DataFrame with identical index and
            column labels when ``axis=None``
        axis : int, str or None
            apply to each column (``axis=0`` or ``'index'``)
            or to each row (``axis=1`` or ``'columns'``) or
            to the entire DataFrame at once with ``axis=None``
        subset : IndexSlice
            a valid indexer to limit ``data`` to *before* applying the
            function. Consider using a pandas.IndexSlice
        kwargs : dict
            pass along to ``func``

        Returns
        -------
        self : Styler

        Notes
        -----
        The output shape of ``func`` should match the input, i.e. if
        ``x`` is the input row, column, or table (depending on ``axis``),
        then ``func(x).shape == x.shape`` should be true.

        This is similar to ``DataFrame.apply``, except that ``axis=None``
        applies the function to the entire DataFrame at once,
        rather than column-wise or row-wise.

        Examples
        --------
        >>> def highlight_max(x):
        ...     return ['background-color: yellow' if v == x.max() else ''
                        for v in x]
        ...
        >>> df = pd.DataFrame(np.random.randn(5, 2))
        >>> df.style.apply(highlight_max)
        c         S   s   t  |  d  S(   NR   (   t   getattr(   t   instance(    (    s6   lib/python2.7/site-packages/pandas/io/formats/style.pyR%   b  s    (   R(   R   (   R#   R   R   R   R   (    (    s6   lib/python2.7/site-packages/pandas/io/formats/style.pyR   5  s    -c         K   s^   t  | |  } | d  k r( t j } n  t |  } |  j j | j |  } |  j |  |  S(   N(	   R   R8   R*   t
   IndexSliceR   R4   R   t   applymapR   (   R#   R   R   R   R   (    (    s6   lib/python2.7/site-packages/pandas/io/formats/style.pyt	   _applymapf  s    c         K   s&   |  j  j d   | | f | f  |  S(   s*  
        Apply a function elementwise, updating the HTML
        representation with the result.

        Parameters
        ----------
        func : function
            ``func`` should take a scalar and return a scalar
        subset : IndexSlice
            a valid indexer to limit ``data`` to *before* applying the
            function. Consider using a pandas.IndexSlice
        kwargs : dict
            pass along to ``func``

        Returns
        -------
        self : Styler

        See Also
        --------
        Styler.where
        c         S   s   t  |  d  S(   NR   (   R   (   R   (    (    s6   lib/python2.7/site-packages/pandas/io/formats/style.pyR%     s    (   R(   R   (   R#   R   R   R   (    (    s6   lib/python2.7/site-packages/pandas/io/formats/style.pyR   o  s    c            s:    d k r d  n  |  j     f d   d | | S(   s   
        Apply a function elementwise, updating the HTML
        representation with a style which is selected in
        accordance with the return value of a function.

        .. versionadded:: 0.21.0

        Parameters
        ----------
        cond : callable
            ``cond`` should take a scalar and return a boolean
        value : str
            applied when ``cond`` returns true
        other : str
            applied when ``cond`` returns false
        subset : IndexSlice
            a valid indexer to limit ``data`` to *before* applying the
            function. Consider using a pandas.IndexSlice
        kwargs : dict
            pass along to ``cond``

        Returns
        -------
        self : Styler

        See Also
        --------
        Styler.applymap
        RI   c            s     |   r  S S(   N(    (   t   val(   t   condt   otherRh   (    s6   lib/python2.7/site-packages/pandas/io/formats/style.pyR%     s    R   N(   R8   R   (   R#   R   Rh   R   R   R   (    (   R   R   Rh   s6   lib/python2.7/site-packages/pandas/io/formats/style.pyt   where  s    	c         C   s   | |  _  |  S(   s   
        Set the precision used to render.

        Parameters
        ----------
        precision : int

        Returns
        -------
        self : Styler
        (   R!   (   R#   R!   (    (    s6   lib/python2.7/site-packages/pandas/io/formats/style.pyt   set_precision  s    	c         C   s   | |  _  |  S(   s  
        Set the table attributes.

        These are the items that show up in the opening ``<table>`` tag
        in addition to to automatic (by default) id.

        Parameters
        ----------
        attributes : string

        Returns
        -------
        self : Styler

        Examples
        --------
        >>> df = pd.DataFrame(np.random.randn(10, 4))
        >>> df.style.set_table_attributes('class="pure-table"')
        # ... <table class="pure-table"> ...
        (   R9   (   R#   Rr   (    (    s6   lib/python2.7/site-packages/pandas/io/formats/style.pyt   set_table_attributes  s    	c         C   s   |  j  S(   s   
        Export the styles to applied to the current Styler.

        Can be applied to a second style with ``Styler.use``.

        Returns
        -------
        styles : list

        See Also
        --------
        Styler.use
        (   R(   (   R#   (    (    s6   lib/python2.7/site-packages/pandas/io/formats/style.pyt   export  s    c         C   s   |  j  j |  |  S(   s>  
        Set the styles on the current Styler, possibly using styles
        from ``Styler.export``.

        Parameters
        ----------
        styles : list
            list of style functions

        Returns
        -------
        self : Styler

        See Also
        --------
        Styler.export
        (   R(   R   (   R#   t   styles(    (    s6   lib/python2.7/site-packages/pandas/io/formats/style.pyt   use  s    c         C   s   | |  _  |  S(   s   
        Set the uuid for a Styler.

        Parameters
        ----------
        uuid : str

        Returns
        -------
        self : Styler
        (   R5   (   R#   R5   (    (    s6   lib/python2.7/site-packages/pandas/io/formats/style.pyt   set_uuid  s    	c         C   s   | |  _  |  S(   s   
        Set the caption on a Styler

        Parameters
        ----------
        caption : str

        Returns
        -------
        self : Styler
        (   R7   (   R#   R7   (    (    s6   lib/python2.7/site-packages/pandas/io/formats/style.pyt   set_caption
  s    	c         C   s   | |  _  |  S(   sC  
        Set the table styles on a Styler.

        These are placed in a ``<style>`` tag before the generated HTML table.

        Parameters
        ----------
        table_styles : list
            Each individual table_style should be a dictionary with
            ``selector`` and ``props`` keys. ``selector`` should be a CSS
            selector that the style will be applied to (automatically
            prefixed by the table's UUID) and ``props`` should be a list of
            tuples with ``(attribute, value)``.

        Returns
        -------
        self : Styler

        Examples
        --------
        >>> df = pd.DataFrame(np.random.randn(10, 4))
        >>> df.style.set_table_styles(
        ...     [{'selector': 'tr:hover',
        ...       'props': [('background-color', 'yellow')]}]
        ... )
        (   R6   (   R#   R6   (    (    s6   lib/python2.7/site-packages/pandas/io/formats/style.pyt   set_table_styles  s    	c         C   s   t  |  _ |  S(   s   
        Hide any indices from rendering.

        .. versionadded:: 0.23.0

        Returns
        -------
        self : Styler
        (   R   R;   (   R#   (    (    s6   lib/python2.7/site-packages/pandas/io/formats/style.pyt
   hide_index7  s    
	c         C   s8   t  |  } |  j j | } |  j j | j  |  _ |  S(   s-  
        Hide columns from rendering.

        .. versionadded:: 0.23.0

        Parameters
        ----------
        subset : IndexSlice
            An argument to ``DataFrame.loc`` that identifies which columns
            are hidden.

        Returns
        -------
        self : Styler
        (   R   R4   R   R2   R   R<   (   R#   R   t	   hidden_df(    (    s6   lib/python2.7/site-packages/pandas/io/formats/style.pyt   hide_columnsD  s    c         C   s#   t  j |   r d j d |  Sd S(   Ns   background-color: {color}t   colorRI   (   R*   t   isnaR   (   R   t
   null_color(    (    s6   lib/python2.7/site-packages/pandas/io/formats/style.pyt   _highlight_null]  s    t   redc         C   s   |  j  |  j d | |  S(   s   
        Shade the background ``null_color`` for missing values.

        Parameters
        ----------
        null_color : str

        Returns
        -------
        self : Styler
        R   (   R   R   (   R#   R   (    (    s6   lib/python2.7/site-packages/pandas/io/formats/style.pyt   highlight_nullb  s    t   PuBug&1?c         C   sV   t  |  j |  } t |  } |  j |  j d | d | d | d | d | d | |  S(   s  
        Color the background in a gradient according to
        the data in each column (optionally row).

        Requires matplotlib.

        Parameters
        ----------
        cmap : str or colormap
            matplotlib colormap
        low, high : float
            compress the range by these values.
        axis : int or str
            1 or 'columns' for columnwise, 0 or 'index' for rowwise
        subset : IndexSlice
            a valid slice for ``data`` to limit the style application to
        text_color_threshold : float or int
            luminance threshold for determining text color. Facilitates text
            visibility across varying background colors. From 0 to 1.
            0 = all text is dark colored, 1 = all text is light colored.

            .. versionadded:: 0.24.0

        Returns
        -------
        self : Styler

        Raises
        ------
        ValueError
            If ``text_color_threshold`` is not a value from 0 to 1.

        Notes
        -----
        Set ``text_color_threshold`` or tune ``low`` and ``high`` to keep the
        text legible by not using the entire range of the color map. The range
        of the data is extended by ``low * (x.max() - x.min())`` and ``high *
        (x.max() - x.min())`` before normalizing.
        t   cmapR   R   t   lowt   hight   text_color_threshold(   R   R4   R   R   t   _background_gradient(   R#   R   R   R  R   R   R  (    (    s6   lib/python2.7/site-packages/pandas/io/formats/style.pyt   background_gradientq  s    )c            sj  t   t t f  s3 d  k o- d k n rH d } t |   n  t t j  \ }   |  j j   } |  j j	   } | | }	   j
 | |	 | | |	 |  }
 | j j |  |
 |  j   } d        f d   } |  j d k rg  | D] } | |  ^ q St j g  | D]% } g  | D] } | |  ^ q/^ q"d |  j d |  j SWd QXd S(	   sD   
        Color background in a range according to the data.
        i    i   s3   `text_color_threshold` must be a value from 0 to 1.c         S   s5   d   |  d  D \ } } } d | d | d | S(   s  
                Calculate relative luminance of a color.

                The calculation adheres to the W3C standards
                (https://www.w3.org/WAI/GL/wiki/Relative_luminance)

                Parameters
                ----------
                color : rgb or rgba tuple

                Returns
                -------
                float
                    The relative luminance as a value from 0 to 1
                c         s   s3   |  ]) } | d  k r | d n | d d Vq d S(   g#?gףp=
)@g)\(?gzG?g333333@Ng61?(    (   R   R"   (    (    s6   lib/python2.7/site-packages/pandas/io/formats/style.pys	   <genexpr>  s   i   gz6?g,C?g]m{?(    (   t   rgbaR   t   gt   b(    (    s6   lib/python2.7/site-packages/pandas/io/formats/style.pyt   relative_luminance  s    c            sC    |    k  } | r d n d } d j  d   j |   d |  S(   Ns   #f1f1f1s   #000000s!   background-color: {b};color: {c};R  R   (   R   t   rgb2hex(   R  t   darkt
   text_color(   R   R  R  (    s6   lib/python2.7/site-packages/pandas/io/formats/style.pyt   css  s    	R0   R2   N(   R)   t   floatt   intR3   R   R   R  t   valuest   mint   maxt	   Normalizet   cmt   get_cmapR.   R*   R,   R0   R2   (   t   sR   R   R  R  R   R   t   smint   smaxt   rngt   normt   rgbasR  R  Rs   (    (   R   R  R  s6   lib/python2.7/site-packages/pandas/io/formats/style.pyR    s"    
"!	2c            sA   d j  d   | j   D      f d   } |  j | d | S(   sd  
        Convenience method for setting one or more non-data dependent
        properties or each cell.

        Parameters
        ----------
        subset : IndexSlice
            a valid slice for ``data`` to limit the style application to
        kwargs : dict
            property: value pairs to be set for each cell

        Returns
        -------
        self : Styler

        Examples
        --------
        >>> df = pd.DataFrame(np.random.randn(10, 4))
        >>> df.style.set_properties(color="white", align="right")
        >>> df.style.set_properties(**{'background-color': 'yellow'})
        R   c         s   s-   |  ]# \ } } d  j  d | d |  Vq d S(   s   {p}: {v}t   pR   N(   R   (   R   R  R   (    (    s6   lib/python2.7/site-packages/pandas/io/formats/style.pys	   <genexpr>  s   c            s     S(   N(    (   R"   (   R  (    s6   lib/python2.7/site-packages/pandas/io/formats/style.pyR%     s    R   (   R   R   R   (   R#   R   R   t   f(    (   R  s6   lib/python2.7/site-packages/pandas/io/formats/style.pyt   set_properties  s    id   c            s  | d
 k r |  j   n | } t | t  r< | j   } n  | d
 k rT |  j   n | } t | t  rx | j   } n    d k r t d |  } t d |  } n1   d k r t t |  t |   } | } n   |  j | | | d }  | | | d   f d         f d   }	 |  j d k r]g  | D] }
 |	 |
  ^ qGSt j	 g  | D]% } g  | D] }
 |	 |
  ^ qw^ qjd |  j
 d	 |  j Sd
 S(   s4   
        Draw bar chart in dataframe cells.
        t   midi    t   zerog-q=c            ss   d } | |  k ro | d 7} |  d k rG | d j  d |  d |  7} n  | d j  d t |    d |  7} n  | S(	   sL   
            Generate CSS code to draw a bar from start to end.
            s   width: 10em; height: 80%;s"   background: linear-gradient(90deg,i    s%    transparent {s:.1f}%, {c} {s:.1f}%, R  R   s#   {c} {e:.1f}%, transparent {e:.1f}%)t   e(   R   R  (   t   startt   endR   R  (   t   width(    s6   lib/python2.7/site-packages/pandas/io/formats/style.pyt   css_bar  s    
c            su   t  j |   r d S|   k r)  d n  d }   d k rO  d |  |  S t |    t |    |  Sd  S(   NRI   i   i    t   left(   R*   R   R  R  (   R"   R   (   t   alignR   R$  R  (    s6   lib/python2.7/site-packages/pandas/io/formats/style.pyR    s     i   R0   R2   N(   R8   R  R)   R	   R  t   absR  R.   R*   R,   R0   R2   (   R  R&  R   R#  t   vmint   vmaxR  R  t   normedR  R"   Rs   (    (   R&  R   R$  R#  R  s6   lib/python2.7/site-packages/pandas/io/formats/style.pyt   _bar  s*    
2s   #d65f5fR%  c         C   s   | d k r t  d   n  t |  s6 | | g } nJ t |  d k r_ | d | d g } n! t |  d k r t  d   n  t |  j |  } t |  } |  j |  j d	 | d
 | d | d | d | d | d | |  S(   s  
        Draw bar chart in the cell backgrounds.

        Parameters
        ----------
        subset : IndexSlice, optional
            A valid slice for `data` to limit the style application to.
        axis : int, str or None, default 0
            Apply to each column (`axis=0` or `'index'`)
            or to each row (`axis=1` or `'columns'`) or
            to the entire DataFrame at once with `axis=None`.
        color : str or 2-tuple/list
            If a str is passed, the color is the same for both
            negative and positive numbers. If 2-tuple/list is used, the
            first element is the color_negative and the second is the
            color_positive (eg: ['#d65f5f', '#5fba7d']).
        width : float, default 100
            A number between 0 or 100. The largest value will cover `width`
            percent of the cell's width.
        align : {'left', 'zero',' mid'}, default 'left'
            How to align the bars with the cells.

            - 'left' : the min value starts at the left of the cell.
            - 'zero' : a value of zero is located at the center of the cell.
            - 'mid' : the center of the cell is at (max-min)/2, or
              if values are all negative (positive) the zero is aligned
              at the right (left) of the cell.

              .. versionadded:: 0.20.0

        vmin : float, optional
            Minimum bar value, defining the left hand limit
            of the bar drawing range, lower values are clipped to `vmin`.
            When None (default): the minimum value of the data will be used.

            .. versionadded:: 0.24.0

        vmax : float, optional
            Maximum bar value, defining the right hand limit
            of the bar drawing range, higher values are clipped to `vmax`.
            When None (default): the maximum value of the data will be used.

            .. versionadded:: 0.24.0

        Returns
        -------
        self : Styler
        R%  R  R  s.   `align` must be one of {'left', 'zero',' mid'}i   i    i   sp   `color` must be string or a list-like of length 2: [`color_neg`, `color_pos`] (eg: color=['#d65f5f', '#5fba7d'])R   R   R&  R   R#  R(  R)  (   R%  R  R  (   R3   R   R   R   R4   R   R   R+  (   R#   R   R   R   R#  R&  R(  R)  (    (    s6   lib/python2.7/site-packages/pandas/io/formats/style.pyt   bar1  s    2t   yellowc      	   C   s"   |  j  d | d | d | d t  S(   s  
        Highlight the maximum by shading the background.

        Parameters
        ----------
        subset : IndexSlice, default None
            a valid slice for ``data`` to limit the style application to
        color : str, default 'yellow'
        axis : int, str, or None; default 0
            0 or 'index' for columnwise (default), 1 or 'columns' for rowwise,
            or ``None`` for tablewise

        Returns
        -------
        self : Styler
        R   R   R   t   max_(   t   _highlight_handlerR   (   R#   R   R   R   (    (    s6   lib/python2.7/site-packages/pandas/io/formats/style.pyt   highlight_maxw  s    c      	   C   s"   |  j  d | d | d | d t  S(   s  
        Highlight the minimum by shading the background.

        Parameters
        ----------
        subset : IndexSlice, default None
            a valid slice for ``data`` to limit the style application to
        color : str, default 'yellow'
        axis : int, str, or None; default 0
            0 or 'index' for columnwise (default), 1 or 'columns' for rowwise,
            or ``None`` for tablewise

        Returns
        -------
        self : Styler
        R   R   R   R.  (   R/  R:   (   R#   R   R   R   (    (    s6   lib/python2.7/site-packages/pandas/io/formats/style.pyt   highlight_min  s    c      
   C   sD   t  t |  j |   } |  j |  j d | d | d | d | |  S(   NR   R   R   R.  (   R   R   R4   R   t   _highlight_extrema(   R#   R   R   R   R.  (    (    s6   lib/python2.7/site-packages/pandas/io/formats/style.pyR/    s    c         C   s   d j  |  } |  j d k rn | r9 |  |  j   k } n |  |  j   k } g  | D] } | rd | n d ^ qR S| r |  |  j   j   k } n |  |  j   j   k } t j t j | | d  d |  j d |  j	 Sd S(   sD   
        Highlight the min or max in a Series or DataFrame.
        s   background-color: {0}i   RI   R0   R2   N(
   R   R.   R  R  R*   R,   t   npR   R0   R2   (   R4   R   R.  t   attrt   extremaR   (    (    s6   lib/python2.7/site-packages/pandas/io/formats/style.pyR2    s    #c            s>   t  t |  |  j g    d |  f    f d     Y} | S(   s  
        Factory function for creating a subclass of ``Styler``
        with a custom template and Jinja environment.

        Parameters
        ----------
        searchpath : str or list
            Path or paths of directories containing the templates
        name : str
            Name of your custom template to use for rendering

        Returns
        -------
        MyStyler : subclass of Styler
            has the correct ``env`` and ``template`` class attributes set.
        t   MyStylerc              s&   e  Z e d     Z e j   Z RS(   R   (   R   t
   __module__R   t   envt   get_templateR   (    (   R   R   (    s6   lib/python2.7/site-packages/pandas/io/formats/style.pyR6    s   (   R   R   R   (   t   clst
   searchpathR   R6  (    (   R   R   s6   lib/python2.7/site-packages/pandas/io/formats/style.pyt   from_custom_template  s
    	c         O   s   t  j |  | | |  S(   s	  
        Apply ``func(self, *args, **kwargs)``, and return the result.

        .. versionadded:: 0.24.0

        Parameters
        ----------
        func : function
            Function to apply to the Styler.  Alternatively, a
            ``(callable, keyword)`` tuple where ``keyword`` is a string
            indicating the keyword of ``callable`` that expects the Styler.
        *args, **kwargs :
            Arguments passed to `func`.

        Returns
        -------
        object :
            The value returned by ``func``.

        See Also
        --------
        DataFrame.pipe : Analogous method for DataFrame.
        Styler.apply : Apply a function row-wise, column-wise, or table-wise to
            modify the dataframe's styling.

        Notes
        -----
        Like :meth:`DataFrame.pipe`, this method can simplify the
        application of several user-defined functions to a styler.  Instead
        of writing:

        .. code-block:: python

            f(g(df.style.set_precision(3), arg1=a), arg2=b, arg3=c)

        users can write:

        .. code-block:: python

            (df.style.set_precision(3)
               .pipe(g, arg1=a)
               .pipe(f, arg2=b, arg3=c))

        In particular, this allows users to define functions that take a
        styler object, along with other parameters, and return the styler after
        making styling changes (such as calling :meth:`Styler.apply` or
        :meth:`Styler.set_properties`).  Using ``.pipe``, these user-defined
        style "transformations" can be interleaved with calls to the built-in
        Styler interface.

        Examples
        --------
        >>> def format_conversion(styler):
        ...     return (styler.set_properties(**{'text-align': 'right'})
        ...                   .format({'conversion': '{:.1%}'}))

        The user-defined ``format_conversion`` function above can be called
        within a sequence of other style modifications:

        >>> df = pd.DataFrame({'trial': list(range(5)),
        ...                    'conversion': [0.75, 0.85, np.nan, 0.7, 0.72]})
        >>> (df.style
        ...    .highlight_min(subset=['conversion'], color='yellow')
        ...    .pipe(format_conversion)
        ...    .set_caption("Results with minimum conversion highlighted."))
        (   R   t   _pipe(   R#   R   R   R   (    (    s6   lib/python2.7/site-packages/pandas/io/formats/style.pyt   pipe  s    CN(8   R   R7  t   __doc__R   R   R   R   R8  R9  R   R8   R?   RA   R   R   R   RB   R   R   R@   R   R:   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   t   staticmethodR   R   R  R  R  R+  R,  R0  R1  R/  R2  t   classmethodR<  R>  (    (    (    s6   lib/python2.7/site-packages/pandas/io/formats/style.pyR   7   s|   ;		$	
	A	2					!1	%									/	8:E	c         C   s   | |  f | k S(   s/   
    Index -> {(idx_row, idx_col): bool}).
    (    (   t   idx_rowt   idx_colt   lengths(    (    s6   lib/python2.7/site-packages/pandas/io/formats/style.pyR     s    c         C   s  t  j   } |  j d | d t d t  } | d k r? g  } n  i  } |  j d k r x9 t |  D]+ \ } } | | k ra d | d | f <qa qa W| Sx t |  D] \ } } x t |  D] \ } }	 t d  s d | | | f <q |	 | k r| | k r| }
 d | | |
 f <q |	 | k r;| }
 d | | |
 f <q | | k r | | |
 f c d 7<q q Wq Wd   | j   D } | S(	   s   
    Given an index, find the level length for each element.

    Optional argument is a list of index positions which
    should not be visible.

    Result is a dictionary of (level, inital_position): span
    t   sparsifyt   adjoinR   i   i    s   display.multi_sparsec         S   s+   i  |  ]! \ } } | d  k r | |  q S(   i   (    (   R   t   elementt   length(    (    s6   lib/python2.7/site-packages/pandas/io/formats/style.pys
   <dictcomp>J  s   	 N(	   R   t   sentinel_factoryR   R:   R8   R   R   R   R   (   R0   t   hidden_elementst   sentinelt   levelsRD  R   Rh   Rm   R   Rs   t
   last_labelt   non_zero_lengths(    (    s6   lib/python2.7/site-packages/pandas/io/formats/style.pyR   $  s0    		!c            sK   t     r   f d   St    r)   Sd j d    } t |   d  S(   Nc            s     j  |   S(   N(   R   (   R"   (   R]   (    s6   lib/python2.7/site-packages/pandas/io/formats/style.pyR%   Q  s    s?   Expected a template string or callable, got {formatter} insteadR]   (   R   t   callableR   R-   (   R]   R   (    (   R]   s6   lib/python2.7/site-packages/pandas/io/formats/style.pyR   O  s    		(=   R?  t   collectionsR    t
   contextlibR   R   t	   functoolsR   t	   itertoolsR   R5   R   t   numpyR3  t   pandas.compatR   t   pandas.util._decoratorsR   t   pandas.core.dtypes.commonR   R   t   pandas.core.dtypes.genericR	   R   R*   t   pandas.api.typesR
   R   t   pandas.core.commont   coret   commonR   t   pandas.core.configR   t   pandas.core.genericR   t   pandas.core.indexingR   R   t   jinja2R   R   R   R   R   t   matplotlib.pyplott   pyplotR   t
   matplotlibR   R   R   R:   R   R   t   objectR   R   R8   R   R   (    (    (    s6   lib/python2.7/site-packages/pandas/io/formats/style.pyt   <module>   sJ   &

    	+