HOME


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

���hd�@sdZddlmZddlZddlZddlmZmZmZm	Z	m
Z
ddlZddlZ
ddlmZddlmZddlmZmZddlmZdd	lmZmZmZmZddljjZdd
l m!Z!ddl"m#Z#m$Z$ddl%m&Z&dd
l'm(Z(Gdd�d�Z)Gdd�d�Z*Gdd�d�Z+dS)zC
Utilities for conversion to writer-agnostic Excel representation.
�)�reduceN)�Callable�Dict�Optional�Sequence�Union)�Label)�missing)�is_float�	is_scalar)�ABCIndex)�	DataFrame�Index�
MultiIndex�PeriodIndex)�stringify_path)�CSSResolver�
CSSWarning)�get_level_lengths)�pprint_thingc@s&eZdZdZeZdeed�d	d
�ZdS)
�	ExcelCell�row�col�val�style�
mergestart�mergeendN)rrcCs(||_||_||_||_||_||_dS)N)rrrrrr)�selfrrrrrr�r�9/tmp/pip-build-5_djhm0z/pandas/pandas/io/formats/excel.py�__init__ szExcelCell.__init__)rrrrrr)NNN)�__name__�
__module__�__qualname__Z
__fields__�	__slots__�intr rrrrrsrc@s�eZdZdZd:eed�dd�Ze�Zee	ee	eeffd�dd�Z
e	eefe	ee	eeffd	�d
d�Zddd
dddd�Ze	eee
eeffd�dd�Ze	e	ee	eeffd	�dd�Zeed�dd�Ze	eefd�dd�Zdddddddddddddd�
Zdddd�Ze	eee
eeeffd�dd �Zd!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d1d2d3d4�Zeed5�d6d7�Ze	e	eeefd	�d8d9�ZdS);�CSSToExcelConverteraK
    A callable for converting CSS declarations to ExcelWriter styles

    Supports parts of CSS 2.2, with minimal CSS 3.0 support (e.g. text-shadow),
    focusing on font styling, backgrounds, borders and alignment.

    Operates by first computing CSS styles in a fairly generic
    way (see :meth:`compute_css`) then determining Excel style
    properties from CSS properties (see :meth:`build_xlstyle`).

    Parameters
    ----------
    inherited : str, optional
        CSS declarations understood to be the containing scope for the
        CSS processed by :meth:`__call__`.
    N)�	inheritedcCs|dk	r|j|�}||_dS)N)�compute_cssr')rr'rrrr Bs
zCSSToExcelConverter.__init__)�declarations_str�returncCs|j||j�}|j|�S)ax
        Convert CSS declarations to ExcelWriter style.

        Parameters
        ----------
        declarations_str : str
            List of CSS declarations.
            e.g. "font-weight: bold; background: blue"

        Returns
        -------
        xlstyle : dict
            A style as interpreted by ExcelWriter when found in
            ExcelCell.style.
        )r(r'�
build_xlstyle)rr)Z
propertiesrrr�__call__JszCSSToExcelConverter.__call__)�propsr*csV|j|�|j|�|j|�|j|�|j|�d�}tttfdd��fdd���|�|S)N)�	alignmentZborder�fill�fontZ
number_format)�dr*csJxDt|j��D]4\}}|dkr&||=qt|t�r�|�|s||=qWdS)z4Remove key where value is None, through nested dictsN)�list�items�
isinstance�dict)r1�k�v)�remove_nonerrr8is
z6CSSToExcelConverter.build_xlstyle.<locals>.remove_none)�build_alignment�build_border�
build_fill�
build_font�build_number_formatr�str)rr-�outr)r8rr+^s
z!CSSToExcelConverter.build_xlstyle�top�center�bottom)r@ztext-topZmiddleZbaselinerBztext-bottom)r*cCs:|jd�|jj|jd��|jd�dkr*dn
|ddkd�S)	Nz
text-alignzvertical-alignzwhite-space�nowrap�pre�pre-line)�
horizontal�vertical�	wrap_text)rCrDrE)�get�VERTICAL_MAP)rr-rrrr9�sz#CSSToExcelConverter.build_alignmentcs��fdd�dD�S)NcsPi|]H}�j�jd|�d���jd|�d����j�jd|�d���d�|�qS)zborder-z-stylez-widthz-color)r�color)�
_border_stylerI�color_to_excel)�.0Zside)r-rrr�
<dictcomp>�sz4CSSToExcelConverter.build_border.<locals>.<dictcomp>r@�rightrB�left)r@rPrBrQr)rr-r)r-rrr:�sz CSSToExcelConverter.build_border)rcCs�|dkr|dkrdS|dks$|dkr(dS|dkr4d}t|dd��}|dkrPdS|dkr^d}n|dkrld	}nd
}|dkr|d}|dkr�dS|dkr�|S|dkr�|dkr�dSdS|dkr�|dkr�dSdSdS)N�none�hiddenZ2pt�g�h㈵��>g�������?�thingffffff@ZmediumZthick�groove�ridge�inset�outset�solid�doubleZdotted�hairZmediumDashDotDotZdashedZmediumDashed���)NrVrWrXrY)r\rU)r\rU)�float)rr�widthZ
width_namerrrrL�s8z!CSSToExcelConverter._border_style)r-cCs&|jd�}|dkr"|j|�dd�SdS)Nzbackground-color�transparentrRrZ)ZfgColorZpatternType)Nr`rR)rIrM)rr-Z
fill_colorrrrr;�s
zCSSToExcelConverter.build_fillTF)
�boldZbolderZ600Z700Z800Z900�normalZlighterZ100Z200Z300Z400�500)rb�italicZobliquec
Cs�|jd�}|dk	r0|jd�s t�t|dd��}tjd|jdd��}g}xn|D]f}|dd�dkrx|dd�jd	d�}n.|dd�d
kr�|dd�jdd
�}n|j�}|rN|j|�qNWd}xJ|D]B}|dkr�d}Pq�|d
kr�d}Pq�|dkr�d}Pq�|dkr�d}Pq�W|jd�}|dk	�r&|j	�}nf}|�r8|dnd|||j
j|jd��|jj|jd��d|k�rldndd|k�pzd|j|jd��d|k�r�t
tjd|d��ndd�	S) Nz	font-size�ptrTz�(?x)
            (
            "(?:[^"]|\\")+"
            |
            '(?:[^']|\\')+'
            |
            [^'",]+
            )(?=,|\s*$)
        zfont-family���"z\"�'z\'Zserifz
