U
    f/eRP                     @  sn  d dl mZ d dlmZmZ d dlZd dlmZmZm	Z	m
Z
mZmZ d dlmZ d dlmZmZ d dlmZ d dlmZ d d	lmZ erd d
lmZ ddddddZddddddZd1dddddZG dd deZG dd deZG dd dZ G d d! d!e Z!G d"d# d#eZ"G d$d% d%e"Z#G d&d' d'e#Z$G d(d) d)e"Z%G d*d+ d+e#e%Z&d,d-d.d/d0Z'dS )2    )annotations)ABCabstractmethodN)IOTYPE_CHECKINGIterableIteratorMappingSequence
get_option)DtypeFrameOrSeriesUnion)Index)format)pprint_thing)	DataFramezstr | Dtypeintstr)sspacereturnc                 C  s   t | d| |S )a  
    Make string of specified length, padding to the right if necessary.

    Parameters
    ----------
    s : Union[str, Dtype]
        String to be formatted.
    space : int
        Length to force string to be of.

    Returns
    -------
    str
        String coerced to given length.

    Examples
    --------
    >>> pd.io.formats.info._put_str("panda", 6)
    'panda '
    >>> pd.io.formats.info._put_str("panda", 4)
    'pand'
    N)r   ljust)r   r    r   :/tmp/pip-unpacked-wheel-tiezk1ph/pandas/io/formats/info.py_put_str!   s    r   zint | float)numsize_qualifierr   c                 C  sB   dD ],}| dk r(| d| d|   S | d } q| d| dS )a{  
    Return size in human readable format.

    Parameters
    ----------
    num : int
        Size in bytes.
    size_qualifier : str
        Either empty, or '+' (if lower bound).

    Returns
    -------
    str
        Size in human readable format.

    Examples
    --------
    >>> _sizeof_fmt(23028, '')
    '22.5 KB'

    >>> _sizeof_fmt(23028, '+')
    '22.5+ KB'
    )bytesZKBZMBZGBZTBg      @z3.1f z PBr   )r   r   xr   r   r   _sizeof_fmt;   s
    
r!   bool | str | None
bool | str)memory_usager   c                 C  s   | dkrt d} | S )z5Get memory usage based on inputs and display options.Nzdisplay.memory_usager   )r$   r   r   r   _initialize_memory_usageZ   s    r%   c                   @  s   e Zd ZU dZded< ded< eedddd	Zeed
dddZeeddddZ	eeddddZ
eddddZeddddZeddddddddZdS ) BaseInfoaj  
    Base class for DataFrameInfo and SeriesInfo.

    Parameters
    ----------
    data : DataFrame or Series
        Either dataframe or series.
    memory_usage : bool or str, optional
        If "deep", introspect the data deeply by interrogating object dtypes
        for system-level memory consumption, and include it in the returned
        values.
    r   datar#   r$   Iterable[Dtype]r   c                 C  s   dS )z
        Dtypes.

        Returns
        -------
        dtypes : sequence
            Dtype of each of the DataFrame's columns (or one series column).
        Nr   selfr   r   r   dtypest   s    zBaseInfo.dtypesMapping[str, int]c                 C  s   dS )!Mapping dtype - number of counts.Nr   r*   r   r   r   dtype_counts   s    zBaseInfo.dtype_countsSequence[int]c                 C  s   dS )BSequence of non-null counts for all columns or column (if series).Nr   r*   r   r   r   non_null_counts   s    zBaseInfo.non_null_countsr   c                 C  s   dS )z
        Memory usage in bytes.

        Returns
        -------
        memory_usage_bytes : int
            Object's total memory usage in bytes.
        Nr   r*   r   r   r   memory_usage_bytes   s    zBaseInfo.memory_usage_bytesr   c                 C  s   t | j| j dS )z0Memory usage in a form of human readable string.
)r!   r3   r   r*   r   r   r   memory_usage_string   s    zBaseInfo.memory_usage_stringc                 C  s2   d}| j r.| j dkr.d| jks*| jj r.d}|S )N deepobject+)r$   r/   r'   indexZ_is_memory_usage_qualified)r+   r   r   r   r   r      s    

