
    PiQ                      d Z ddlmZ ddlmZmZ ddlZddlmZ ddl	Z	ddl
mZmZmZmZ ddlZddlZddlZddlmZmZmZmZ ddlmZ dd	lmZmZ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/ ddl0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z= ddl>m?Z?m@Z@ ddlAmBZBmCZC ddlDmEZEmFZF ddlGmHZHmIZImJZJmKZKmLZL ddlMmNc mOZP ddlQmRZRmSZSmTZT ddlUmVZV ddlWmNc mXZY ddlZm[Z[m\Z\ ddl]m^Z^ ddl_m`Z`maZa erddlGmbZb ddlcmdZd ddlQmeZe ddlfmgZg ejh        eji        ejj        eji        ejk        ejl        ejm        ejn        ejo        ejp        ejq        ejr        ejs        ejt        eju        ejv        ejw        ejx        ejy        ejx        ejz        ej{        ej|        ej}        ej~        ej        ej        ej        ej        ej        iZej        ejk        ur= ej        ej                  j        dk    rejl        eej        <   neji        eej        <   ej        ejs        ur= ej        ej                  j        dk    rejt        eej        <   nejr        eej        <   ej        eTeJeCfZ e)d          ddddd d d d!ej        d dfdd9            Z	 	 	 	 	 	 	 	 	 ddd:Zdd;Z e)d          	 	 	 	 	 	 	 	 ddd@            Z e)d          	 	 	 	 	 	 	 	 	 	 	 	 dddH            Z G dI dJ          Z	 	 dddOZ	 	 dddRZddZZ G d[ d\e          Zdd]Z G d^ d_e          ZddbZddcZddgZ	 dddkZ	 	 dddoZddqZddtZdd{Zdd|Zdd}ZddZddZdS )z
SQL-style merge routines
    )annotations)HashableSequenceN)partial)TYPE_CHECKINGLiteralcastfinal)	Timedelta	hashtablejoinlib)is_range_indexer)AnyArrayLike	ArrayLike
IndexLabelJoinHowMergeHowShapeSuffixesnpt)
MergeError)cache_readonly
set_module)find_stack_level)ExtensionDtype)find_common_type)ensure_int64ensure_objectis_boolis_bool_dtypeis_float_dtype
is_integeris_integer_dtypeis_list_like	is_numberis_numeric_dtypeis_object_dtypeis_string_dtypeneeds_i8_conversion)CategoricalDtypeDatetimeTZDtype)ABCDataFrame	ABCSeries)isnana_value_for_dtype)
ArrowDtypeCategoricalIndex
MultiIndexSeries)ArrowExtensionArrayBaseMaskedArrayExtensionArray)StringDtype)ensure_wrapped_if_datetimelikeextract_array)default_index)get_group_indexis_int64_overflow_possible)	DataFrame)groupby)DatetimeArray)
FrozenList   pandasinnerF_x_yleftDataFrame | Seriesrighthowr   on IndexLabel | AnyArrayLike | Noneleft_onright_on
left_indexboolright_indexsortsuffixesr   copybool | lib.NoDefault	indicator
str | boolvalidate
str | Nonereturnr?   c                   t          |           }|                     |
           t          |          }|dk    rt          |||||||||	||          S t          ||||||||||	||          }|                                S )a  
    Merge DataFrame or named Series objects with a database-style join.

    A named Series object is treated as a DataFrame with a single named column.

    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.
    When performing a cross merge, no column specifications to merge on are
    allowed.

    .. warning::

        If both key columns contain rows where the key is a null value, those
        rows will be matched against each other. This is different from usual SQL
        join behaviour and can lead to unexpected results.

    Parameters
    ----------
    left : DataFrame or named Series
        First pandas object to merge.
    right : DataFrame or named Series
        Second pandas object to merge.
    how : {'left', 'right', 'outer', 'inner', 'cross', 'left_anti', 'right_anti},
        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.
        * cross: creates the cartesian product from both frames, preserves the order
          of the left keys.
        * left_anti: use only keys from left frame that are not in right frame, similar
          to SQL left anti join; preserve key order.
        * right_anti: use only keys from right frame that are not in left frame, similar
          to SQL right anti join; preserve key order.
    on : Hashable or a sequence of the previous
        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 : Hashable or a sequence of the previous, 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 : Hashable or a sequence of the previous, 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 False
        This keyword is now ignored; changing its value will have no
        impact on the method.

        .. deprecated:: 3.0.0

            This keyword is ignored and will be removed in pandas 4.0. Since
            pandas 3.0, this method always returns a new object using a lazy
            copy mechanism that defers copies until necessary
            (Copy-on-Write). See the `user guide on Copy-on-Write
            <https://pandas.pydata.org/docs/dev/user_guide/copy_on_write.html>`__
            for more details.

    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.

    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  bar        2  bar        6
    3  baz        3  baz        7
    4  foo        5  foo        5
    5  foo        5  foo        8

    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  bar           2  bar            6
    3  baz           3  baz            7
    4  foo           5  foo            5
    5  foo           5  foo            8

    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='str')

    >>> df1 = pd.DataFrame({"a": ["foo", "bar"], "b": [1, 2]})
    >>> df2 = pd.DataFrame({"a": ["foo", "baz"], "c": [3, 4]})
    >>> df1
          a  b
    0   foo  1
    1   bar  2
    >>> df2
          a  c
    0   foo  3
    1   baz  4

    >>> df1.merge(df2, how="inner", on="a")
          a  b  c
    0   foo  1  3

    >>> df1.merge(df2, how="left", on="a")
          a  b  c
    0   foo  1  3.0
    1   bar  2  NaN

    >>> df1 = pd.DataFrame({"left": ["foo", "bar"]})
    >>> df2 = pd.DataFrame({"right": [7, 8]})
    >>> df1
        left
    0   foo
    1   bar
    >>> df2
        right
    0   7
    1   8

    >>> df1.merge(df2, how="cross")
       left  right
    0   foo      7
    1   foo      8
    2   bar      7
    3   bar      8
    cross)	rM   rO   rP   rQ   rS   rT   rU   rX   rZ   
rL   rM   rO   rP   rQ   rS   rT   rU   rX   rZ   )_validate_operand_check_copy_deprecation_cross_merge_MergeOperation
get_result)rI   rK   rL   rM   rO   rP   rQ   rS   rT   rU   rV   rX   rZ   left_dfright_dfops                   m/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/pandas/core/reshape/merge.pymergeri      s    |  %%G  &&& ''H
g~~!#
 
 
 	
 !#
 
 
 }}    c                    |s|s|||t          d          dt          j                     } | j        di |di}  |j        di |di}|gx}}t	          | |d||||||||	|
          }||= |S )z,
    See merge.__doc__ with how='cross'
    NzMCan not pass on, right_on, left_on or set right_index=True or left_index=True_cross_   rE   r_    )r   uuiduuid4assignri   )rI   rK   rM   rO   rP   rQ   rS   rT   rU   rX   rZ   	cross_colress                rh   rb   rb     s    $ 	



 >
 
 	

 )$*,,((I4;(()Q((DEL**Iq>**E#$Gh
  C 	IJrj   c                H   g }t          | t          t          f          s| g} |                    | d          }d}t	          fd| D                       r                    | d          }|j                            |j                  D ]\  }}|}	n	                     |j	        |                   }	n# t          $ r~ |j                                        fdj        D             z   }
|                    |
          }t          t          |                    |_        |                    |           Y w xY w |||	          }||| <   |                    |           ddlm}  ||d	
          }|                    |d         j                  }||fS )z
    groupby & merge; we are always performing a left-by type operation

    Parameters
    ----------
    by: field to group
    left: DataFrame
    right: DataFrame
    merge_pieces: function for merging
    FrT   Nc              3  *   K   | ]}|j         v V  d S Ncolumns).0itemrK   s     rh   	<genexpr>z%_groupby_and_merge.<locals>.<genexpr>  s*      
