3
���h,� �
@ s� d Z ddlZddlmZ ddlZddlmZ ddlZddlmZ ddl m
Z
mZmZm
Z
mZmZmZmZmZmZmZmZmZmZmZmZmZmZ ddlZddlZddlj Z ddl!m"Z" ddl#m$Z%m&Z&m'Z' dd l(m)Z) dd
l*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5 ddl6m7Z7 ddl8m9Z9 dd
l:m;Z< ddl=m>Z>m?Z?m@Z@mAZAmBZB ddlCmDZDmEZEmFZF ddlGmHZHmIZImJZJmKZKmLZLmMZMmNZNmOZOmPZPmQZQmRZRmSZSmTZT ddlUmVZVmWZWmXZXmYZYmZZZm[Z[m\Z\m]Z]m^Z^m_Z_m`Z`maZambZbmcZcmdZdmeZemfZfmgZgmhZhmiZimjZj ddlkmlZlmmZmmnZn ddlompZpmqZrmsZsmtZt ddlumvZv ddlwmxZxmyZy ddlzm{Z{m|Z| ddl}m~Z ddl�m�Z� ddl�m�Z� ddl�m�Z�m�Z� ddl�m�Z� ddl�m�Z�m�Z�m�Z� ddl�m�Z� ddl�m�Z�m�Z� ddl�m�Z� dd l�m�Z�m�Z� dd!l�m�Z� dd"l�m�Z�m�Z�m�Z�m�Z�m�Z�m�Z�m�Z�m�Z�m�Z� dd#l�m�Z� dd$l�m�Z� dd%l�m�Z� dd&l�m�Z� dd'l�m�Z�m�Z� dd(l�m�Z� ddl�Z�e�r�dd)l�m�Z� dd*l�m�Z� e�d+d,d-d.d/d0d1d2d3�Z�d4Z�d5Z�G d6d,� d,e��Z�e�j�� e�j�� etj�e�� etj�e�� d7d8� Z�dS )9a#
DataFrame
---------
An efficient 2D container for potentially mixed-type time series or other
labeled data series.
Similar to its R counterpart, data.frame, except providing automatic data
alignment and a host of useful data manipulation methods having to do with the
labeling information
� N)�abc)�StringIO)�dedent)�IO�
TYPE_CHECKING�Any�AnyStr�Dict� FrozenSet�Hashable�Iterable�Iterator�List�Mapping�Optional�Sequence�Set�Tuple�Type�Union�cast)�
get_option)�algos�lib�
properties)�
no_default)� ArrayLike�Axes�Axis�Dtype�FilePathOrBuffer�FrameOrSeriesUnion�IndexKeyFunc�Label�Level�Renamer�ValueKeyFunc)�PY37)�import_optional_dependency)�function)�Appender�Substitution�deprecate_kwarg�doc�rewrite_axis_style_signature)�validate_axis_style_args�validate_bool_kwarg�validate_percentile)
�cast_scalar_to_array�coerce_to_dtypes�"construct_1d_arraylike_from_scalar�find_common_type�infer_dtype_from_scalar�invalidate_string_dtypes�maybe_cast_to_datetime�maybe_convert_platform�maybe_downcast_to_dtype�maybe_infer_to_datetimelike�maybe_upcast�maybe_upcast_putmask�validate_numeric_casting)�ensure_int64�ensure_platform_int�infer_dtype_from_object�
is_bool_dtype�is_dataclass�is_datetime64_any_dtype�is_dict_like�is_dtype_equal�is_extension_array_dtype�is_float_dtype�is_hashable�
is_integer�is_integer_dtype�is_iterator�is_list_like�is_named_tuple�is_object_dtype� is_scalar�is_sequence�needs_i8_conversion�pandas_dtype)�isna�na_value_for_dtype�notna)�
algorithms�common�nanops�ops)�CachedAccessor)�reconstruct_func�relabel_result)�Categorical�ExtensionArray)�DatetimeLikeArrayMixin)�SparseFrameAccessor)�
extract_array)�NDFrame�_shared_docs)�base)�Index�ensure_index�ensure_index_from_sequences)�
DatetimeIndex)�
MultiIndex�maybe_droplevels)�PeriodIndex)�check_bool_indexer�convert_to_index_sliceable)�BlockManager) �
arrays_to_mgr�dataclasses_to_dicts�get_names_from_index� init_dict�init_ndarray�masked_rec_array_to_mgr�reorder_arrays�sanitize_index� to_arrays)�melt)�Series)�ensure_key_mapped)�get_filepath_or_buffer)�console�format)�
DataFrameInfo)�DataFrameGroupBy)�Stylerzindex, columns� DataFramez{0 or 'index', 1 or 'columns'}z�axis : {0 or 'index', 1 or 'columns'}, default 0
If 0 or 'index': apply function to each column.
If 1 or 'columns': apply function to each row.a�
by : str or list of str
Name or list of names to sort by.
- if `axis` is 0 or `'index'` then `by` may contain index
levels and/or column labels.
- if `axis` is 1 or `'columns'` then `by` may contain column
levels and/or index labels.
.. versionchanged:: 0.23.0
Allow specifying index or column level names.� zhlabels : array-like, optional
New labels / index to conform the axis specified by 'axis' to.z�axis : int or str, optional
Axis to target. Can be either the axis name ('index', 'columns')
or number (0, 1).)�axes�klassZaxes_single_arg�axisZoptional_byZversionadded_to_excelZoptional_labelsZ
optional_axisz�numeric_only : boolean, default None
Include only float, int, boolean data. If None, will attempt to use
everything, then use only numeric data
a�
Merge DataFrame or named Series objects with a database-style join.
The join is done on columns or indexes. If joining columns on
columns, the DataFrame indexes *will be ignored*. Otherwise if joining indexes
on indexes or indexes on a column or columns, the index will be passed on.
Parameters
----------%s
right : DataFrame or named Series
Object to merge with.
how : {'left', 'right', 'outer', 'inner'}, default 'inner'
Type of merge to be performed.
* left: use only keys from left frame, similar to a SQL left outer join;
preserve key order.
* right: use only keys from right frame, similar to a SQL right outer join;
preserve key order.
* outer: use union of keys from both frames, similar to a SQL full outer
join; sort keys lexicographically.
* inner: use intersection of keys from both frames, similar to a SQL inner
join; preserve the order of the left keys.
on : label or list
Column or index level names to join on. These must be found in both
DataFrames. If `on` is None and not merging on indexes then this defaults
to the intersection of the columns in both DataFrames.
left_on : label or list, or array-like
Column or index level names to join on in the left DataFrame. Can also
be an array or list of arrays of the length of the left DataFrame.
These arrays are treated as if they are columns.
right_on : label or list, or array-like
Column or index level names to join on in the right DataFrame. Can also
be an array or list of arrays of the length of the right DataFrame.
These arrays are treated as if they are columns.
left_index : bool, default False
Use the index from the left DataFrame as the join key(s). If it is a
MultiIndex, the number of keys in the other DataFrame (either the index
or a number of columns) must match the number of levels.
right_index : bool, default False
Use the index from the right DataFrame as the join key. Same caveats as
left_index.
sort : bool, default False
Sort the join keys lexicographically in the result DataFrame. If False,
the order of the join keys depends on the join type (how keyword).
suffixes : list-like, default is ("_x", "_y")
A length-2 sequence where each element is optionally a string
indicating the suffix to add to overlapping column names in
`left` and `right` respectively. Pass a value of `None` instead
of a string to indicate that the column name from `left` or
`right` should be left as-is, with no suffix. At least one of the
values must not be None.
copy : bool, default True
If False, avoid copy if possible.
indicator : bool or str, default False
If True, adds a column to the output DataFrame called "_merge" with
information on the source of each row. The column can be given a different
name by providing a string argument. The column will have a Categorical
type with the value of "left_only" for observations whose merge key only
appears in the left DataFrame, "right_only" for observations
whose merge key only appears in the right DataFrame, and "both"
if the observation's merge key is found in both DataFrames.
validate : str, optional
If specified, checks if merge is of specified type.
* "one_to_one" or "1:1": check if merge keys are unique in both
left and right datasets.
* "one_to_many" or "1:m": check if merge keys are unique in left
dataset.
* "many_to_one" or "m:1": check if merge keys are unique in right
dataset.
* "many_to_many" or "m:m": allowed, but does not result in checks.
Returns
-------
DataFrame
A DataFrame of the two merged objects.
See Also
--------
merge_ordered : Merge with optional filling/interpolation.
merge_asof : Merge on nearest keys.
DataFrame.join : Similar method using indices.
Notes
-----
Support for specifying index levels as the `on`, `left_on`, and
`right_on` parameters was added in version 0.23.0
Support for merging named Series objects was added in version 0.24.0
Examples
--------
>>> df1 = pd.DataFrame({'lkey': ['foo', 'bar', 'baz', 'foo'],
... 'value': [1, 2, 3, 5]})
>>> df2 = pd.DataFrame({'rkey': ['foo', 'bar', 'baz', 'foo'],
... 'value': [5, 6, 7, 8]})
>>> df1
lkey value
0 foo 1
1 bar 2
2 baz 3
3 foo 5
>>> df2
rkey value
0 foo 5
1 bar 6
2 baz 7
3 foo 8
Merge df1 and df2 on the lkey and rkey columns. The value columns have
the default suffixes, _x and _y, appended.
>>> df1.merge(df2, left_on='lkey', right_on='rkey')
lkey value_x rkey value_y
0 foo 1 foo 5
1 foo 1 foo 8
2 foo 5 foo 5
3 foo 5 foo 8
4 bar 2 bar 6
5 baz 3 baz 7
Merge DataFrames df1 and df2 with specified left and right suffixes
appended to any overlapping columns.
>>> df1.merge(df2, left_on='lkey', right_on='rkey',
... suffixes=('_left', '_right'))
lkey value_left rkey value_right
0 foo 1 foo 5
1 foo 1 foo 8
2 foo 5 foo 5
3 foo 5 foo 8
4 bar 2 bar 6
5 baz 3 baz 7
Merge DataFrames df1 and df2, but raise an exception if the DataFrames have
any overlapping columns.
>>> df1.merge(df2, left_on='lkey', right_on='rkey', suffixes=(False, False))
Traceback (most recent call last):
...
ValueError: columns overlap but no suffix specified:
Index(['value'], dtype='object')
c s e Zd ZU dZddhejB ZdZeed d�dd��Z e
Zee
eje
g �B Zee dhZee ed d
� �Z�d�ee ee ee ed
�dd�Zeee d�dd��Zeeeef d�dd��Zeed�dd��Zeed�dd��Zed�dd�Z �d�eed�dd�Z!ed�dd�Z"ed�dd �Z#ee d�d!d"�Z$e%d#d$d%d&d'�e%e&j'e&j(d(��d�ee)e ee*e ee e+ee*e f eeee&j, ee&j- ee eee ee ee ee eeee ee ee ee d,�d-d.���Z.ed/d�d0d1��Z/d2e0d3< |