zBaseInfo.size_qualifierIO[str] | None
int | Nonebool | NoneNonebufmax_colsverboseshow_countsr   c                C  s   dS )a^  
        Print a concise summary of a %(klass)s.

        This method prints information about a %(klass)s including
        the index dtype%(type_sub)s, non-null values and memory usage.
        %(version_added_sub)s
        Parameters
        ----------
        data : %(klass)s
            %(klass)s to print information about.
        verbose : bool, optional
            Whether to print the full summary. By default, the setting in
            ``pandas.options.display.max_info_columns`` is followed.
        buf : writable buffer, defaults to sys.stdout
            Where to send the output. By default, the output is printed to
            sys.stdout. Pass a writable buffer if you need to further process
            the output.
        %(max_cols_sub)s
        memory_usage : bool, str, optional
            Specifies whether total memory usage of the %(klass)s
            elements (including the index) should be displayed. By default,
            this follows the ``pandas.options.display.memory_usage`` setting.

            True always show memory usage. False never shows memory usage.
            A value of 'deep' is equivalent to "True with deep introspection".
            Memory usage is shown in human-readable units (base-2
            representation). Without deep introspection a memory estimation is
            made based in column dtype and number of rows assuming values
            consume the same memory amount for corresponding dtypes. With deep
            memory introspection, a real memory usage calculation is performed
            at the cost of computational resources.
        %(show_counts_sub)s

        Returns
        -------
        None
            This method prints a summary of a %(klass)s and returns None.

        See Also
        --------
        %(see_also_sub)s

        Examples
        --------
        %(examples_sub)s
        Nr   )r+   r@   rA   rB   rC   r   r   r   render   s    	zBaseInfo.renderN)__name__
__module____qualname____doc____annotations__propertyr   r,   r/   r2   r3   r5   r   rD   r   r   r   r   r&   c   s*   


r&   c                   @  s   e Zd ZdZd!dddddZedd	d
dZedd	ddZedd	ddZedd	ddZ	edd	ddZ
edd	ddZdddddddd ZdS )"DataFrameInfoz0
    Class storing dataframe-specific info.
    Nr   r"   )r'   r$   c                 C  s   || _ t|| _d S N)r'   r%   r$   )r+   r'   r$   r   r   r   __init__   s    zDataFrameInfo.__init__r-   r)   c                 C  s
   t | jS rL   )_get_dataframe_dtype_countsr'   r*   r   r   r   r/      s    zDataFrameInfo.dtype_countsr(   c                 C  s   | j jS )z
        Dtypes.

        Returns
        -------
        dtypes
            Dtype of each of the DataFrame's columns.
        )r'   r,   r*   r   r   r   r,      s    
zDataFrameInfo.dtypesr   c                 C  s   | j jS )zz
        Column names.

        Returns
        -------
        ids : Index
            DataFrame's column names.
        )r'   columnsr*   r   r   r   ids  s    
zDataFrameInfo.idsr   c                 C  s
   t | jS z#Number of columns to be summarized.)lenrP   r*   r   r   r   	col_count  s    zDataFrameInfo.col_countr0   c                 C  s
   | j  S )r1   )r'   countr*   r   r   r   r2     s    zDataFrameInfo.non_null_countsc                 C  s(   | j dkrd}nd}| jj d|d S )Nr7   TF)r:   r7   )r$   r'   sum)r+   r7   r   r   r   r3     s    