0
0T45= 
0
0
0
0
0
0rj   c                6    g | ]}|t                    v|S rn   )set)rz   rlcolss     rh   
<listcomp>z&_groupby_and_merge.<locals>.<listcomp>  s)    PPPaASZZ<O<O<O<O<Orj   rx   r   concatT)ignore_index)
isinstancelisttupler@   all_grouperget_iterator_selected_objtakeindicesKeyErrorry   tolistreindexrangelenindexappendpandas.core.reshape.concatr   )byrI   rK   merge_piecespieceslbyrbykeylhsrhscolsmergedr   resultr   s     `           @rh   _groupby_and_merger     s    Fb4-(( T
,,r,
&
&CCGC 
0
0
0
0R
0
0
000 ,mmBUm++L--c.?@@  S;CC	jjS!122   **,,PPPP5=PPPPT22$S[[11f%%% c3'' r
f 211111VF...F^^F1I$5^66F3;s    B::BEEouterIndexLabel | Nonefill_methodr   c
                d  	 d	fd||t          d          |}t          |t                    r|g}t          |                              | j                  }
t          |
          dk    rt          |
 d          t          || |fd          \  }}n|}t          |t                    r|g}t          |                              |j                  }
t          |
          dk    rt          |
 d	          t          ||| fd
          \  }}n | |          }|S )a  
    Perform a merge for ordered data with optional filling/interpolation.

    Designed for ordered data like time series data. Optionally
    perform group-wise merge (see examples).

    Parameters
    ----------
    left : DataFrame or named Series
        First pandas object to merge.
    right : DataFrame or named Series
        Second pandas object to merge.
    on : Hashable or a sequence of the previous
        Field names to join on. Must be found in both DataFrames.
    left_on : Hashable or a sequence of the previous, or array-like
        Field names to join on in left DataFrame. Can be a vector or list of
        vectors of the length of the DataFrame to use a particular vector as
        the join key instead of columns.
    right_on : Hashable or a sequence of the previous, or array-like
        Field names to join on in right DataFrame or vector/list of vectors per
        left_on docs.
    left_by : column name or list of column names
        Group left DataFrame by group columns and merge piece by piece with
        right DataFrame. Must be None if either left or right are a Series.
    right_by : column name or list of column names
        Group right DataFrame by group columns and merge piece by piece with
        left DataFrame. Must be None if either left or right are a Series.
    fill_method : {'ffill', None}, default None
        Interpolation method for data.
    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.

    how : {'left', 'right', 'outer', 'inner'}, default 'outer'
        * left: use only keys from left frame (SQL: left outer join)
        * right: use only keys from right frame (SQL: right outer join)
        * outer: use union of keys from both frames (SQL: full outer join)
        * inner: use intersection of keys from both frames (SQL: inner join).

    Returns
    -------
    DataFrame
        The merged DataFrame output type will be the same as
        'left', if it is a subclass of DataFrame.

    See Also
    --------
    merge : Merge with a database-style join.
    merge_asof : Merge on nearest keys.

    Examples
    --------
    >>> from pandas import merge_ordered
    >>> df1 = pd.DataFrame(
    ...     {
    ...         "key": ["a", "c", "e", "a", "c", "e"],
    ...         "lvalue": [1, 2, 3, 1, 2, 3],
    ...         "group": ["a", "a", "a", "b", "b", "b"],
    ...     }
    ... )
    >>> df1
      key  lvalue group
    0   a       1     a
    1   c       2     a
    2   e       3     a
    3   a       1     b
    4   c       2     b
    5   e       3     b

    >>> df2 = pd.DataFrame({"key": ["b", "c", "d"], "rvalue": [1, 2, 3]})
    >>> df2
      key  rvalue
    0   b       1
    1   c       2
    2   d       3

    >>> merge_ordered(df1, df2, fill_method="ffill", left_by="group")
      key  lvalue group  rvalue
    0   a       1     a     NaN
    1   b       1     a     1.0
    2   c       2     a     2.0
    3   d       2     a     3.0
    4   e       3     a     3.0
    5   a       1     b     NaN
    6   b       1     b     1.0
    7   c       2     b     2.0
    8   d       2     b     3.0
    9   e       3     b     3.0
    r\   r?   c           
     Z    t          | |          }|                                S )N)rM   rO   rP   rU   r   rL   )_OrderedMergerd   )	xyrg   r   rL   rO   rM   rP   rU   s	      rh   _mergerzmerge_ordered.<locals>._mergerk  s?    #	
 	
 	
 }}rj   Nz*Can only group either left or right framesr   z not found in left columnsc                     | |          S rw   rn   r   r   r   s     rh   <lambda>zmerge_ordered.<locals>.<lambda>  s    ''RSUV-- rj   z not found in right columnsc                     ||           S rw   rn   r   s     rh   r   zmerge_ordered.<locals>.<lambda>  s    1 rj   r\   r?   )	
ValueErrorr   strr~   
differencery   r   r   r   )rI   rK   rM   rO   rP   left_byright_byr   rU   rL   checkr   _r   s     ```  ```   @rh   merge_orderedr      s   V           x3EFFFgs## 	 iGG''55u::??e???@@@&we=W=W=W=WXX			h$$ 	" zHH((77u::??e@@@AAA&eT#=#=#=#=
 
	 u%%Mrj   Tbackward	toleranceint | datetime.timedelta | Noneallow_exact_matches	directionr   c                f    t          | |||||||||	|
d|||          }|                                S )a$  
    Perform a merge by key distance.

    This is similar to a left-join except that we match on nearest
    key rather than equal keys. Both DataFrames must be first sorted by
    the merge key in ascending order before calling this function.
    Sorting by any additional 'by' grouping columns is not required.

    For each row in the left DataFrame:

      - A "backward" search selects the last row in the right DataFrame whose
        'on' key is less than or equal to the left's key.

      - A "forward" search selects the first row in the right DataFrame whose
        'on' key is greater than or equal to the left's key.

      - A "nearest" search selects the row in the right DataFrame whose 'on'
        key is closest in absolute distance to the left's key.

    Optionally match on equivalent keys with 'by' before searching with 'on'.

    Parameters
    ----------
    left : DataFrame or named Series
        First pandas object to merge.
    right : DataFrame or named Series
        Second pandas object to merge.
    on : label
        Field name to join on. Must be found in both DataFrames.
        The data MUST be in ascending order. Furthermore this must be
        a numeric column, such as datetimelike, integer, or float. ``on``
        or ``left_on`` / ``right_on`` must be given.
    left_on : label
        Field name to join on in left DataFrame. If specified, sort the left
        DataFrame by this column in ascending order before merging.
    right_on : label
        Field name to join on in right DataFrame. If specified, sort the right
        DataFrame by this column in ascending order before merging.
    left_index : bool
        Use the index of the left DataFrame as the join key.
    right_index : bool
        Use the index of the right DataFrame as the join key.
    by : column name or list of column names
        Match on these columns before performing merge operation. It is not required
        to sort by these columns.
    left_by : column name
        Field names to match on in the left DataFrame.
    right_by : column name
        Field names to match on in the right DataFrame.
    suffixes : 2-length sequence (tuple, list, ...)
        Suffix to apply to overlapping column names in the left and right
        side, respectively.
    tolerance : int or timedelta, optional, default None
        Select asof tolerance within this range; must be compatible
        with the merge index.
    allow_exact_matches : bool, default True

        - If True, allow matching with the same 'on' value
          (i.e. less-than-or-equal-to / greater-than-or-equal-to)
        - If False, don't match the same 'on' value
          (i.e., strictly less-than / strictly greater-than).

    direction : 'backward' (default), 'forward', or 'nearest'
        Whether to search for prior, subsequent, or closest matches.

    Returns
    -------
    DataFrame
        A DataFrame of the two merged objects, containing all rows from the
        left DataFrame and the nearest matches from the right DataFrame.

    See Also
    --------
    merge : Merge with a database-style join.
    merge_ordered : Merge with optional filling/interpolation.

    Examples
    --------
    >>> left = pd.DataFrame({"a": [1, 5, 10], "left_val": ["a", "b", "c"]})
    >>> left
        a left_val
    0   1        a
    1   5        b
    2  10        c

    >>> right = pd.DataFrame({"a": [1, 2, 3, 6, 7], "right_val": [1, 2, 3, 6, 7]})
    >>> right
       a  right_val
    0  1          1
    1  2          2
    2  3          3
    3  6          6
    4  7          7

    >>> pd.merge_asof(left, right, on="a")
        a left_val  right_val
    0   1        a          1
    1   5        b          3
    2  10        c          7

    >>> pd.merge_asof(left, right, on="a", allow_exact_matches=False)
        a left_val  right_val
    0   1        a        NaN
    1   5        b        3.0
    2  10        c        7.0

    >>> pd.merge_asof(left, right, on="a", direction="forward")
        a left_val  right_val
    0   1        a        1.0
    1   5        b        6.0
    2  10        c        NaN

    >>> pd.merge_asof(left, right, on="a", direction="nearest")
        a left_val  right_val
    0   1        a          1
    1   5        b          6
    2  10        c          7

    We can use indexed DataFrames as well.

    >>> left = pd.DataFrame({"left_val": ["a", "b", "c"]}, index=[1, 5, 10])
    >>> left
       left_val
    1         a
    5         b
    10        c

    >>> right = pd.DataFrame({"right_val": [1, 2, 3, 6, 7]}, index=[1, 2, 3, 6, 7])
    >>> right
       right_val
    1          1
    2          2
    3          3
    6          6
    7          7

    >>> pd.merge_asof(left, right, left_index=True, right_index=True)
       left_val  right_val
    1         a          1
    5         b          3
    10        c          7

    Here is a real-world times-series example

    >>> quotes = pd.DataFrame(
    ...     {
    ...         "time": [
    ...             pd.Timestamp("2016-05-25 13:30:00.023"),
    ...             pd.Timestamp("2016-05-25 13:30:00.023"),
    ...             pd.Timestamp("2016-05-25 13:30:00.030"),
    ...             pd.Timestamp("2016-05-25 13:30:00.041"),
    ...             pd.Timestamp("2016-05-25 13:30:00.048"),
    ...             pd.Timestamp("2016-05-25 13:30:00.049"),
    ...             pd.Timestamp("2016-05-25 13:30:00.072"),
    ...             pd.Timestamp("2016-05-25 13:30:00.075"),
    ...         ],
    ...         "ticker": [
    ...             "GOOG",
    ...             "MSFT",
    ...             "MSFT",
    ...             "MSFT",
    ...             "GOOG",
    ...             "AAPL",
    ...             "GOOG",
    ...             "MSFT",
    ...         ],
    ...         "bid": [720.50, 51.95, 51.97, 51.99, 720.50, 97.99, 720.50, 52.01],
    ...         "ask": [720.93, 51.96, 51.98, 52.00, 720.93, 98.01, 720.88, 52.03],
    ...     }
    ... )
    >>> quotes
                         time ticker     bid     ask
    0 2016-05-25 13:30:00.023   GOOG  720.50  720.93
    1 2016-05-25 13:30:00.023   MSFT   51.95   51.96
    2 2016-05-25 13:30:00.030   MSFT   51.97   51.98
    3 2016-05-25 13:30:00.041   MSFT   51.99   52.00
    4 2016-05-25 13:30:00.048   GOOG  720.50  720.93
    5 2016-05-25 13:30:00.049   AAPL   97.99   98.01
    6 2016-05-25 13:30:00.072   GOOG  720.50  720.88
    7 2016-05-25 13:30:00.075   MSFT   52.01   52.03

    >>> trades = pd.DataFrame(
    ...     {
    ...         "time": [
    ...             pd.Timestamp("2016-05-25 13:30:00.023"),
    ...             pd.Timestamp("2016-05-25 13:30:00.038"),
    ...             pd.Timestamp("2016-05-25 13:30:00.048"),
    ...             pd.Timestamp("2016-05-25 13:30:00.048"),
    ...             pd.Timestamp("2016-05-25 13:30:00.048"),
    ...         ],
    ...         "ticker": ["MSFT", "MSFT", "GOOG", "GOOG", "AAPL"],
    ...         "price": [51.95, 51.95, 720.77, 720.92, 98.0],
    ...         "quantity": [75, 155, 100, 100, 100],
    ...     }
    ... )
    >>> trades
                         time ticker   price  quantity
    0 2016-05-25 13:30:00.023   MSFT   51.95        75
    1 2016-05-25 13:30:00.038   MSFT   51.95       155
    2 2016-05-25 13:30:00.048   GOOG  720.77       100
    3 2016-05-25 13:30:00.048   GOOG  720.92       100
    4 2016-05-25 13:30:00.048   AAPL   98.00       100

    By default we are taking the asof of the quotes

    >>> pd.merge_asof(trades, quotes, on="time", by="ticker")
                         time ticker   price  quantity     bid     ask
    0 2016-05-25 13:30:00.023   MSFT   51.95        75   51.95   51.96
    1 2016-05-25 13:30:00.038   MSFT   51.95       155   51.97   51.98
    2 2016-05-25 13:30:00.048   GOOG  720.77       100  720.50  720.93
    3 2016-05-25 13:30:00.048   GOOG  720.92       100  720.50  720.93
    4 2016-05-25 13:30:00.048   AAPL   98.00       100     NaN     NaN

    We only asof within 2ms between the quote time and the trade time

    >>> pd.merge_asof(
    ...     trades, quotes, on="time", by="ticker", tolerance=pd.Timedelta("2ms")
    ... )
                         time ticker   price  quantity     bid     ask
    0 2016-05-25 13:30:00.023   MSFT   51.95        75   51.95   51.96
    1 2016-05-25 13:30:00.038   MSFT   51.95       155     NaN     NaN
    2 2016-05-25 13:30:00.048   GOOG  720.77       100  720.50  720.93
    3 2016-05-25 13:30:00.048   GOOG  720.92       100  720.50  720.93
    4 2016-05-25 13:30:00.048   AAPL   98.00       100     NaN     NaN

    We only asof within 10ms between the quote time and the trade time
    and we exclude exact matches on time. However *prior* data will
    propagate forward

    >>> pd.merge_asof(
    ...     trades,
    ...     quotes,
    ...     on="time",
    ...     by="ticker",
    ...     tolerance=pd.Timedelta("10ms"),
    ...     allow_exact_matches=False,
    ... )
                         time ticker   price  quantity     bid     ask
    0 2016-05-25 13:30:00.023   MSFT   51.95        75     NaN     NaN
    1 2016-05-25 13:30:00.038   MSFT   51.95       155   51.97   51.98
    2 2016-05-25 13:30:00.048   GOOG  720.77       100     NaN     NaN
    3 2016-05-25 13:30:00.048   GOOG  720.92       100     NaN     NaN
    4 2016-05-25 13:30:00.048   AAPL   98.00       100     NaN     NaN
    asof)rM   rO   rP   rQ   rS   r   r   r   rU   rL   r   r   r   )
_AsOfMergerd   )rI   rK   rM   rO   rP   rQ   rS   r   r   r   rU   r   r   r   rg   s                  rh   
merge_asofr     sV    J 
/
 
 
B" ==??rj   c                  *   e Zd ZU dZdZded<   ded<   ded<   ded	<   d
ed<   d
ed<   d
ed<   ded<   ded<   ded<   ded<   ded<   ded<   	 	 	 	 	 	 	 	 	 	 dIdJd%ZedKd'            ZdLd(Z	dMd)Z
edNd0            ZdOd1ZeedPd2                        ZedQd4            ZedRd6            ZedSd7            ZedTd8            ZdUd:ZedVd<            Ze	 dWdXdA            ZedYdB            ZedZdD            Zed[dE            ZdF Zed\dH            ZdS )]rc   z
    Perform a database (SQL) merge operation between two DataFrame or Series
    objects using either columns as keys or their row indexes
    ri   JoinHow | Literal['asof']rL   r   rM   z!Sequence[Hashable | AnyArrayLike]rO   rP   rR   rQ   rS   rT   r   rU   rY   rX   r[   rZ   zlist[Hashable]
join_nameslist[ArrayLike]right_join_keysleft_join_keysrE   NFTrF   rI   rJ   rK   4JoinHow | Literal['left_anti', 'right_anti', 'asof']rN   r\   Nonec                p   t          |          }t          |          }|x| _        | _        |x| _        | _        |                     |          \  | _        | _        t          j	        |          | _
        |
| _        |	p|dk    | _        || _        || _        || _        t!          |          st#          dt%          |                     t!          |          st#          dt%          |                     |j        j        |j        j        k    r,d|j        j         d|j        j         d}t+          |          |                     ||          \  | _        | _        |                                 \  | _        | _        | _        }}|r| j                            |          | _        |r| j                            |          | _        |                     | j        | j                   |                     | j                   |                                   || !                    |           d S d S )Nr   z/left_index parameter must be of type bool, not z0right_index parameter must be of type bool, not z0Not allowed to merge between different levels. (z levels on the left, z on the right))"r`   rI   	orig_leftrK   
orig_right_validate_howrL   	anti_joincommaybe_make_listrM   rU   rT   rQ   rS   rX   r    r   typery   nlevelsr   _validate_left_right_onrO   rP   _get_merge_keysr   r   r   _drop_labels_or_levels_maybe_require_matching_dtypes_validate_tolerance_maybe_coerce_merge_keys_validate_validate_kwd)selfrI   rK   rL   rM   rO   rP   rQ   rS   rT   rU   rX   rZ   _left_rightmsg	left_drop
right_drops                     rh   __init__z_MergeOperation.__init__  sW    "$''"5))%**	DN'--
T_#'#5#5c#:#: $.%b)) *C7N	$&"z"" 	T$zBRBRTT   {## 	V4CTCTVV  
 = FN$::::M): :>): : : 
 S//!&*&B&B7H&U&U#dm   ""	
 O  	D	88CCDI 	G:::FFDJ++D,?AUVVV  !4555 	%%'''
 ''11111  rj   &tuple[JoinHow | Literal['asof'], bool]c                    h d}||vrt          d| d          d}|dv r|                    d          d         }d}t          t          t          d	         z  |          }||fS )
zx
        Validate the 'how' parameter and return the actual join type and whether
        this is an anti join.
        >   r   rI   r^   rE   r   rK   	left_anti
right_anti'zZ' is not a valid Merge type: left, right, inner, outer, left_anti, right_anti, cross, asofF>   r   r   r   r   Tr   )r   splitr	   r   r   )r   rL   
merge_typer   s       rh   r   z_MergeOperation._validate_how  s    	
 	
 	

 j  QC Q Q Q   	---))C..#CI7WV_,c22I~rj   c                    d S rw   rn   )r   r   r   s      rh   r   z._MergeOperation._maybe_require_matching_dtypes.  s	     	rj   c                    d S rw   rn   )r   r   s     rh   r   z#_MergeOperation._validate_tolerance4  s    rj   
join_indexr3   left_indexernpt.NDArray[np.intp] | Noneright_indexerr?   c                j   | j         dd         }| j        dd         }t          | j         j        | j        j        | j                  \  }}|Yt          |t          |                    s<|j                            ||dddd          }|	                    ||j
                  }||_        |Yt          |t          |                    s<|j                            ||dddd          }	|	                    |	|	j
                  }||_        ddlm}
 ||_        ||_         |
||gd          }|S )	z?
        reindex along index and concat along columns.
        Nrm   T)axis
only_slice
allow_dupsuse_na_proxy)axesr   r   )r   )rI   rK   _items_overlap_with_suffix
_info_axisrU   r   r   _mgrreindex_indexer_constructor_from_mgrr   r   rD   r   ry   )r   r   r   r   rI   rK   llabelsrlabelslmgrrmgrr   r   s               rh   _reindex_and_concatz#_MergeOperation._reindex_and_concat8  sk    y|
1115I $*"7
 
 #,<\3t99,U,U# 9,,! -  D --d-CCD
$-=3u::.
 .
$ :--! .  D //49/EEE !!!!!!uA...rj   c                   | j         r-|                     | j        | j                  \  | _        | _        |                                 \  }}}|                     |||          }| j         r|                     |          }|                     |||           |                     |           |	                    t          j        | j        | j        g| j        | j                  d          S )z$
        Execute the merge.
        )
input_objsrI   rK   ri   )method)rX   _indicator_pre_mergerI   rK   _get_join_infor   _indicator_post_merge_maybe_add_join_keys_maybe_restore_index_levels__finalize__typesSimpleNamespace)r   r   r   r   r   s        rh   rd   z_MergeOperation.get_resultn  s     > 	U$($=$=di$T$T!DItz262E2E2G2G/
L-))*lMRR> 	8//77F!!&,FFF((000""! Itz2$*   	 # 
 
 	
rj   c                    t          | j        t                    r| j        S t          | j        t                    r| j        rdnd S t	          d          )N_mergez<indicator option can only accept boolean or string arguments)r   rX   r   rR   r   r   s    rh   _indicator_namez_MergeOperation._indicator_name  sZ     dnc** 	>!-- 	#~78847N  rj   tuple[DataFrame, DataFrame]c                   |j                             |j                   }dD ]}||v rt          d|           | j        |v rt          d          |                    d          }|                    d          }d|d<   |d                             d          |d<   d	|d
<   |d
                             d          |d
<   ||fS )a  
        Add one indicator column to each of the left and right inputs.

        These columns are used to produce another column in the output of the
        merge, indicating for each row of the output whether it was produced
        using the left, right or both inputs.
        )_left_indicator_right_indicatorzECannot use `indicator=True` option when data contains a column named z:Cannot use name of an existing column for indicator columnFdeeprm   r  int8   r  )ry   unionr   r  rV   astype)r   rI   rK   ry   is        rh   r   z$_MergeOperation._indicator_pre_merge  s    ,$$U]338 	 	AG|| 8458 8   
 7**L   yyey$$


&&"#"&'8"9"@"@"H"H$% !$)*<$=$D$DV$L$L !U{rj   r   c                b   |d                              d          |d<   |d                              d          |d<   t          |d         |d         z   g d          || j        <   || j                 j                            g d          || j        <   |                    ddgd          }|S )	z
        Add an indicator column to the merge result.

        This column indicates for each row of the output whether it was produced using
        the left, right or both inputs.
        r  r   r  )rm   r     )
categories)	left_only
right_onlybothrm   )labelsr   )fillnar2   r  catrename_categoriesdrop)r   r   s     rh   r  z%_MergeOperation._indicator_post_merge  s     %++<$=$D$DQ$G$G !%+,>%?%F%Fq%I%I!"'2%&0B)CC yy(
 (
 (
t#$ (. (

 C C CDD 	t#$ %68J$KRSTTrj   c                L   g }t          | j        | j        | j        d          D ]c\  }}}| j                            |          rC| j                            |          r)||k    r#||j        j        vr|	                    |           d|r|
                    |d           dS dS )a  
        Restore index levels specified as `on` parameters

        Here we check for cases where `self.left_on` and `self.right_on` pairs
        each reference an index level in their respective DataFrames. The
        joined columns corresponding to these pairs are then restored to the
        index of `result`.

        **Note:** This method has side effects. It modifies `result` in-place

        Parameters
        ----------
        result: DataFrame
            merge result

        Returns
        -------
        None
        TstrictinplaceN)zipr   rO   rP   r   _is_level_referencer   r   namesr   	set_index)r   r   names_to_restorenameleft_key	right_keys         rh   r  z+_MergeOperation._maybe_restore_index_levels  s    * ),OT\4=*
 *
 *
 	. 	.%D(I 228<<	. O77 . 	)) 222 ''--- 	=-t<<<<<	= 	=rj   c                   d }d }t          d | j        D                       sJ t          | j        | j        | j        d          }t          |          D ]\  }\  }}	t          ||	          sd\  }
}v r||Ӊ| j        v ra||dn|dk    	                                }|r@| j
        |         }         j        | j                 j        k    r| j                 j        }
n| j        v r`||dn|dk    	                                }|r@| j        |         }
         j        | j                 j        k    r| j                 j        }n| j        |         }
| j
        |         }|
|k|
         j        }nA||
}n<t          |
d          }
t          |
j                  }t!          j        |
||          }|         j        }nA||}n<t          |d          }t          |j                  }t!          j        |||          }|7|dk                                     rt%          ||j        d	          |j        }n|7|dk                                     rt%          ||j        d	          |j        }nt%          ||j        d	          ||dk    }                    | |          t)          |j        |j        g          }|j        j        d
k    r"|j        j        d
k    r|j        dk    rj        }                              r"                    |j                  <                                 rlt5          j        t6                    r:_        fdj        j        D             }                    |d           Rt%                    _        j                    |pd|            d S )Nc              3  @   K   | ]}t          |t                    V  d S rw   )r   _knownrz   r   s     rh   r|   z7_MergeOperation._maybe_add_join_keys.<locals>.<genexpr>  s,      FFQ:a((FFFFFFrj   Tr#  NNFextract_numpy
fill_valuedtyperV   MO)r:  r   c                T    g | ]$}|k    rj                             |          n%S rn   )r   get_level_values)rz   
level_namekey_colr,  r   s     rh   r   z8_MergeOperation._maybe_add_join_keys.<locals>.<listcomp>`  sN     $ $ $ !+ $.#5#5 !' = =j I I I%,	$ $ $rj   r%  r,  key_) r   r   r'  r   rO   rP   	enumerate_should_fillrI   anyr   r:  _valuesrK   r;   r0   algostake_ndr3   wherer   kind_is_label_reference_constructor_slicedr   r(  r   r4   r,  r)  r*  insert)r   r   r   r   left_has_missingright_has_missingkeysr  lnamername	take_left
take_rightlvalslfillrvalstakerrfillresult_dtype	mask_leftidx_listr@  r,  s    `                  @@rh   r  z$_MergeOperation._maybe_add_join_keys  s      FF$2EFFFFFFFF4?DL$-MMM'0 h	B h	B#A#eUu-- $.!Izv~~+}/Hty((+3 $0#7 !&&2b&8%=%=%?%? - , D)-)=a)@J%d|1TYt_5JJJ,0IdO,C	++,4 $1#8 !&&3r&9%>%>%@%@ . - F(,(;A(>I%d|1TZ5E5KKK-1Z-=-E
 !/2	!1!4
$
(>$"4L0EE!)%EE !.it L L LI.y??E!M)\eTTTE%"4L0EE"*&EE **DIIIE.u{;;E!M%5QQQE  +1C0H0H0J0J+#E5IIIG#(;LL".MR4G3L3L3N3N.#E5IIIG#(;LL#E5IIIG#/$0B$6	")--
E"B"B#3U[%+4N#O#OL(C//!K,33(-44 (/}--d33 B#)#=#=|6< $> $ $F4LL //55 B!&,
;; A'+$ $ $ $ $ $ /5l.@$ $ $ ((4(@@@@',W4'@'@'@MM!T%7ZAZZAAAQh	B h	Brj   ?tuple[npt.NDArray[np.intp] | None, npt.NDArray[np.intp] | None]c                j    | j         dk    sJ t          | j        | j        | j        | j                   S )return the join indexersr   rT   rL   )rL   get_join_indexersr   r   rT   r
  s    rh   _get_join_indexersz"_MergeOperation._get_join_indexerso  sA    
 x6!!!! !5DI48
 
 
 	
rj   Ftuple[Index, npt.NDArray[np.intp] | None, npt.NDArray[np.intp] | None]c                F   | j         j        }| j        j        }| j        r;| j        r4| j        dk    r)|                    || j        d| j                  \  }}}n| j        r.| j        dk    r#t          ||| j	        | j                  \  }}}nl| j        r.| j        dk    r#t          ||| j
        | j                  \  }}}n7|                                 \  }}| j        r_t          | j                   dk    r|                     |||d          }n||                                }n|                    |          }n| j        r| j        dk    r|                     |||d          }nt          | j                  dk    r|                     |||d          }n\||                                }nE|                    |          }n/|t          |          nt          |          }t!          |          }| j        r|                     |||          \  }}}|||fS )	Nr   TrL   return_indexersrT   rI   ru   rK   r   rL   )rI   r   rK   rQ   rS   rL   r   rT   _left_join_on_indexr   r   rb  r   _create_join_indexrV   r   r<   r   _handle_anti_join)r   left_axright_axr   r   r   ns          rh   r  z_MergeOperation._get_join_infoy  s    )/:#? 4	.t/ 4	.DH4F4F6=lldh49 7C 7 73Jmm  /	.$(f"4"46I4#6TY7 7 73Jmm _ *	.W!4!46I'4#7di7 7 73J|| -1,C,C,E,E)\= #.ty>>A%%!%!8!8 $#	 "9 " "JJ #*!)JJ!)}!=!=JJ .8v%%!%!8!8 $"	 "9 " "JJ __q((!%!8!8 %"	 "9 " "JJ ")!(JJ!(l!;!;JJ$0$8CLLLc,>O>O*1--
> 	6:6L6LL-7 73Jm <66rj   r   other_indexindexerr   c                   | j         |dfv rt          |t                    st|dk    }t          j        |          rZt          |j        d          }|j        st          |g          }nt          |g|j                  }|	                    |          }||
                                S |                    |          S )a  
        Create a join index by rearranging one index to match another

        Parameters
        ----------
        index : Index
            index being rearranged
        other_index : Index
            used to supply values not found in index
        indexer : np.ndarray[np.intp] or None
            how to rearrange index
        how : str
            Replacement is only necessary if indexer based on other_index.

        Returns
        -------
        Index
        r   r4  F)compatr:  )rL   r   r4   nprE  r0   r:  _can_hold_nar3   r   rV   r   )r   r   rn  ro  rL   maskr8  	new_indexs           rh   ri  z"_MergeOperation._create_join_index  s    4 8W~%%jj.Q.Q% b=Dvd|| 0/EJJJ
) G %zl 3 3II %zl%+ F F FIY//?::<<zz'"""rj   c                "   |&t          j        t          | j                            }|&t          j        t          | j                            }| j        dv sJ | j        dk    r|dk    }n|dk    }||         }||         }||         }|||fS )a  
        Handle anti join by returning the correct join index and indexers

        Parameters
        ----------
        join_index : Index
            join index
        left_indexer : np.ndarray[np.intp] or None
            left indexer
        right_indexer : np.ndarray[np.intp] or None
            right indexer

        Returns
        -------
        Index, np.ndarray[np.intp] or None, np.ndarray[np.intp] or None
        N>   rI   rK   rI   r4  )rs  aranger   rI   rK   rL   )r   r   r   r   filts        rh   rj  z!_MergeOperation._handle_anti_join  s    0 9S^^44L Ic$*oo66Mx,,,,,8v B&DD  2%D%
#D)%d+<66rj   Wtuple[list[ArrayLike], list[ArrayLike], list[Hashable], list[Hashable], list[Hashable]]c                
   g }g }g }g }g }| j         | j        cfd}fd}t          | j                  rt          | j                  rt          | j        | j        d          D ]\  }}	t          |d          }t          |	d          }	 ||          r
t          t          |          }|	                    |            ||	          r@t          t          |	          }	|	                    |	           |	                    d           t          t          |	          }	|	>|	                                        |	                     |	                    |	           |	                    j        j                   |	                    j        j                   > ||	          s}t          t          |	          }	|	)|	                                        |	                     n|	                    j        j                   |||	k    r|	                    |	           n*t          t          |	          }	|	                    |	           |Tt          t          |          }|	                                        |                     |	                    |           F|	                    j        j                   |	                    j        j                   nut          | j                  r'| j        D ]}
 ||
          rQt          |
d          }
t          t          |
          }
|	                    |
           |	                    d           ^t          t          |
          }
|	                                        |
                     |	                    |
           t          | j        j        t                     r<d t          | j        j        j        | j        j        j        d          D             }nM| j        j        j        g}n9t          | j                  r$| j        D ]}
t          |
d          }
 ||
          r@t          t          |
          }
|	                    |
           |	                    d           ^t          t          |
          }
|	                                        |
                     |	                    |
           t          | j         j        t                     r;d t          | j         j        j        | j         j        j        d          D             }n| j         j        j        g}|||||fS )	zj
        Returns
        -------
        left_keys, right_keys, join_names, left_drop, right_drop
        c                n    t          | t                    ot          |           t                    k    S rw   r   r1  r   )r   rI   s    rh   r   z1_MergeOperation._get_merge_keys.<locals>.<lambda>*  s'    Jq&11Ic!ffD		6I rj   c                n    t          | t                    ot          |           t                    k    S rw   r}  )r   rK   s    rh   r   z1_MergeOperation._get_merge_keys.<locals>.<lambda>+  s'    Jq&11Jc!ffE

6J rj   Tr#  r5  Nc                H    g | ]\  }}|j                             |           S rn   rF  r   rz   lev	lev_codess      rh   r   z3_MergeOperation._get_merge_keys.<locals>.<listcomp>u  s<       &Y K$$Y//  rj   c                H    g | ]\  }}|j                             |           S rn   r  r  s      rh   r   z3_MergeOperation._get_merge_keys.<locals>.<listcomp>  s<       &Y K$$Y//  rj   )rI   rK   _anyrO   rP   r'  r;   r	   r   r   r   _get_label_or_level_valuesr   rF  r,  r   r4   levelscodes)r   	left_keys
right_keysr   r   r   is_lkeyis_rkeylkrkkrI   rK   s              @@rh   r   z_MergeOperation._get_merge_keys  sx    &(	&(
%'
%'
$&	ieIIIIJJJJ  Z	6$t}"5"5 Z	6dlDM$GGG -; -;B"2T:::"2T:::72;; *;i,,B$$R(((wr{{ @!)R00"))"---"))$//// "(B//>&--e.N.Nr.R.RSSS&--b1111 '--ek.ABBB&--ek.>????"72;; . "(B//>&--e.N.Nr.R.RSSSS '--ek.ABBB>bBhh&--b111!)R00"))"---~ "(B//!(()H)H)L)LMMM"))"---- "(();<<<"))$*/::::[-;\ $, +	6\ ) )71:: 
)%at<<<AY**A$$Q'''%%d++++ Xq))A$$T%D%DQ%G%GHHH%%a(((($**J77 8 *-
(/1A1GPT+ + +  

 #j.67

$-   	6] ) )!!488871:: 	)Y**A%%a(((%%d++++ Xq))A%%e&F&Fq&I&IJJJ%%a(((($)/:66 6 *-	.	0Ed+ + +  		 "Y_45	*j)ZGGrj   c                   t          | j        | j        | j        d          D ]+\  }}}t	          |          rt	          |          rt	          |          st	          |          rDt          |d          }t          |d          }t          |j        t                    }t          |j        t                    }t          |j                  pt          |j                  }t          |j                  pt          |j                  }|rD|rBt          t          |          }t          t          |          }|                    |          r/n|s|rn|j        |j        k    rGd|j         d|j         d| d}t          |j                  r2t          |j                  r|j        j        |j        j        k    rt          |j        t                     rt          |j        t                     sst#          |j        |j        g          }	t          |	t                     r-|	                                }
|
                    ||	d	          }n|                    |	          }nt          |j        t                     rrt#          |j        |j        g          }	t          |	t                     r-|	                                }
|
                    ||	d	          }n|                    |	          }t+          |j                  rt-          |j                  rt/          j        d
          5  |                    |j                  }d d d            n# 1 swxY w Y   t/          j        |           }||k    }||                                         s(t7          j        dt:          t=                                 t-          |j                  rt+          |j                  rt/          j        d
          5  |                    |j                  }d d d            n# 1 swxY w Y   t/          j        |           }||k    }||                                         s(t7          j        dt:          t=                                 rt?          j         |d          t?          j         |d          k    rn|rtC          |j                  stC          |j                  r|rn]|rt          |j                  st          |j                  ra|r_t?          j         |d          }t?          j         |d          }g d}g d}||v r||v rn||v r||vs||v r||vrtE          |          ntG          |j                  r#tG          |j                  stE          |          tG          |j                  s#tG          |j                  rtE          |          t          |j        tH                    r)t          |j        tH                    stE          |          t          |j        tH                    s)t          |j        tH                    rtE          |          t          |j        tH                    rt          |j        tH                    s |j        j        dk    r|j        j        dk    r|j        j        dk    r|j        j        dk    rtE          |          |j        j        dk    r|j        j        dk    rtE          |          t          |j                  rt          |j                  r/|| j%        j&        v rp|rt          t          |          j'        j        ntP          }| j%        )                    d          | _%        | j%        |                             |          | j%        |<   || j*        j&        v rp|rt          t          |          j'        j        ntP          }| j*        )                    d          | _*        | j*        |                             |          | j*        |<   -d S )NTr#  r5  zYou are trying to merge on  and z columns for key 'z2'. If you wish to proceed you should use pd.concatFr9  ignore)invalidzjYou are merging on int and float columns where the float values are not equal to their int representation.)