sans-serifZcursive�Zfantasy�ztext-decorationrzfont-weightz
font-style�	underlineZsinglezline-throughrKztext-shadowz^[^#(]*[1-9])	�name�family�sizerardrl�strikerKZshadowr]���rq)rI�endswith�AssertionErrorr^�re�findall�replace�strip�append�split�BOLD_MAP�
ITALIC_MAPrM�bool�search)rr-roZfont_names_tmpZ
font_namesrmrnZ
decorationrrrr<�sX
	




zCSSToExcelConverter.build_fontZ800000ZA52A2AZFF0000ZFFC0CBZFFA500ZFFFF00Z808000Z008000Z800080ZFF00FFZ00FF00Z008080Z00FFFFZ0000FFZ000080Z000000Z808080ZC0C0C0ZFFFFFF)ZmaroonZbrownZredZpinkZorangeZyellowZoliveZgreenZpurpleZfuchsiaZlimeZtealZaquaZblueZnavyZblackZgrayZgreyZsilverZwhite)rcCs�|dkrdS|jd�r2t|�dkr2|dd�j�S|jd�rpt|�dkrp|dd|dd|ddj�Sy
|j|Stk
r�tjdt|���t�YnXdS)N�#�rgrjrT�zUnhandled color format: )	�
startswith�len�upper�NAMED_COLORS�KeyError�warnings�warn�reprr)rrrrrrMAs(
z"CSSToExcelConverter.color_to_excelcCsd|jd�iS)NZformat_codez
number-format)rI)rr-rrrr=Msz'CSSToExcelConverter.build_number_format)N)r!r"r#�__doc__rr>r rr(rr,r+rJrr|r9r:rLr;rzr{r%r<r�rMr=rrrrr&+sh (" 1$Jr&c@s�eZdZdZd&Zd'Zd(eeeeee	e
ee	efeee
e	ee	feeeed
�	dd�Z
ed
d��Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zed�dd�Zd d!�Zd)d$d%�ZdS)*�ExcelFormattera^
    Class for formatting a DataFrame to a list of ExcelCells,

    Parameters
    ----------
    df : DataFrame or Styler
    na_rep: na representation
    float_format : string, default None
            Format string for floating point numbers
    cols : sequence, optional
        Columns to write
    header : boolean or list of string, default True
        Write out column names. If a list of string is given it is
        assumed to be aliases for the column names
    index : boolean, default True
        output row names (index)
    index_label : string or sequence, default None
            Column label for index column(s) if desired. If None is given, and
            `header` and `index` are True, then the index names are used. A
            sequence should be given if the DataFrame uses MultiIndex.
    merge_cells : boolean, default False
            Format MultiIndex and Hierarchical Rows as merged cells.
    inf_rep : string, default `'inf'`
        representation for np.inf values (which aren't representable in Excel)
        A `'-'` sign will be added in front of -inf.
    style_converter : callable, optional
        This translates Styler styles (CSS) into ExcelWriter styles.
        Defaults to ``CSSToExcelConverter()``.
        It should have signature css_declarations string -> excel style.
        This is only called for body cells.
    rT��rfNTF�inf)	�na_rep�float_format�cols�header�index�index_label�merge_cells�inf_rep�style_convertercCs�d|_||_t|t�s8||_|j}|