z DataFrameInfo.memory_usage_bytesr;   r<   r=   r>   r?   c                C  s   t | |||d}|| d S )NinforA   rB   rC   )DataFrameInfoPrinter	to_buffer)r+   r@   rA   rB   rC   printerr   r   r   rD     s    zDataFrameInfo.render)N)rE   rF   rG   rH   rM   rJ   r/   r,   rP   rS   r2   r3   rD   r   r   r   r   rK      s     rK   c                   @  s4   e Zd ZdZddddddZedd	d
dZdS )InfoPrinterAbstractz6
    Class for printing dataframe or series info.
    Nr;   r>   )r@   r   c                 C  s.   |   }| }|dkrtj}t|| dS )z Save dataframe info into buffer.N)_create_table_builder	get_linessysstdoutfmtZbuffer_put_lines)r+   r@   Ztable_builderlinesr   r   r   rY   5  s
    zInfoPrinterAbstract.to_bufferTableBuilderAbstractr)   c                 C  s   dS )z!Create instance of table builder.Nr   r*   r   r   r   r\   =  s    z)InfoPrinterAbstract._create_table_builder)N)rE   rF   rG   rH   rY   r   r\   r   r   r   r   r[   0  s   r[   c                   @  s   e Zd ZdZddddddddZed	d
ddZedd
ddZedd
ddZed	d
ddZ	dd	dddZ
dddddZdd
ddZdS )rX   a{  
    Class for printing dataframe info.

    Parameters
    ----------
    info : DataFrameInfo
        Instance of DataFrameInfo.
    max_cols : int, optional
        When to switch from the verbose to the truncated output.
    verbose : bool, optional
        Whether to print the full summary.
    show_counts : bool, optional
        Whether to show the non-null counts.
    NrK   r<   r=   rV   c                 C  s0   || _ |j| _|| _| || _| || _d S rL   )rW   r'   rB   _initialize_max_colsrA   _initialize_show_countsrC   )r+   rW   rA   rB   rC   r   r   r   rM   R  s
    zDataFrameInfoPrinter.__init__r   r)   c                 C  s   t dt| jd S )z"Maximum info rows to be displayed.zdisplay.max_info_rows   )r   rR   r'   r*   r   r   r   max_rows_  s    zDataFrameInfoPrinter.max_rowsboolc                 C  s   t | j| jkS )zDCheck if number of columns to be summarized does not exceed maximum.)rg   rS   rA   r*   r   r   r   exceeds_info_colsd  s    z&DataFrameInfoPrinter.exceeds_info_colsc                 C  s   t t| j| jkS )zACheck if number of rows to be summarized does not exceed maximum.)rg   rR   r'   rf   r*   r   r   r   exceeds_info_rowsi  s    z&DataFrameInfoPrinter.exceeds_info_rowsc                 C  s   | j jS rQ   rW   rS   r*   r   r   r   rS   n  s    zDataFrameInfoPrinter.col_count)rA   r   c                 C  s   |d krt d| jd S |S )Nzdisplay.max_info_columnsre   )r   rS   )r+   rA   r   r   r   rc   s  s    z)DataFrameInfoPrinter._initialize_max_cols)rC   r   c                 C  s$   |d krt | j o| j S |S d S rL   )rg   rh   ri   )r+   rC   r   r   r   rd   x  s    z,DataFrameInfoPrinter._initialize_show_countsDataFrameTableBuilderc                 C  sR   | j rt| j| jdS | j dkr,t| jdS | jr>t| jdS t| j| jdS dS )z[
        Create instance of table builder based on verbosity and display settings.
        rW   with_countsFrW   N)rB   DataFrameTableBuilderVerboserW   rC   DataFrameTableBuilderNonVerboserh   r*   r   r   r   r\   ~  s    