stacklevel)skipna)integerzmixed-integerbooleanempty)stringunicodemixedbytesr  r;  mr  )+r'  r   r   r   r   r;   r   r:  r+   r(   r)   r	   r2   #_categories_match_up_to_permutationr'   rJ  r   r   construct_array_type_from_sequencer  r$   r"   rs  errstateisnanr   warningswarnUserWarningr   r   infer_dtyper!   r   r*   r,   rI   ry   r  objectrV   rK   )r   r  r  r,  	lk_is_cat	rk_is_catlk_is_object_or_stringrk_is_object_or_stringr   ctcom_clscastedru  matchinferred_leftinferred_right
bool_typesstring_typestyps                      rh   r   z(_MergeOperation._maybe_coerce_merge_keys  s     !5tt
 
 
 y	@ y	@LBD B B R SWW r666Br666B"28-=>>I"28-=>>I%4RX%>%> &/C C" &5RX%>%> &/C C"  
Y 
+r**+r**99"==   i RX%%Ubh U URX U U U U U   )) @.>rx.H.H @8=BHM11bh77 +
HnA A + *28RX*>??B!"n55 +"$"9"9";";$33Bbu3MMYYr]].99 +)28RX*>??B!"n55 +"$"9"9";";$33Bbu3MMYYr]] $BH-- .2J2J X666 5 5 "$28!4!4	5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 HRLL=D&LE !;??,,  I ('7'9'9    !"(++ 0@0J0J X666 5 5 "$28!4!4	5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 HRLL=D&LE !;??,,  I ('7'9'9     ?2e444u9 9 9    ) 4]28-D-D 4bh''4,B4  ) .-=bh-G-G . **./E. !$5 A A A!$E!B!B!BMMM
OOO !J..>Z3O3O "\11nL6X6X"l22}L7X7X$S//) %RX.. 7J287T7T  oo%(22 7J287T7T  oo%BHo66 z/@ @  !oo%/:: z/@ @  !oo%28_55rx99 (-3&&28=C+?+?#%%"(-3*>*> oo%#%%"(-3*>*> oo% ** rx/H/H  ty(((@IUd;++6<<v INNN66	"&)D/"8"8"="=	$tz)))@IUd;++6<<v!Z__%_88
#':d#3#:#:3#?#?
4 sy	@ y	@s$   6MM!	$M!	P++P/	2P/	c           
     d   t          j        |          }t          j        |          }| j        ||| j        r| j        rd\  }}n6| j        rt          d          | j        rt          d          | j        j        }| j        j        }|	                    |          }t          |          dk    r%t          d| d| d| j         d| j                   |                    |d	
          j        r|                    |d	
          j        st          d|          |x}}nU| j        ;||t          d          | j        s| j        rt          d          | j        x}}n|| j        rt          d          | j        s|t          d          | j        r|t          d          t          |          }| j        r7t          |          | j        j        j        k    rt          d          d g|z  }n}|{| j        rt          d          | j        s|t          d          t          |          }| j        r7t          |          | j        j        j        k    rt          d          d g|z  }t          |          t          |          k    rt          d          ||fS )N)rn   rn   z&Must pass right_on or right_index=Truez$Must pass left_on or left_index=Truer   z>No common columns to perform merge on. Merge options: left_on=z, right_on=z, left_index=z, right_index=rE   rg  zData columns not unique: zSCan only pass argument "on" OR "left_on" and "right_on", not a combination of both.zYCan only pass argument "on" OR "left_index" and "right_index", not a combination of both.z:Can only pass argument "left_on" OR "left_index" not both.z&Must pass "right_on" OR "right_index".z<Can only pass argument "right_on" OR "right_index" not both.zDlen(left_on) must equal the number of levels in the index of "right"z$Must pass "left_on" OR "left_index".zDlen(right_on) must equal the number of levels in the index of "left"z%len(right_on) must equal len(left_on))r   r   rM   rQ   rS   r   rI   ry   rK   intersectionr   r   	is_uniquer   r   r   )r   rO   rP   	left_cols
