3
���h�� �
@ s� d Z ddlmZ ddlmZmZ ddlmZ ddlZddl m
Z
ddlmZ ddl
Z
ddlZddlmZ dd lmZmZmZmZmZmZmZmZmZmZmZmZmZmZ dd
l m!Z! ddl"Z#ddl$m%Z%m&Z& ddl'm(Z( dd
l)m*Z* ddl+m,Z, ddl-m.Z.m/Z/m0Z0m1Z1 ddl2m3Z3 ddl4m5Z5m6Z6 ddl7m8Z8 ddl9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZCmDZDmEZEmFZF ddlGmHZHmIZI ddlJmKZK ddlLmMZM ddlNmOZO ddlPjQjRZSddlTmUZU ddlVmWZWmXZXmYZYmZZZ ddl[m\Z\ ddl]m^Z^ ddl_m`Z` ddlambZbmcZcmdZd e�r"ddlemfZfmgZgmhZh eee eedf eeeiejf ef f Zkeeied f Zlee6eeiejf f Zmeeiejeeeiejf ee6eeiejf f f Znd!Zod�Zpd-ZqG d.d/� d/�ZrG d0d1� d1�ZsG d2d3� d3�ZtG d4d5� d5et�Zuetd6�d7d8�ZvG d9d:� d:�ZwG d;d<� d<ew�Zxd�eee eel eieej eeeiejf eieieey eej eei d?�d@dA�ZzG dBdC� dC�Z{G dDdE� dEe{�Z|G dFdG� dGe{�Z}G dHdI� dIe{�Z~G dJdK� dKe{�Zee#j�eeeje�f ee� eeeie�f f eei dL�dMdN�Z�ee#j�eKeWe\f eydO�dPdQ�Z�d�ee3e0f ee eieidS�dTdU�Z�d�ee3e0f eideidV�dWdX�Z�d�eyeidedY�dZd[�Z�ee#j�eKe\f eei eei d\�d]d^�Z�G d_d`� d`e~�Z�G dadb� dbe{�Z�d�ee#j�e^eMf eieyedd�dedf�Z�d�eei eieej eet eei dg�dhdi�Z�d�e#j�eieieei dj�dkdl�Z�d�ee#j�eei f eieieei dm�dndo�Z�eWeydp�dqdr�Z�G dsd � d �Z�d�ejeyddu�dvdw�Z�eej ejeej dx�dydz�Z�d�eeeye�eif eeejejf d|�d}d~�Z�eei eei dd�d�d��Z�dS )�z{
Internal module for formatting output data in csv, html,
and latex files. This module also applies to display formatting.
� )�contextmanager)�
QUOTE_NONE�QUOTE_NONNUMERIC)�tzinfoN)�partial)�StringIO)�get_terminal_size)�IO�
TYPE_CHECKING�Any�Callable�Dict�Iterable�List�Mapping�Optional�Sequence�Tuple�Type�Union�cast)�east_asian_width)�
get_option�
set_option)�lib)�NA)�format_array_from_datetime)�NaT� Timedelta� Timestamp�iNaT)�NaTType)�FilePathOrBuffer�Label)�AbstractMethodError)
�is_categorical_dtype�is_complex_dtype�is_datetime64_dtype�is_datetime64tz_dtype�is_extension_array_dtype�is_float�is_float_dtype�
is_integer�is_integer_dtype�is_list_like�is_numeric_dtype� is_scalar�is_timedelta64_dtype)�isna�notna)�
DatetimeArray)�TimedeltaArray)�PandasObject)�
extract_array)�Index�
MultiIndex�PeriodIndex�ensure_index)�
DatetimeIndex)�TimedeltaIndex)�stringify_path)�adjoin�justify�pprint_thing)�Categorical� DataFrame�Series.�EngFormattera�
Parameters
----------
buf : str, Path or StringIO-like, optional, default None
Buffer to write to. If None, the output is returned as a string.
columns : sequence, optional, default None
The subset of columns to write. Writes all columns by default.
col_space : %(col_space_type)s, optional
%(col_space)s.
header : %(header_type)s, optional
%(header)s.
index : bool, optional, default True
Whether to print index (row) labels.
na_rep : str, optional, default 'NaN'
String representation of NAN to use.
formatters : list, tuple or dict of one-param. functions, optional
Formatter functions to apply to columns' elements by position or
name.
The result of each function must be a unicode string.
List/tuple must be of length equal to the number of columns.
float_format : one-parameter function, optional, default None
Formatter function to apply to columns' elements if they are
floats. The result of this function must be a unicode string.
sparsify : bool, optional, default True
Set to False for a DataFrame with a hierarchical index to print
every multiindex key at each row.
index_names : bool, optional, default True
Prints the names of the indexes.
justify : str, default None
How to justify the column labels. If None uses the option from
the print configuration (controlled by set_option), 'right' out
of the box. Valid values are
* left
* right
* center
* justify
* justify-all
* start
* end
* inherit
* match-parent
* initial
* unset.
max_rows : int, optional
Maximum number of rows to display in the console.
min_rows : int, optional
The number of rows to display in the console in a truncated repr
(when number of rows is above `max_rows`).
max_cols : int, optional
Maximum number of columns to display in the console.
show_dimensions : bool, default False
Display DataFrame dimensions (number of rows by number of columns).
decimal : str, default '.'
Character recognized as decimal separator, e.g. ',' in Europe.
�left�right�centerr@ �justify-all�start�end�inherit�match-parent�initial�unsetz�
Returns
-------
str or None
If buf is None, returns the result as a string. Otherwise returns
None.
c @ sZ e Zd Zddeee eeed�dd�Zed�d d
�Ze e d�dd�Z
ed�d
d�ZdS )�CategoricalFormatterNT�NaNrB )�categorical�buf�length�na_rep�footerc C s8 || _ |d k r|ntd�| _|| _|| _|| _t| _d S )N� )rR r rS rU rT rV r �quoting)�selfrR rS rT rU rV � rZ �:/tmp/pip-build-5_djhm0z/pandas/pandas/io/formats/format.py�__init__� s zCategoricalFormatter.__init__)�returnc C sP d}| j r*|r|d7 }|dt| j�� �7 }| jj� }|r@|d7 }||7 }t|�S )NrW z, zLength: �
)rT �lenrR �_repr_categories_info�str)rY rV �
level_inforZ rZ r[ �_get_footer� s
z CategoricalFormatter._get_footerc C s t | jj� d d | j| jd�S )N)�float_formatrU rX )�format_arrayrR �_internal_get_valuesrU rX )rY rZ rZ r[ �_get_formatted_values� s z*CategoricalFormatter._get_formatted_valuesc C s| | j }t|�dkr$| jr | j� S dS | j� }dd� |D �}dj|�}d| d g}| jrn| j� }|rn|j|� tdj|��S ) Nr rW c S s g | ]}|j � �qS rZ )�strip)�.0�irZ rZ r[ �
<listcomp>� s z2CategoricalFormatter.to_string.<locals>.<listcomp>z, �[�]r^ )rR r_ rV rc rg �join�appendra )rY rR �
fmt_values�values�resultrV rZ rZ r[ � to_string� s
zCategoricalFormatter.to_string)NTrQ T)�__name__�
__module__�__qualname__r r ra �boolr\ rc r rg rs rZ rZ rZ r[ rP � s rP c
@ s� e Zd Zddeee eeef eeeeee eee ee d�dd�Z dd �d
d�Z
ed �dd
�Zee
e ef d �dd�Ze
e d �dd�Zed �dd�ZdS )�SeriesFormatterNTrQ FrD )�seriesrS rT �header�indexrU �namerd �dtype�max_rows�min_rowsc C st || _ |d k r|nt� | _|| _|| _|| _|| _|| _|
| _|| _ |d krTt
d�}|| _| | _t
� | _| j� d S )Nzdisplay.float_format)ry r rS r| rU rz rT r{ r~ r r rd r} �_get_adjustment�adj�
_chk_truncate)rY ry rS rT rz r{ rU r| rd r} r~ r rZ rZ r[ r\ � s zSeriesFormatter.__init__)r] c C s� ddl m} | | j}| j}|o,t| j�|k}| j}|r�tt|�}|rPt||�}|dkrl|}|j d |� }n*|d }||j d |� |j | d � f�}|| _
nd | _
|| _|| _d S )Nr )�concat� � )
�pandas.core.reshape.concatr� r r~ r_ ry r �int�min�iloc�
tr_row_num� tr_series�
truncate_v)rY r� r r~ r� ry �row_numrZ rZ r[ r� s& |