z*DataFrameInfoPrinter._create_table_builder)NNN)rE   rF   rG   rH   rM   rJ   rf   rh   ri   rS   rc   rd   r\   r   r   r   r   rX   B  s       rX   c                   @  s   e Zd ZU dZded< ded< eddddZed	dd
dZeddddZ	eddddZ
eddddZeddddZeddddZddddZddddZddd d!Zd"S )#rb   z*
    Abstract builder for info table.
    	list[str]_linesr&   rW   r)   c                 C  s   dS )z-Product in a form of list of lines (strings).Nr   r*   r   r   r   r]     s    zTableBuilderAbstract.get_linesr   c                 C  s   | j jS rL   rW   r'   r*   r   r   r   r'     s    zTableBuilderAbstract.datar(   c                 C  s   | j jS )z*Dtypes of each of the DataFrame's columns.)rW   r,   r*   r   r   r   r,     s    zTableBuilderAbstract.dtypesr-   c                 C  s   | j jS )r.   )rW   r/   r*   r   r   r   r/     s    z!TableBuilderAbstract.dtype_countsrg   c                 C  s   t | jjS )z Whether to display memory usage.)rg   rW   r$   r*   r   r   r   display_memory_usage  s    z)TableBuilderAbstract.display_memory_usager   c                 C  s   | j jS )z/Memory usage string with proper size qualifier.)rW   r5   r*   r   r   r   r5     s    z(TableBuilderAbstract.memory_usage_stringr0   c                 C  s   | j jS rL   )rW   r2   r*   r   r   r   r2     s    z$TableBuilderAbstract.non_null_countsr>   c                 C  s   | j tt| j dS )z>Add line with string representation of dataframe to the table.N)rr   appendr   typer'   r*   r   r   r   add_object_type_line  s    z)TableBuilderAbstract.add_object_type_linec                 C  s   | j | jj  dS )z,Add line with range of indices to the table.N)rr   ru   r'   r:   _summaryr*   r   r   r   add_index_range_line  s    z)TableBuilderAbstract.add_index_range_linec                 C  s4   dd t | j D }| jdd|  dS )z2Add summary line with dtypes present in dataframe.c                 S  s"   g | ]\}}| d |ddqS )(d)r   ).0keyvalr   r   r   
<listcomp>  s    z8TableBuilderAbstract.add_dtypes_line.<locals>.<listcomp>zdtypes: z, N)sortedr/   itemsrr   ru   join)r+   Zcollected_dtypesr   r   r   add_dtypes_line  s    z$TableBuilderAbstract.add_dtypes_lineN)rE   rF   rG   rH   rI   r   r]   rJ   r'   r,   r/   rt   r5   r2   rw   ry   r   r   r   r   r   rb     s(   
rb   c                   @  s   e Zd ZdZddddZdddd	Zd
dddZed
dddZe	ddddZ
e	ddddZe	ddddZd
dddZdS )rk   z
    Abstract builder for dataframe info table.

    Parameters
    ----------
    info : DataFrameInfo.
        Instance of DataFrameInfo.
    rK   rn   c                C  s
   || _ d S rL   rn   )r+   rW   r   r   r   rM     s    zDataFrameTableBuilder.__init__rq   r)   c                 C  s(   g | _ | jdkr|   n|   | j S )Nr   )rr   rS   _fill_empty_info_fill_non_empty_infor*   r   r   r   r]     s
    

zDataFrameTableBuilder.get_linesr>   c                 C  s.   |    |   | jdt| jj  dS )z;Add lines to the info table, pertaining to empty dataframe.zEmpty N)rw   ry   rr   ru   rv   r'   rE   r*   r   r   r   r     s    z&DataFrameTableBuilder._fill_empty_infoc                 C  s   dS z?Add lines to the info table, pertaining to non-empty dataframe.Nr   r*   r   r   r   r     s    z*DataFrameTableBuilder._fill_non_empty_infor   c                 C  s   | j jS )z
DataFrame.rs   r*   r   r   r   r'     s    zDataFrameTableBuilder.datar   c                 C  s   | j jS )zDataframe columns.)rW   rP   r*   r   r   r   rP     s    zDataFrameTableBuilder.idsr   c                 C  s   | j jS )z-Number of dataframe columns to be summarized.rj   r*   r   r   r   rS     s    zDataFrameTableBuilder.col_countc                 C  s   | j d| j  dS )z!Add line containing memory usage.zmemory usage: N)rr   ru   r5   r*   r   r   r   add_memory_usage_line  s    z+DataFrameTableBuilder.add_memory_usage_lineN)rE   rF   rG   rH   rM   r]   r   r   r   rJ   r'   rP   rS   r   r   r   r   r   rk     s   	rk   c                   @  s,   e Zd ZdZddddZddddZdS )	rp   z>
    Dataframe info table builder for non-verbose output.
    r>   r)   c                 C  s2   |    |   |   |   | jr.|   dS r   )rw   ry   add_columns_summary_liner   rt   r   r*   r   r   r   r     s    z4DataFrameTableBuilderNonVerbose._fill_non_empty_infoc                 C  s   | j | jjdd d S )NZColumnsname)rr   ru   rP   rx   r*   r   r   r   r     s    z8DataFrameTableBuilderNonVerbose.add_columns_summary_lineN)rE   rF   rG   rH   r   r   r   r   r   r   rp     s   	rp   c                   @  s   e Zd ZU dZdZded< ded< ded< d	ed