right_colscommon_colsrm  s          rh   r   z'_MergeOperation._validate_left_right_onY  si   %g..&x00 7?w83C 14#3 1$*! 1 !IJJJ! 1 !GHHH !I-	!Z/
'44Z@@{##q(($:29: :$,: : '+o: : (,'7	: :   "{@@JR%??;G?DDNR %%P%P%PQQQ%00((W "h&: A    $"2  D   "&(Ghh   P   # K(8 !IJJJ H$8 R   GA &w<<4:#3#;;;$<   !6A:!  R   ? Iw !GHHHHA %x==DIO$;;;$;    &1*x==CLL((DEEE  rj   r   c                     j         r j        j        }|j        }n t	          j         j                  }|j        } j        r j        j        } j        j        j        }n t	          j         j	                  }|j        }d fd}d fd}|dv rf|s(|s&t          d ||            ||                     |st          d	 ||                     |st          d
 ||                     d S |dv r|st          d ||                     d S |dv r|st          d ||                     d S |dv rd S t          d| d          )Nr   r3   r\   r   c                    j         sj        nt          j        }| |                                          d d                             |                              d          }d| dS )N   rA  Fr   z
Duplicates in left:
  ...)rQ   rO   r   
no_default
duplicatedto_frame	to_stringr   r,  r   r   s      rh   left_error_msgz>_MergeOperation._validate_validate_kwd.<locals>.left_error_msg  se    '+J4<<CNDALLNN#BQB'00d0;;EEEERRC7c7777rj   c                    j         sj        nt          j        }| |                                          d d                             |                              d          }d| dS )Nr  rA  Fr  z
Duplicates in right:
 r  )rS   rP   r   r  r  r  r  r  s      rh   right_error_msgz?_MergeOperation._validate_validate_kwd.<locals>.right_error_msg  sf    (,(8L4==cnDALLNN#BQB'00d0;;EEEERRC8s8888rj   )
one_to_onez1:1zRMerge keys are not unique in either left or right dataset; not a one-to-one merge.zAMerge keys are not unique in left dataset; not a one-to-one mergezBMerge keys are not unique in right dataset; not a one-to-one merge)one_to_manyz1:mzBMerge keys are not unique in left dataset; not a one-to-many merge)many_to_onezm:1zDMerge keys are not unique in right dataset; not a many-to-one merge
)many_to_manyzm:m"z" is not a valid argument. Valid arguments are:
- "1:1"
- "1:m"
- "m:1"
- "m:m"
- "one_to_one"
- "one_to_many"
- "many_to_one"
- "many_to_many")r   r3   r\   r   )rQ   r   r   r  r4   from_arraysr   rS   r   r   r   r   )r   rZ   left_join_indexleft_uniqueright_join_indexright_uniquer  r  s   `       rh   r   z&_MergeOperation._validate_validate_kwd  sp    ? 	4"n2O)3KK(4T5HIIO)3K 	6#4?0:LL)5d6JKK+5L	8 	8 	8 	8 	8 	8
	9 	9 	9 	9 	9 	9 ,,, |  ;%~o66; ''788; ;     9%~o669 9      ;&'788; ;    ///  9%~o669 9    ///  ;&'788; ;    000D 	#H 	# 	# 	#  rj   )
rE   NNNFFTrF   FN)rI   rJ   rK   rJ   rL   r   rM   rN   rO   rN   rP   rN   rQ   rR   rS   rR   rT   rR   rU   r   rX   rY   rZ   r[   r\   r   )rL   r   r\   r   r   r   r   r   r\   r   r   r   r\   r   )r   r3   r   r   r   r   r\   r?   r   )r\   r[   )rI   r?   rK   r?   r\   r  )r   r?   r\   r?   )r   r?   r\   r   )r   r?   r   r   r   r   r\   r   )r\   r]  )r\   rc  )rI   )
r   r3   rn  r3   ro  r   rL   r   r\   r3   )r   r3   r   r   r   r   r\   rc  )r\   rz  )r\   r   )rZ   r   r\   r   )__name__
__module____qualname____doc___merge_type__annotations__r   r
   r   r   r   r   rd   r   r  r   r  r  r  rb  r  ri  rj  r   r   r   r   rn   rj   rh   rc   rc     sE         
 K"""" /...////JJJ$$$$#### EL/34859 !) %#L2 L2 L2 L2 L2\    U<       3 3 3 U3j
 
 
 
2    ^ U       U D    U* )= )= )= U)=V tB tB tB UtBl
 
 
 
 A7 A7 A7 UA7F  )# )# )# )# U)#V '7 '7 '7 U'7R @H @H @H U@HD @@ @@ @@ U@@DP! P! P!d L L L UL L Lrj   rc   r  r   r  r]  c                    t                     t                    k    s
J d            t           d                   }t          d                   }|dk    r)|dv rt                      S s|dv rt          |d          S n.|dk    r(|dv rt                      S s|dv rt          |d          S t                     d	k    ri fd
t          t                               D             }t	          |ddi}d |D             \  }}	}
t          ||	t          |
                    \  }}n d         }d         }t          |d          }t          |d          }|j        r3|j        r,|j	        s|j	        r|
                    ||d          \  }}}nt          |j        |j        |          \  }}|t          |t          |                    rd}|t          |t          |                    rd}||fS )a  

    Parameters
    ----------
    left_keys : list[ndarray, ExtensionArray, Index, Series]
    right_keys : list[ndarray, ExtensionArray, Index, Series]
    sort : bool, default False
    how : {'inner', 'outer', 'left', 'right'}, default 'inner'

    Returns
    -------
    np.ndarray[np.intp] or None
        Indexer into the left_keys.
    np.ndarray[np.intp] or None
        Indexer into the right_keys.
    z0left_keys and right_keys must be the same lengthr   )rI   rE   )rK   r   T)rK   rE   )rI   r   Frm   c              3  T   K   | ]"}t          |         |                    V  #dS ru   N_factorize_keys)rz   rm  r  r  rT   s     rh   r|   z$get_join_indexers.<locals>.<genexpr>'  sN       
 
 IaL*Q-dCCC
 
 
 
 
 
rj   r$  c              3  4   K   | ]}t          |          V  d S rw   r   r2  s     rh   r|   z$get_join_indexers.<locals>.<genexpr>,  (      55T!WW555555rj   rV   re  N)r   _get_empty_indexer _get_no_sort_one_missing_indexerr   r'  _get_join_keysr   r3   is_monotonic_increasingr  r   get_join_indexers_non_uniquerF  r   )r  r  rT   rL   left_nright_nmappedzippedllabrlabshapelkeyrkeyrI   rK   r   lidxridxs   ```               rh   ra  ra    sw   , y>>S__,,,: -,,
 1F*Q-  G{{###%''' 	C#!3333GTBBB	A$$$%''' 	C#!2223FEBBB 9~~
 
 
 
 
 
3y>>**
 
 
 f*T**55f555dE $D$edCC
dd|!}E"""D$U###E 	$	
)	
 ^	
  %	

 		%S$T	RR41L%-s
 

d ,T3t99==,T3u::>>:rj   r   1tuple[npt.NDArray[np.intp], npt.NDArray[np.intp]]c                V   t          | |||          \  }}}|dk    r||fS |dk    rt          j        ||||          \  }}nc|dk    rt          j        ||||          \  }}nA|dk    rt          j        ||||          \  }}n|dk    rt          j        |||          \  }}||fS )aW  
    Get join indexers for left and right.

    Parameters
    ----------
    left : ArrayLike
    right : ArrayLike
    sort : bool, default False
    how : {'inner', 'outer', 'left', 'right'}, default 'inner'

    Returns
    -------
    np.ndarray[np.intp]
        Indexer into left.
    np.ndarray[np.intp]
        Indexer into right.
    r`  r4  rI   ru   rK   rE   r   )r  libjoinleft_outer_join
inner_joinfull_outer_join)	rI   rK   rT   rL   r  r  countr  r  s	            rh   r  r  I  s    . (e$CHHHD${{Tz
f}},T4TJJJ
dd	,T4TJJJ
dd	'dEEEE
dd	,T4??
d:rj   r4   r   r3   lindexernpt.NDArray[np.intp]rindexer)tuple[FrozenList, FrozenList, FrozenList]c                F   d	d} ||          }|j         }|j        }|j        }	|D ]v}
|
| j        v r| }|}n|}|}|j                            |
          }|j         |         }|j        |         }||}nt	          j        ||d          }||gz   }||gz   }|	|
gz   }	w|||	fS )
a  
    *this is an internal non-public method*

    Returns the levels, labels and names of a multi-index to multi-index join.
    Depending on the type of join, this method restores the appropriate
    dropped levels of the joined multi-index.
    The method relies on lindexer, rindexer which hold the index positions of
    left and right, where a join was feasible

    Parameters
    ----------
    left : MultiIndex
        left index
    right : MultiIndex
        right index
    dropped_level_names : str array
        list of non-common level names
    join_index : Index
        the index of the join between the
        common levels of left and right
    lindexer : np.ndarray[np.intp]
        left indexer
    rindexer : np.ndarray[np.intp]
        right indexer

    Returns
    -------
    levels : list of Index
        levels of combined multiindexes
    labels : np.ndarray[np.intp]
        labels of combined multiindexes
    names : List[Hashable]
        names of combined multiindex levels

    r   r3   r\   r4   c                t    t          | t                    r| S t          j        | j        g| j        g          S )N)r)  )r   r4   r  rF  r,  r  s    rh   _convert_to_multiindexz@restore_dropped_levels_multijoin.<locals>._convert_to_multiindex  s8    eZ(( 	OL)5=/%*NNNNrj   Nr4  r7  )r   r3   r\   r4   )r  r  r)  r   rG  rH  )rI   rK   dropped_level_namesr   r  r  r   join_levels
join_codesr   dropped_level_nameidxro  name_idxrestore_levelsr  restore_codess                    rh    restore_dropped_levels_multijoinr	  o  s   XO O O O ('
33J#K!J!J 2 7 7++CGGCG 9??#566H- 	(#?!MM!M%RHHHM "^$44=/1
#5"66


J..rj   c                  4    e Zd ZdZ	 	 	 	 	 	 	 	 dddZddZdS )r   ordered_mergeNFrF   r   rI   rJ   rK   rM   r   rO   rP   rQ   rR   rS   rU   r   r   r[   rL   r   r\   r   c                ^    |	| _         t                              | ||||||||
|d           d S )NT)rM   rO   rQ   rS   rP   rL   rU   rT   )r   rc   r   )r   rI   rK   rM   rO   rP   rQ   rS   rU   r   rL   s              rh   r   z_OrderedMerge.__init__  sT     '  !# 	! 	
 	
 	
 	
 	
rj   r?   c                D   |                                  \  }}}| j        dk    r3|d }nt          j        |          }|d }n0t          j        |          }n| j        |}|}nt	          d          |                     |||          }|                     |||           |S )Nffillz#fill_method must be 'ffill' or None)r  r   r  ffill_indexerr   r   r  )r   r   r   r   left_join_indexerright_join_indexerr   s          rh   rd   z_OrderedMerge.get_result  s    262E2E2G2G/
L-
 w&&#$(!!$+$9,$G$G!$%)""%,%:=%I%I""% ,!.BCCC)))+=
 
 	!!&,FFFrj   )NNNFFrF   Nr   )rI   rJ   rK   rJ   rM   r   rO   r   rP   r   rQ   rR   rS   rR   rU   r   r   r[   rL   r   r\   r   r   )r  r  r  r  r   rd   rn   rj   rh   r   r     s`        !K !%%)&* !)"&)0
 
 
 
 
8     rj   r   c                :    d|  d}t          t          |d           S )N
asof_join_
_on_X_by_Y)getattrr  )r   r,  s     rh   _asof_by_functionr  	  s$    -	---D7D$'''rj   c                  f     e Zd ZdZ	 	 	 	 	 	 	 	 	 	 	 	 	 d)d*dZ fdZd+d Zd,d!Zd-d&Zd.d(Z	 xZ
S )/r   
asof_mergeNFrF   r   Tr   rI   rJ   rK   rM   r   rO   rP   rQ   rR   rS   rU   r   rL   Literal['asof']r   r   r   r\   r   c                >   || _         |	| _        |
| _        || _        || _        || _        | j        dvrt          d| j                   t          | j                  sd| j         }t          |          t          	                    | |||||||||d            d S )N)r   forwardnearestzdirection invalid: z,allow_exact_matches must be boolean, passed )rM   rO   rP   rQ   rS   rL   rU   r   )
r   r   r   r   r   r   r   r    r   r   )r   rI   rK   rM   rO   rP   rQ   rS   r   r   r   rU   rL   r   r   r   r   s                    rh   r   z_AsOfMerge.__init__	  s    $  "#6 " >!CCCC4>CCDDD t/00 	"525 5  S//!!# 	 	
 	
 	
 	
 	
rj   c                   t                                          ||          \  }}t          |          dk    r| j        st	          d          t          |          dk    r| j        st	          d          | j        r.t          | j        j        t                    rt	          d          | j        r.t          | j
        j        t                    rt	          d          | j        0| j        | j        t	          d          | j        x| _        | _        | j        | j        t	          d          | j        | j        t	          d          | j        sr|d	         }t          |t                    r|j        }n^|| j        j        v r| j                            |          j        n| j        j                            |          }n| j        j        j        }| j        sr|d	         }t          |t                    r|j        }n^|| j
        j        v r| j
                            |          j        n| j
        j                            |          }n| j
        j        j        }t'          |          s-t'          |          st)          |          st)          |          rt	          d
|d|d          | j        t+          | j                  s| j        g| _        t+          | j                  s| j        g| _        t          | j                  t          | j                  k    rt	          d          | j        t-          |          z   }| j        t-          |          z   }||fS )Nrm   zcan only asof on a key for leftz can only asof on a key for rightzleft can only have one indexzright can only have one indexz(Can only pass by OR left_by and right_byzmissing left_byzmissing right_byr   zIncompatible merge dtype, r  z$, both sides must have numeric dtypez,left_by and right_by must be the same length)superr   r   rQ   r   rS   r   rI   r   r4   rK   r   r   r   r1  r:  ry   r  r>  r(   r)   r%   r   )r   rO   rP   	left_on_0lo_dtype
right_on_0ro_dtype	__class__s          rh   r   z"_AsOfMerge._validate_left_right_on?	  sx   !GG;;GXNN w<<1T_>???x==Ad&6?@@@? 	=z$)/:FF 	=;<<< 	>
4:+;Z H H 	><=== 7|'4=+D !KLLL+/72DL4=<DM$=.///<#(=/000  	-
I)V,, $? !DI$555 I88CCII99)DD  y,H 	.!!J*f-- %+ "TZ%777 J99*EEKK):::FF  z'-H H%%		x((		 x((		 x((			 DX D DD D D   <#-- . $~.. 0!%4<  C$6$666 !OPPPlT']]2G}tH~~5H  rj   r   r   r   c                   dd}t          t          ||d	
                    D ]\  }\  }} ||||           | j        r| j        j        j        }n|d         }| j        r| j        j        j        }n|d         } |||d           d S )NrI   r   rK   r  intr\   r   c                   | j         |j         k    rpt          | j         t                    r1t          |j         t                    rd| d| j         d|j         d}nd| d| j         d|j         d}t          |          d S )Nzincompatible merge keys [z] r  z), both sides category, but not equal onesz, must be the same type)r:  r   r+   r   )rI   rK   r  r   s       rh   _check_dtype_matchzE_AsOfMerge._maybe_require_matching_dtypes.<locals>._check_dtype_match	  s    zU[((dj*:;; 
K!1A A TA T T T T ;T T T CBA B B B B ;B B B  !oo%+ )(rj   Tr#  r4  r   )rI   r   rK   r   r  r%  r\   r   )rC  r'  rQ   rI   r   rF  rS   rK   )	r   r   r   r'  r  r  r  ltrts	            rh   r   z)_AsOfMerge._maybe_require_matching_dtypes	  s    
	& 	& 	& 	&2 %SQU%V%V%VWW 	* 	*KAxBr2q))))? 	$(BB#B 	%!)BB $B2r1%%%%%rj   c                   | j         v| j        r| j        j        j        }n|d         }d| j          d|j        }t          |j                  s#t          |t                    re|j        j	        dv rWt          | j         t          j                  st          |          | j         t          d          k     rt          d          d S t          |j                  r?t          | j                   st          |          | j         dk     rt          d          d S t!          |j                  r?t#          | j                   st          |          | j         dk     rt          d          d S t          d          d S )Nr4  zincompatible tolerance z, must be compat with type mMr   ztolerance must be positivez'key must be integer, timestamp or float)r   rQ   rI   r   rF  r:  r*   r   r6   rJ  datetime	timedeltar   r   r$   r#   r"   r&   )r   r   r(  r   s       rh   r   z_AsOfMerge._validate_tolerance	  s   >% (Y_,#B'*$. * *X* * 
 #28,, L2233L8:8M8M!$.(2DEE *$S//)>IaLL00$%ABBB 10 ""(++ L!$.11 *$S//)>A%%$%ABBB &%  )) L 00 *$S//)>A%%$%ABBB &% !!JKKKC &%rj   valuesr   side
np.ndarrayc                   t          |d          j        sFt          |                                          rt	          d| d          t	          | d          t          |t                    r|                                }t          |j	                  r|
                    d          }nFt          |t                    r|j        }n)t          |t                    r|                                }|S )NFr  z"Merge keys contain null values on z sidez keys must be sortedi8)r3   r  r/   rE  r   r   r6   _maybe_convert_datelike_arrayr*   r:  viewr7   _datar8   to_numpy)r   r.  r/  s      rh   _convert_values_for_libjoinz&_AsOfMerge._convert_values_for_libjoin	  s     V%(((@ 	<F||!! S !Qd!Q!Q!QRRR:::;;;f122 	<99;;Fv|,, 	'[[&&FF00 	'\FF// 	'__&&F
 rj   r  c           	     Z   | j         r| j        j        j        n| j        d         }| j        r| j        j        j        n| j        d         }|j        |j        k    sJ | j	        }|t          |j                  s#t          |t                    r|j        j        dv rtt          |          }|j        j        dv rPt          |t                    r|j        j        j        }nt#          |          j        }|                    |          }|j        }|                     |d          }|                     |d          }| j        <| j         r| j        r| j        | j        n| j        dd         | j        dd         fdt-          t/                              D             }t/                    dk    r|d         d         }|d         d         }nad	 |D             }t1          d
 |D                       }	t3          ||	dd          }
t/          d                   }|
d|         }|
|d         }t5          |          }t5          |          }t7          | j                  } |||||| j        |          S t7          | j                  } |||dd| j        |d          S )r_  r4  Nr+  rI   rK   r   c                L    g | ] }t          |         |         d           !S )Fru   r  )rz   rm  r   r   s     rh   r   z1_AsOfMerge._get_join_indexers.<locals>.<listcomp>-
  sM          "1%#A&    rj   rm   c                F    g | ]}t          j        |d d                   S )Nr  )rs  concatenaterz   r  s     rh   r   z1_AsOfMerge._get_join_indexers.<locals>.<listcomp>:
  s*    >>>!q!u-->>>rj   c              3  &   K   | ]}|d          V  dS )r  Nrn   r<  s     rh   r|   z0_AsOfMerge._get_join_indexers.<locals>.<genexpr>;
  s&      33qad333333rj   F)r  rT   xnull)rQ   rI   r   rF  r   rS   rK   r   r:  r   r*   r   r6   rJ  r   pyarrow_dtypeunitr:   as_unit_valuer7  r   r   r   r   r=   r   r  r   r   )r   left_valuesright_valuesr   r@  r  left_by_valuesright_by_valuesarrsr  group_indexleft_lenfuncr   r   s                @@rh   rb  z_AsOfMerge._get_join_indexers	  s   
 (,SDIO##D<OPR<S 	 )-(8VDJ$$d>RSU>V 	
  L$66666N	  #;#455 -;(;<<-%*d22%i00	 $)T11 "+/BCC P*0>C=kJJO ) 1 1$ 7 7I%,	 66{FKK77gNN <# =4#3 =!%!4"&"6!%!4QrT!:"&"6qt"<     s>2233  F >""a''!'1"()A,>>v>>>33F33333-E   ~a011!,YhY!7"-hii"8).99N*?;;O %T^44D4(   %T^44D4(  rj   )NNNFFNNNrF   r   NTr   )rI   rJ   rK   rJ   rM   r   rO   r   rP   r   rQ   rR   rS   rR   rU   r   rL   r  r   rR   r   r   r\   r   r  r  )r.  r   r/  r   r\   r0  r\   r  )r  r  r  r  r   r   r   r   r7  rb  __classcell__)r#  s   @rh   r   r   		  s        K !%%)&* !)%$(#!1
 1
 1
 1
 1
fN! N! N! N! N!`+& +& +& +&Z#L #L #L #LJ   4` ` ` ` ` ` ` `rj   r   	join_keysr   c                     fdt          j                  D             }t          |ddi}d |D             \  }}}r.t          t	          t
          j        |j                            }n%d }t          t	          |j                            }t                     D ]\  }	}
j        |	         dk    }|	                                r\|
||	         ||	         dz
  k             }|j
        dk    s|d         |d         k    s||	xx         dz  cc<   ||	         dz
  ||	         |<   t          ||t          |                    \  }}||fS )	Nc              3  h   K   | ],}t          j        |         j        |                    V  -dS r  )r  r  rF  )rz   rm  r   rM  rT   s     rh   r|   z*_get_multiindex_indexer.<locals>.<genexpr>b
  sS         	Q/1DIII     rj   r$  Tc              3  4   K   | ]}t          |          V  d S rw   r  r2  s     rh   r|   z*_get_multiindex_indexer.<locals>.<genexpr>g
  r  rj   c                0    |                      dd          S )Nr2  F)subok)r  )as    rh   r   z)_get_multiindex_indexer.<locals>.<lambda>k
  s    188D866 rj   r4  rm   r   )r   r   r'  r   maprs  r   r  rC  rE  sizer  r   )rM  r   rT   r  r  rcodeslcodesr  i8copyr  join_keyru  rS  r  r  s   ```            rh   _get_multiindex_indexerrZ  ^
  s        u}%%  F &&&&F55f555FFE 0c"'65;778866c&%+..// !++ 	+ 	+8{1~#88:: 	+ eAhl23Av{{!A$!A$,,aA#AhlF1IdO  edCCJD$:rj   c                     t          j        g t           j                  t          j        g t           j                  fS )zReturn empty join indexers.rr  )rs  arrayintprn   rj   rh   r  r  
  s6     	27###
27### rj   rm  r%  left_missingc                    t          j        | t           j                  }t          j        | dt           j                  }|r||fS ||fS )a  
    Return join indexers where all of one side is selected without sorting
    and none of the other side is selected.

    Parameters
    ----------
    n : int
        Length of indexers to create.
    left_missing : bool
        If True, the left indexer will contain only -1's.
        If False, the right indexer will contain only -1's.

    Returns
    -------
    np.ndarray[np.intp]
        Left indexer
    np.ndarray[np.intp]
        Right indexer
    rr  r4  )r  r8  r:  )rs  rx  r]  full)rm  r^  r  idx_missings       rh   r  r  
  sP    , )ARW
%
%
%C'b@@@K  Crj   rk  rl  ?tuple[Index, npt.NDArray[np.intp] | None, npt.NDArray[np.intp]]c                Z   t          |t                    rt          |||          \  }}n|d         }|j        }t	          |||          \  }}}t          j        ||||          \  }	}
|s t          |           t          |	          k    r|                     |	          }||	|
fS | d |
fS )Nru   r   )	r   r4   rZ  rF  r  r  r  r   r   )rk  rl  rM  rT   r  r  r-  r.  r  r   r   r   s               rh   rh  rh  
  s     (J'' 	 ,YtLLL
dd
 | !0t$!G!G!GHi")"9)U# # #L-  7s7||s<0000\\,//
<66 D-''rj   r  r  6tuple[npt.NDArray[np.intp], npt.NDArray[np.intp], int]c                   t          | j        t                    rt          |j        t                    s4t          j        | j        d          rlt          j        |j        d          rRt          d|                               |          \  } }t          d|           j        } t          d|          j        }nt          | j        t                    rt          |j        t                    r}| j        |j        k    rmt          | t                    sJ t          |t                    sJ | 
                    |          }t          | j                  } t          |j                  }nt          | t                    r| j        |j        k    rt          | j        t                    s,t          | j        t                    r| j        j        dk    rddl}ddlm} t)          |           }| j        } |j        }|                    | j        |j        z                                                                             }|                    |j        t9          |                   d                                                              t>          j         d          |                    |j        t9          |d                   d                                                              t>          j         d          t)          |j!                  }
}	}|r/|j!                            d	          }tE          |||	          \  }}	|j#        dk    ri|dk    }|$                                }|	dk    }|$                                }|rt?          j%        |||
           |rt?          j%        |	||
           |
d
z  }
||	|
fS t          | tL                    swt          | j        t                    r/tO          | j        j(                  sDtS          | j                  r|r.| *                                \  } }|*                                \  }}tW          | j                  rP| j        |j        k    r@t?          j,        | t>          j-                  } t?          j,        |t>          j-                  }t]          | |          \  }} } |t_          t)          |           t)          |                    t          |tL          t`          f                    }t          | tL                    r4t          |tL                    sJ | j1        | j2        }}|j1        |j2        }}nt          | t`                    rt          |t`                    sJ |                     d
| j        j(                  }|                    d
| j        j(                  }| 3                                |3                                }}n	| |}}d\  }}|dk    o| o| j        j4        dv }|rr|5                    ||          }	|6                                t)          |	          k    r|7                    ||          \  }}||dfS |5                    ||          }n.|5                    ||          }|5                    ||          }	|j        t?          j        t>          j                   k    sJ |j                    |	j        t?          j        t>          j                   k    sJ |	j                    |6                                }
|r-|j8        9                                }tE          |||	          \  }}	|dk    }|$                                }|	dk    }|$                                }|s|r5|rt?          j%        |||
           |rt?          j%        |	||
           |
d
z  }
||	|
fS )a  
    Encode left and right keys as enumerated types.

    This is used to get the join indexers to be used when merging DataFrames.

    Parameters
    ----------
    lk : ndarray, ExtensionArray
        Left key.
    rk : ndarray, ExtensionArray
        Right key.
    sort : bool, defaults to True
        If True, the encoding is done such that the unique elements in the
        keys are sorted.
    how: str, optional
        Used to determine if we can use hash-join. If not given, then just factorize
        keys.

    Returns
    -------
    np.ndarray[np.intp]
        Left (resp. right if called with `key='right'`) labels, as enumerated type.
    np.ndarray[np.intp]
        Right (resp. left if called with `key='right'`) labels, as enumerated type.
    int
        Number of unique elements in union of left and right labels. -1 if we used
        a hash-join.

    See Also
    --------
    merge : Merge DataFrame or named Series objects
        with a database-style join.
    algorithms.factorize : Encode the object as an enumerated type
        or categorical variable.

    Examples
    --------
    >>> lk = np.array(["a", "c", "b"])
    >>> rk = np.array(["a", "c"])

    Here, the unique values are `'a', 'b', 'c'`. With the default
    `sort=True`, the encoding will be `{0: 'a', 1: 'b', 2: 'c'}`:

    >>> pd.core.reshape.merge._factorize_keys(lk, rk)
    (array([0, 2, 1]), array([0, 2]), 3)

    With the `sort=False`, the encoding will correspond to the order
    in which the unique elements first appear: `{0: 'a', 1: 'c', 2: 'b'}`:

    >>> pd.core.reshape.merge._factorize_keys(lk, rk, sort=False)
    (array([0, 1, 2]), array([0, 1]), 3)
    r;  rA   pyarrowr   Nr4  Fr  )zero_copy_onlyrm   rr  )	uses_mask)na_valuer:  r3  rE   iufb)ru  ):r   r:  r,   r   is_np_dtyper	   _ensure_matching_resos_ndarrayr+   r2   _encode_with_my_categoriesr   r  r8   r1   r9   storagerf  pyarrow.computecomputer   	_pa_arraychunked_arraychunkscombine_chunksdictionary_encode	fill_nullr   slicer6  r  rs  r]  
dictionary_sort_labels
null_countrE  putmaskr7   r'   numpy_dtyper)   _values_for_factorizer*   asarrayint64#_convert_arrays_and_get_rizer_klassmaxr6   r5  _maskr/   rJ  	factorize	get_counthash_inner_joinuniquesto_array)r  r  rT   rL   papclen_lkdcr  r  r  r  lmasklanyrmaskranyr   klassrizerlk_datalk_maskrk_datark_maskhash_join_availabler  r  s                             rh   r  r  
  s6   z 	28_--M/2<RX2W2WM/
/"(C
(
(M/-0_RXs-K-KM/
 or**AA"EEB/2&&//2&&/ 	28-..D/rx!122D/ H  "k*****"k***** **2.."(##"(##	B	'	' 6/BH,@,@bh
++ '	%rx--'	%24(2Bi2O2O    ((((((WWFBB  RY!677!!""$$  RZf6;;e,,RZfd(;(;<bAAe,,BM"" $D  ?-000FF)'4>>
d}q  
yy{{
yy{{ 3JtUE222 3JtUE222
u$$"o.. 	/rx,,	/ !!566		/
 $BH--	/
 7;	/ ,,..EB ,,..EB28$$ ,RX)=)= Z"(+++Z"(+++7B??ME2rECGGSWWR/3F!GHH  E
 "o&& &"o.....8RX8RX	B+	,	, &"122222 ++q0D+EE++q0D+EE7799bggii
 r%.QXQ"(-6:Q 	6wW55??D		))..w@@JD$r>!??7?99DDwW55wW55:"'*****DJ***:"'*****DJ***OOE 7-((**!'466
d BJE99;;DBJE99;;D t  	+JtUE*** 	+JtUE***
urj   :tuple[type[libhashtable.Factorizer], ArrayLike, ArrayLike]c                   t          | j                  r| j        |j        k    rt          | j        |j        g          }t          |t                    r|                                }t          | t                    s|                    | |d          } n|                     |d          } t          |t                    s|                    ||d          }nF|                    |d          }n.|                     |d          } |                    |d          }t          | t                    rt          | j        j                 }nyt          | j        t                    rt          | j        j        j                 }nBt          | j        j                 }n*t          j        }t!          |           } t!          |          }|| |fS )NFr9  r  )r'   r:  r   r   r   r  r8   r  r  r7   _factorizersr   r1   r}  libhashtableObjectFactorizerr   )r  r  r:  clsr  s        rh   r  r    s    !! 8rx$bh%9::E%00 20022!"n55 6++Be%+HHBB5u55B!"n55 6++Be%+HHBB5u55BBYYu5Y11YYu5Y11b/** 	0 /EE*-- 	0 !5!:;EE /EE -22"b=rj   r  r0  c                    t          |          }t          j        ||g          }t          j        | |d          \  }}|d |         ||d          }}||fS )NT)use_na_sentinel)r   rs  r;  rG  	safe_sort)	r  rI   rK   llengthr  r   
new_labelsnew_left	new_rights	            rh   rz  rz    sb     $iiG^T5M**FOGVTJJJMAz$XgX.
7880DiHYrj   r  %list[npt.NDArray[np.int64 | np.intp]]r  r  r   3tuple[npt.NDArray[np.int64], npt.NDArray[np.int64]]c                   t          fdt          t                    dd          D                       }t          j        d|         d          }|| d                             ddd          z  }||d                             ddd          z  }t          d|          D ]V}t          j        d	
          5  ||         z  }d d d            n# 1 swxY w Y   || |         |z  z  }|||         |z  z  }W|t                    k    r||fS t          |||          \  }}}	|g| |d          } |g||d          }|	g|d          R t          | ||          S )Nc              3  J   K   | ]}t          d |                   |V  d S rw   )r>   )rz   r  r  s     rh   r|   z!_get_join_keys.<locals>.<genexpr>  sM        )%+66     rj   r   r4  rm   r2  rr  F)rR  rV   r  )divideru   )	nextr   r   rs  prodr  r  r  r  )
r  r  r  rT   nlevstrider  r  r  r  s
     `       rh   r  r    s        UQ++    D WU1T6]$///FDGNN4u5NAAADDGNN4u5NAAAD1d^^ ! ![))) 	  	 uQxF	  	  	  	  	  	  	  	  	  	  	  	  	  	  	 Q&  Q&  s5zzTz (d>>>D$4;D4;D"U455\""E$eT222s    CC	C	c                f    t          | t                    rt          |t                    sdS | |k    S )NT)r   r   )rQ  rR  s     rh   rD  rD    s4    eS!! E3)?)? tE>rj   c                &    | d uot          j        |  S rw   )r   any_not_none)r   s    rh   r  r    s    D=1S-q11rj   objc                    t          | t                    r| S t          | t                    r*| j        t	          d          |                                 S t          dt          |            d          )Nz$Cannot merge a Series without a namez.Can only merge Series or DataFrame objects, a z was passed)r   r-   r.   r,  r   r  	TypeErrorr   )r  s    rh   r`   r`     sv    #|$$ 	

	C	#	# 
8CDDD||~~ST#YYSSS
 
 	
rj   tuple[Index, Index]c                   t          |d          rt          |t                    r t          dt	          |           d          |                     |          t                    dk    r| |fS |\  }}|s|st          d           dfd	}t          ||
          }t          ||
          }| 	                    |          }|	                    |          }	g }
|j
        sU|
                    ||                                |                                  z                                                      |	j
        sU|
                    |	|	                                |                                 z                                                      |
                    |                    |                                                                                   |
                    |	                    |                                                                                    |
r t          dt!          |
           d          ||	fS )z
    Suffixes type validation.

    If two indices overlap, add suffixes to overlapping entries.

    If corresponding suffix is empty, the entry is simply converted to string.

    F)
allow_setszPassing 'suffixes' as a z:, is not supported. Provide 'suffixes' as a tuple instead.r   z)columns overlap but no suffix specified: suffixr[   c                     | v r||  | S | S )a?  
        Rename the left and right indices.

        If there is overlap, and suffix is not None, add
        suffix, otherwise, leave it as-is.

        Parameters
        ----------
        x : original column name
        suffix : str or None

        Returns
        -------
        x : renamed column name
        rn   )r   r  	to_renames     rh   renamerz+_items_overlap_with_suffix.<locals>.renamer  s&      	>>f0>>>!rj   )r  z1Passing 'suffixes' which cause duplicate columns z is not allowed.)r  r[   )r%   r   dictr  r   r  r   r   r   _transform_indexr  extendr  r   r   r   r~   )rI   rK   rU   lsuffixrsuffixr  lrenamerrrenamerr   r   dupsr  s              @rh   r   r     s}    U333 
z(D7Q7Q 
5tH~~ 5 5 5
 
 	

 !!%((I
9~~U{GW R7 RPYPPQQQ     ( ww///Hww///H##H--G$$X..GD U 	GW//11t7H7H6HIJQQSSTTT VGW//11u7G7G7I7I6IJKRRTTUUUKK$$U%5%5i%@%@AAHHJJKKKKK$$T__Y%?%?@@GGIIJJJ 
D		   
 
 	

 Grj   )rI   rJ   rK   rJ   rL   r   rM   rN   rO   rN   rP   rN   rQ   rR   rS   rR   rT   rR   rU   r   rV   rW   rX   rY   rZ   r[   r\   r?   )	NNNFFFrF   FN)rI   r?   rK   r?   rM   rN   rO   rN   rP   rN   rQ   rR   rS   rR   rT   rR   rU   r   rX   rY   rZ   r[   r\   r?   )rI   rJ   rK   rJ   )NNNNNNrF   r   )rI   rJ   rK   rJ   rM   r   rO   r   rP   r   r   r[   rU   r   rL   r   r\   r?   )NNNFFNNNrF   NTr   )rI   rJ   rK   rJ   rM   r   rO   r   rP   r   rQ   rR   rS   rR   rU   r   r   r   r   rR   r   r   r\   r?   )FrE   )
r  r   r  r   rT   rR   rL   r   r\   r]  )
rI   r   rK   r   rT   rR   rL   r   r\   r  )rI   r4   rK   r4   r   r3   r  r  r  r  r\   r  )r   r   )rM  r   r   r4   rT   rR   r\   r  rK  )rm  r%  r^  rR   r\   r  )F)
rk  r3   rl  r3   rM  r   rT   rR   r\   rb  )TN)
r  r   r  r   rT   rR   rL   r[   r\   rd  )r  r   r  r   r\   r  )r  r0  rI   r  rK   r  r\   r  )
r  r  r  r  r  r   rT   rR   r\   r  )r\   rR   )r  rJ   r\   r?   )rI   r3   rK   r3   rU   r   r\   r  )r  
__future__r   collections.abcr   r   r,  	functoolsr   r  typingr   r   r	   r
   ro   r  numpyrs  pandas._libsr   r   r  r   r  r   pandas._libs.libr   pandas._typingr   r   r   r   r   r   r   r   pandas.errorsr   pandas.util._decoratorsr   r   pandas.util._exceptionsr   pandas.core.dtypes.baser   pandas.core.dtypes.castr   pandas.core.dtypes.commonr   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   pandas.core.dtypes.dtypesr+   r,   pandas.core.dtypes.genericr-   r.   pandas.core.dtypes.missingr/   r0   rD   r1   r2   r3   r4   r5   pandas.core.algorithmscore
algorithmsrG  pandas.core.arraysr6   r7   r8   pandas.core.arrays.string_r9   pandas.core.commoncommonr   pandas.core.constructionr:   r;   pandas.core.indexes.apir<   pandas.core.sortingr=   r>   r?   pandas.corer@   rA   pandas.core.indexes.frozenrB   r  Int64Factorizerlonglongint32Int32Factorizerint16Int16Factorizerr  Int8Factorizeruint64UInt64Factorizeruint32UInt32Factorizeruint16UInt16Factorizeruint8UInt8Factorizerbool_float64Float64Factorizerfloat32Float32Factorizer	complex64Complex64Factorizer
complex128Complex128Factorizerobject_r  r  intcr:  itemsizeuintcndarrayr1  r  ri   rb   r   r   r   rc   ra  r  r	  r   r  r   rZ  r  r  rh  r  r  rz  r  rD  r  r`   r   rn   rj   rh   <module>r     sB	    # " " " " "                                            . - - - - -	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 % $ $ $ $ $        5 4 4 4 4 4 2 2 2 2 2 2 4 4 4 4 4 4                                                  
              ' & & & & & & & &         
 3 2 2 2 2 2                          2 1 1 1 1 1       
  6      ######000000555555 Hl*K-Hl*Hl*G\(I|,I|,I|,Hl*Hl*J.J.L,2M<4J-& 7"(rx!Q&& , <RW , <RW829rx"a''!-!>RX!-!>RX *neY	7 H +/0415%!$!} } } } }F ,00415%!2 2 2 2 2j6 6 6 6r H !!%"&"%L L L L L^ H !!%"&%15 $U U U U UrN N N N N N N Nh" 	K K K K Kb 	# # # # #LV/ V/ V/ V/r9 9 9 9 9O 9 9 9x( ( ( (
R R R R R R R Rj
   B      < OT( ( ( ( (> 	M M M M M`       F	 	 	 	"3 "3 "3 "3J   2 2 2 2

 

 

 

D D D D D Drj   