dkr0t�}
|
|_nd|_||_|dk	r�t	t
|�|j@�sftd��t	t
|�|j@�t	|�kr�td��|j
|d�|_|jj|_||_||_||_||_||_|	|_dS)Nrz,passes columns are not ALL present dataframez.Not all names specified in 'columns' are found)�columns)�
rowcounterr�r4r
�styler�datar&r��dfr�rr�r�Zreindexr�r�r�r�r�r�)rr�r�r�r�r�r�r�r�r�r�rrrr us.


zExcelFormatter.__init__cCs ddiddddd�ddd�d�S)	NraTrU)r@rPrBrQrAr@)rFrG)r0Zbordersr.r)rrrr�header_style�szExcelFormatter.header_stylecCs�t|�rtj|�r|j}nJt|�rdtj|�r4|j}n0tj|�rLd|j��}n|jdk	rdt	|j|�}t
|dd�dk	r|td��|S)N�-�tzinfoz{Excel does not support datetimes with timezones. Please ensure that datetimes are timezone unaware before writing to Excel.)rr	Zisnar�r
Zisposinf_scalarr�Zisneginf_scalarr�r^�getattr�
ValueError)rrrrr�
_format_value�s


zExcelFormatter._format_valueccs�|jjdkr|jstd��t|jtttj	t
f�}|p8|js>dS|j}|j|jddd�}t
|�}d}d}|jr�t|jjt�r�t|jjd�d}|j�rhx0tt|��D] }|j|}t||||j�Vq�Wx�tt||j|j��D]�\}\}}	}
|	j|
�}xj|D]b}||dk�r>t|||d|||j|||||�Vnt|||d|||j�V�q�Wq�WnFxDtt|��D]4\}}djtt|��}
t|||d|
|j�V�qvW||_dS)Nrgz]Writing to Excel with MultiIndex columns and no index ('index'=False) is not yet implemented.F)�sparsify�adjoin�namesr�.)r�Znlevelsr��NotImplementedErrorr4r��tupler2�np�ndarrayr�formatr�rr�rr��ranger�rr��	enumerate�zip�levels�codes�take�join�maprr�)r�has_aliasesr��
level_strs�
level_lengths�	coloffset�lnumrm�spansr��level_codes�values�ir7rrr�_format_header_mi�sJ

 


( z ExcelFormatter._format_header_miccs�t|jtttjtf�}|s |jr�d}|jrLd}t|jjt	�rLt
|jjd�}|j}|r�t
|j�t
|j�kr�tdt
|j��dt
|j��d���n|j}x,t
|�D] \}}t|j||||j�Vq�WdS)NrrgzWriting z cols but got z aliases)r4r�r�r2r�r�rr�r�rr�r�r�r�rr�r�)rr�r�Zcolnames�colindexZcolnamerrr�_format_header_regular�s 
"z%ExcelFormatter._format_header_regularcs�t�jt�r�j�}n�j�}f}�jjjr�dd��jjjD�dgt�j�}t	dd�t
dd�|��r��fdd�t|�D�}�jd	7_t
j||�S)
NcSsg|]}|dk	r|nd�qS)Nrfr)rN�xrrr�
<listcomp>sz1ExcelFormatter._format_header.<locals>.<listcomp>rfcSs|o|S)Nr)r��yrrr�<lambda>sz/ExcelFormatter._format_header.<locals>.<lambda>cSs|dkS)Nrfr)r�rrrr�sc3s$|]\}}t�j||�j�VqdS)N)rr�r�)rNr�r)rrr�	<genexpr>sz0ExcelFormatter._format_header.<locals>.<genexpr>rg)r4r�rr�r�r�r�r�r�rr�r�r��	itertools�chain)r�genZgen2rr)rr�_format_headers