< eeddddZeddddZ	ddddZ
ddddZddddZeddddZeddddZddddZdddd Zddd!d"Zd#dd$d%Zd#dd&d'Zd(S ))TableBuilderVerboseMixinz(
    Mixin for verbose info output.
    z  r   SPACINGzSequence[Sequence[str]]strrowsr0   gross_column_widthsrg   rm   Sequence[str]r)   c                 C  s   dS ).Headers names of the columns in verbose table.Nr   r*   r   r   r   headers  s    z TableBuilderVerboseMixin.headersc                 C  s   dd | j D S )z'Widths of header columns (only titles).c                 S  s   g | ]}t |qS r   rR   r}   colr   r   r   r   "  s     zATableBuilderVerboseMixin.header_column_widths.<locals>.<listcomp>)r   r*   r   r   r   header_column_widths  s    z-TableBuilderVerboseMixin.header_column_widthsc                 C  s   |   }dd t| j|D S )zAGet widths of columns containing both headers and actual content.c                 S  s   g | ]}t | qS r   max)r}   Zwidthsr   r   r   r   '  s   zETableBuilderVerboseMixin._get_gross_column_widths.<locals>.<listcomp>)_get_body_column_widthszipr   )r+   Zbody_column_widthsr   r   r   _get_gross_column_widths$  s    
z1TableBuilderVerboseMixin._get_gross_column_widthsc                 C  s   t t| j }dd |D S )z$Get widths of table content columns.c                 S  s   g | ]}t d d |D qS )c                 s  s   | ]}t |V  qd S rL   r   )r}   r    r   r   r   	<genexpr>/  s     zNTableBuilderVerboseMixin._get_body_column_widths.<locals>.<listcomp>.<genexpr>r   r   r   r   r   r   /  s     zDTableBuilderVerboseMixin._get_body_column_widths.<locals>.<listcomp>)listr   r   )r+   Zstrcolsr   r   r   r   ,  s    z0TableBuilderVerboseMixin._get_body_column_widthsIterator[Sequence[str]]c                 C  s   | j r|  S |  S dS )z
        Generator function yielding rows content.

        Each element represents a row comprising a sequence of strings.
        N)rm   _gen_rows_with_counts_gen_rows_without_countsr*   r   r   r   	_gen_rows1  s    z"TableBuilderVerboseMixin._gen_rowsc                 C  s   dS z=Iterator with string representation of body data with counts.Nr   r*   r   r   r   r   <  s    z.TableBuilderVerboseMixin._gen_rows_with_countsc                 C  s   dS z@Iterator with string representation of body data without counts.Nr   r*   r   r   r   r   @  s    z1TableBuilderVerboseMixin._gen_rows_without_countsr>   c                 C  s0   | j dd t| j| jD }| j| d S )Nc                 S  s   g | ]\}}t ||qS r   r   )r}   headerZ	col_widthr   r   r   r   F  s   z<TableBuilderVerboseMixin.add_header_line.<locals>.<listcomp>)r   r   r   r   r   rr   ru   )r+   Zheader_liner   r   r   add_header_lineD  s    z(TableBuilderVerboseMixin.add_header_linec                 C  s0   | j dd t| j| jD }| j| d S )Nc                 S  s   g | ]\}}t d | |qS )-r   )r}   Zheader_colwidthgross_colwidthr   r   r   r   O  s   z?TableBuilderVerboseMixin.add_separator_line.<locals>.<listcomp>)r   r   r   r   r   rr   ru   )r+   Zseparator_liner   r   r   add_separator_lineM  s     z+TableBuilderVerboseMixin.add_separator_linec                 C  s:   | j D ].}| jdd t|| jD }| j| qd S )Nc                 S  s   g | ]\}}t ||qS r   r   )r}   r   r   r   r   r   r   [  s   z;TableBuilderVerboseMixin.add_body_lines.<locals>.<listcomp>)r   r   r   r   r   rr   ru   )r+   rowZ	body_liner   r   r   add_body_linesX  s    