zExcelFormatter._format_headercCs"t|jjt�r|j�S|j�SdS)N)r4r�r�r�_format_hierarchical_rows�_format_regular_rows)rrrr�_format_body#szExcelFormatter._format_bodyccs@t|jtttjtf�}|s |jr.|jd7_|j�r|j	r^t|j	tttjt
f�r^|j	d}n(|j	rxt|j	t�rx|j	}n|jjj
d}t|jt�r�|jd7_|r�|jdk	r�t|jdd||j�V|jj}t|jjt�r�|jjj�}x,t|�D] \}}t|j|d||j�Vq�Wd}nd}x|j|�D]}|V�q,WdS)NrgrF)r4r�r�r2r�r�rr�r�r�rr>r�r�r�rrr�rZto_timestampr��_generate_body)rr�r�Zindex_values�idxZidxvalr��cellrrrr�)s.
z#ExcelFormatter._format_regular_rowsc
cst|jtttjtf�}|s |jr.|jd7_d}|j�r�|j	jj
}|jrft|jtttjtf�rf|j}t|j
t�r�|jr�|jd7_tj|�r�|jdk	r�x,t|�D] \}}t|jd|||j�Vq�W|j�r�|j	jjdddd�}t|�}x�t||j	jj|j	jj�D]�\}}	}
|	j|
|	jdd�}xl|D]d}||dk�rnt|j|||||j|j|||d|�Vnt|j|||||j�V�q(W|d7}�qWnPxNt|j	j�D]>}
x.t|
�D]"\}}t|j||||j�V�q�W|d7}�q�Wx|j|�D]}|V�q�WdS)NrgrFT)r�r�r�)Z
allow_fillZ
fill_value)r4r�r�r2r�r�rr�r�r�r�r�rr�rr��comZany_not_noner�rr�r�rr�r�r�r�Z_can_hold_nar�)rr�ZgcolidxZindex_labelsZcidxrmr�r�r�r�r�r�r�Zindexcolvalsr�Zindexcolvalr�rrrr�Qs^

 

z(ExcelFormatter._format_hierarchical_rows)r�ccs�|jdkrd}n|jj�j}|s$d}d}xztt|j��D]h}|jjdd�|f}xNt|�D]B\}}|dk	r�|j	dj
|||f��}t|j|||||�VqZWq8WdS)N�;)
r�Z_compute�ctxr�r�r�r�Zilocr�r�r�rr�)rr�ZstylesZxlstyleZcolidxZseriesr�rrrrr��s
zExcelFormatter._generate_bodyccs6x0tj|j�|j��D]}|j|j�|_|VqWdS)N)r�r�r�r�r�r)rr�rrr�get_formatted_cells�sz"ExcelFormatter.get_formatted_cells�Sheet1rc	Cs�ddlm}|jj\}}	||jks,|	|jkrPtd|�d|	�d|j�d|j����t||�r`d}
n|t|�|d�}d}
|j	�}|j
|||||d	�|
r�|j�d
S)a�
        writer : string or ExcelWriter object
            File path or existing ExcelWriter
        sheet_name : string, default 'Sheet1'
            Name of sheet which will contain DataFrame
        startrow :
            upper left cell row to dump data frame
        startcol :
            upper left cell column to dump data frame
        freeze_panes : tuple of integer (length 2), default None
            Specifies the one-based bottommost row and rightmost column that
            is to be frozen
        engine : string, default None
            write engine to use if writer is a path - you can also set this
            via the options ``io.excel.xlsx.writer``, ``io.excel.xls.writer``,
            and ``io.excel.xlsm.writer``.
        r)�ExcelWriterz-This sheet is too large! Your sheet size is: z, z Max sheet size is: F)�engineT)�startrow�startcol�freeze_panesN)Zpandas.io.excelr�r��shape�max_rows�max_colsr�r4rr�Zwrite_cells�save)r�writerZ
sheet_namer�r�r�r�r�Znum_rowsZnum_colsZ	need_saveZformatted_cellsrrr�write�s$"
zExcelFormatter.writeii@)	rfNNTTNFr�N)r�rrNN)r!r"r#r�r�r�r>rrrrr|rr �propertyr�r�r�r�r�r�r�r�r%r�r�r�rrrrr�Qs6L!
7(Mr�),r��	functoolsrr�rtZtypingrrrrrr�Znumpyr�Zpandas._typingrZpandas.core.dtypesr	Zpandas.core.dtypes.commonr
rZpandas.core.dtypes.genericrZpandasr
rrrZpandas.core.common�core�commonr�Zpandas.io.commonrZpandas.io.formats.cssrrZpandas.io.formats.formatrZpandas.io.formats.printingrrr&r�rrrr�<module>s*(