z'TableBuilderVerboseMixin.add_body_linesIterator[str]c                 c  s   | j D ]}| dV  qdS )z7Iterator with string representation of non-null counts.z	 non-nullN)r2   )r+   rT   r   r   r   _gen_non_null_countsb  s    
z-TableBuilderVerboseMixin._gen_non_null_countsc                 c  s   | j D ]}t|V  qdS )z5Iterator with string representation of column dtypes.N)r,   r   )r+   Zdtyper   r   r   _gen_dtypesg  s    
z$TableBuilderVerboseMixin._gen_dtypesN)rE   rF   rG   rH   r   rI   rJ   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s,   
	
r   c                   @  s   e Zd ZdZdddddZddd	d
ZeddddZddddZddddZ	ddddZ
ddddZddddZdS )ro   z:
    Dataframe info table builder for verbose output.
    rK   rg   rl   c                C  s(   || _ || _t|  | _|  | _d S rL   )rW   rm   r   r   r   r   r   )r+   rW   rm   r   r   r   rM   r  s    z%DataFrameTableBuilderVerbose.__init__r>   r)   c                 C  sJ   |    |   |   |   |   |   |   | jrF|   dS r   )	rw   ry   r   r   r   r   r   rt   r   r*   r   r   r   r   }  s    z1DataFrameTableBuilderVerbose._fill_non_empty_infor   c                 C  s   | j rddddgS dddgS )r   z # ZColumnzNon-Null Countr   )rm   r*   r   r   r   r     s    z$DataFrameTableBuilderVerbose.headersc                 C  s   | j d| j d d S )NzData columns (total z
 columns):)rr   ru   rS   r*   r   r   r   r     s    z5DataFrameTableBuilderVerbose.add_columns_summary_liner   c                 c  s"   t |  |  |  E dH  dS r   )r   _gen_line_numbers_gen_columnsr   r*   r   r   r   r     s
    z5DataFrameTableBuilderVerbose._gen_rows_without_countsc                 c  s(   t |  |  |  |  E dH  dS r   )r   r   r   r   r   r*   r   r   r   r     s    z2DataFrameTableBuilderVerbose._gen_rows_with_countsr   c                 c  s$   t | jD ]\}}d| V  q
dS )z6Iterator with string representation of column numbers.r   N)	enumeraterP   )r+   i_r   r   r   r     s    z.DataFrameTableBuilderVerbose._gen_line_numbersc                 c  s   | j D ]}t|V  qdS )z4Iterator with string representation of column names.N)rP   r   )r+   r   r   r   r   r     s    
z)DataFrameTableBuilderVerbose._gen_columnsN)rE   rF   rG   rH   rM   r   rJ   r   r   r   r   r   r   r   r   r   r   ro   m  s   	ro   r   r-   )dfr   c                 C  s   | j  dd  S )zK
    Create mapping between datatypes and their number of occurrences.
    c                 S  s   | j S rL   r   )r    r   r   r   <lambda>      z-_get_dataframe_dtype_counts.<locals>.<lambda>)r,   Zvalue_countsgroupbyrU   )r   r   r   r   rN     s    rN   )N)(
__future__r   abcr   r   r^   typingr   r   r   r   r	   r
   Zpandas._configr   Zpandas._typingr   r   Zpandas.core.indexes.apir   Zpandas.io.formatsr   r`   Zpandas.io.formats.printingr   Zpandas.core.framer   r   r!   r%   r&   rK   r[   rX   rb   rk   rp   r   ro   rN   r   r   r   r   <module>   s2    	  	 LQ83]B