B
    dy                 @   s   d Z ddlmZ ddlZddlmZmZ ddlm	Z	 G dd de
ZG d	d
 d
ZG dd dZdddZdd ZG dd dZG dd deZG dd dZG dd deZG dd deZdS )a  
The Index class can use several implementations as its
engine. Any implementation should implement the following:

__init__(data, row_index) : initialize index based on key/row list pairs
add(key, row) -> None : add (key, row) to existing data
remove(key, data=None) -> boolean : remove data from self[key], or all of
                                    self[key] if data is None
shift_left(row) -> None : decrement row numbers after row
shift_right(row) -> None : increase row numbers >= row
find(key) -> list : list of rows corresponding to key
range(lower, upper, bounds) -> list : rows in self[k] where k is between
                               lower and upper (<= or < based on bounds)
sort() -> None : make row order align with key order
sorted_data() -> list of rows in sorted order (by key)
replace_rows(row_map) -> None : replace row numbers based on slice
items() -> list of tuples of the form (key, data)

Notes
-----
    When a Table is initialized from another Table, indices are
    (deep) copied and their columns are set to the columns of the new Table.

    Column creation:
    Column(c) -> deep copy of indices
    c[[1, 2]] -> deep copy and reordering of indices
    c[1:2] -> reference
    array.view(Column) -> no indices
    )deepcopyN   )MinValueMaxValue)SortedArrayc               @   s   e Zd ZdZdS )
QueryErrorzH
    Indicates that a given index cannot handle the supplied query.
    N)__name__
__module____qualname____doc__ r   r   `/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/astropy/table/index.pyr   (   s   r   c                   s   e Zd ZdZd.ddZdd Zdd	 Zd
d Zdd Zdd Z	dd Z
dd Zd/ddZdd Zdd Zd0ddZd1ddZd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Z fd,d-Z  ZS )2Indexa  
    The Index class makes it possible to maintain indices
    on columns of a Table, so that column values can be queried
    quickly and efficiently. Column values are stored in lexicographic
    sorted order, which allows for binary searching in O(log n).

    Parameters
    ----------
    columns : list or None
        List of columns on which to create an index. If None,
        create an empty index for purposes of deep copying.
    engine : type, instance, or None
        Indexing engine class to use (from among SortedArray, BST,
        and SCEngine) or actual engine instance.
        If the supplied argument is None (by default), use SortedArray.
    unique : bool (defaults to False)
        Whether the values of the index must be unique
    NFc             C   s  ddl m}m} ddlm} |d k	r,t|}|d k	rVt|tsV|j| _	|| _
|| _d S |p\t| _	|d krxg }g }g }n*t|dkrtdnt|dkr|d }	||	 }||	| g}nt|d }
g }xT|D ]L}	t|	|r||	j |	|	j|	jd|	jd }||j q||	 qW ||t|
g dd	}|d d d
 }y|t| }W n" tk
r   ||  }Y nX ||jd d
  }||jd
  }| j	|||d| _
|| _d S )Nr   )TableColumnr   )Timez/Cannot create index without at least one columnjd)formatscaleF)copy_indices)unique)tabler   r   Zastropy.timer   list
isinstancetype	__class__enginedatacolumnsr   len
ValueErrorZargsortappendr   r   npZarangeZlexsort	TypeErrorcolnames)selfr   r   r   r   r   r   r   Z	row_indexcolnum_rowsZnew_columns	remainderr   Zsort_columnslinesr   r   r   __init__B   sL    

zIndex.__init__c             C   s   t | jd S )z*
        Number of rows in index.
        r   )r    r   )r&   r   r   r   __len__x   s    zIndex.__len__c             C   s   || j | |jj< dS )z
        Replace an indexed column with an updated reference.

        Parameters
        ----------
        prev_col : Column
            Column reference to replace
        new_col : Column
            New column reference
        N)r   col_positioninfoname)r&   prev_colnew_colr   r   r   replace_col~   s    zIndex.replace_colc             C   s   | j | j| jd dS )zC
        Recreate the index based on data in self.columns.
        )r   N)r+   r   r   )r&   r   r   r   reload   s    zIndex.reloadc             C   s:   x&t | jD ]\}}|jj|kr|S qW td| dS )z
        Return the position of col_name in self.columns.

        Parameters
        ----------
        col_name : str
            Name of column to look up
        z!Column does not belong to index: N)	enumerater   r.   r/   r!   )r&   col_nameicr   r   r   r-      s    	zIndex.col_positionc          	   C   s   dgt | j }xFt|D ]:\}}y|| || |jj< W q tk
rR   wY qX qW t | jd }||k rz| j| | j	t
|| dS )a*  
        Insert a new row from the given values.

        Parameters
        ----------
        pos : int
            Position at which to insert row
        vals : list or tuple
            List of values to insert into a new row
        columns : list
            Table column references
        Nr   )r    r   r4   r-   r.   r/   r!   r   Zshift_rightaddtuple)r&   posvalsr   keyr6   r'   r(   r   r   r   
insert_row   s    zIndex.insert_rowc             C   sb   t |ttjfr|fS t |ttjfr*|S t |trPt| jd }t	|
| S td|dS )z
        Return an iterable corresponding to the
        input row specifier.

        Parameters
        ----------
        row_specifier : int, list, ndarray, or slice
        r   z?Expected int, array of ints, or slice but got {} in remove_rowsN)r   intr#   integerr   ndarrayslicer    r   rangeindicesr!   r   )r&   row_specifierZcol_lenr   r   r   get_row_specifier   s    	
zIndex.get_row_specifierc             C   sV   g }x*|  |D ]}| j|dd || qW x tt|D ]}| j| q>W dS )z
        Remove the given rows from the index.

        Parameters
        ----------
        row_specifier : int, list, ndarray, or slice
            Indicates which row(s) to remove
        F)reorderN)rE   
remove_rowr"   reversedsortedr   
shift_left)r&   rD   rowsrowr   r   r   remove_rows   s    	zIndex.remove_rowsTc                sF   | j t fdd| jD  s2td  d|rB| j   dS )z
        Remove the given row from the index.

        Parameters
        ----------
        row : int
            Position of row to remove
        reorder : bool
            Whether to reorder indices after removal
        c                s   g | ]}|  qS r   r   ).0r'   )rL   r   r   
<listcomp>   s    z$Index.remove_row.<locals>.<listcomp>zCould not remove row z from indexN)r   remover9   r   r!   rJ   )r&   rL   rF   r   )rL   r   rG      s    "zIndex.remove_rowc             C   s   | j |S )z
        Return the row values corresponding to key, in sorted order.

        Parameters
        ----------
        key : tuple
            Values to search for in each column
        )r   find)r&   r<   r   r   r   rQ      s    	z
Index.findc             C   s   |  ||dS )z
        Return rows whose keys contain the supplied key as a prefix.

        Parameters
        ----------
        key : tuple
            Prefix for which to search
        )TT)same_prefix_range)r&   r<   r   r   r   same_prefix   s    	zIndex.same_prefixTTc             C   sv   t |}t | j}|d r t nt }|d r4t nt }|t|| |g  }|t|| |g  }| j|||S )a  
        Return rows whose keys have a prefix in the given range.

        Parameters
        ----------
        lower : tuple
            Lower prefix bound
        upper : tuple
            Upper prefix bound
        bounds : tuple (x, y) of bools
            Indicates whether the search should be inclusive or
            exclusive with respect to the endpoints. The first
            argument x corresponds to an inclusive lower bound,
            and the second argument y to an inclusive upper bound.
        r   r   )r    r   r   r   r9   r   rB   )r&   lowerupperboundsnZncolsabr   r   r   rR   	  s    
zIndex.same_prefix_rangec             C   s   | j |||S )a  
        Return rows within the given range.

        Parameters
        ----------
        lower : tuple
            Lower prefix bound
        upper : tuple
            Upper prefix bound
        bounds : tuple (x, y) of bools
            Indicates whether the search should be inclusive or
            exclusive with respect to the endpoints. The first
            argument x corresponds to an inclusive lower bound,
            and the second argument y to an inclusive upper bound.
        )r   rB   )r&   rU   rV   rW   r   r   r   rB   #  s    zIndex.rangec                sF   | j  dd  fdd| jD }||| |< | jt|  dS )a/  
        Replace the value of a column at a given position.

        Parameters
        ----------
        row : int
            Row number to modify
        col_name : str
            Name of the Column to modify
        val : col.info.dtype
            Value to insert at specified row of col
        F)rF   c                s   g | ]}|  qS r   r   )rN   r7   )rL   r   r   rO   C  s    z!Index.replace.<locals>.<listcomp>N)rG   r   r-   r   r8   r9   )r&   rL   r5   valr<   r   )rL   r   replace5  s    zIndex.replacec             C   s&   t dd t|D }| j| dS )a  
        Modify rows in this index to agree with the specified
        slice. For example, given an index
        {'5': 1, '2': 0, '3': 2} on a column ['2', '5', '3'],
        an input col_slice of [2, 0] will result in the relabeling
        {'3': 0, '2': 1} on the sliced column ['3', '2'].

        Parameters
        ----------
        col_slice : list
            Indices to slice
        c             s   s   | ]\}}||fV  qd S )Nr   )rN   r6   rL   r   r   r   	<genexpr>T  s    z%Index.replace_rows.<locals>.<genexpr>N)dictr4   r   replace_rows)r&   	col_sliceZrow_mapr   r   r   r_   G  s    zIndex.replace_rowsc             C   s   | j   dS )z_
        Make row numbers follow the same sort order as the keys
        of the index.
        N)r   sort)r&   r   r   r   ra   W  s    z
Index.sortc             C   s
   | j  S )z|
        Returns a list of rows in sorted order based on keys;
        essentially acts as an argsort() on columns.
        )r   sorted_data)r&   r   r   r   rb   ^  s    zIndex.sorted_datac             C   s
   t | |S )z
        Returns a sliced version of this index.

        Parameters
        ----------
        item : slice
            Input slice

        Returns
        -------
        SlicedIndex
            A sliced reference to this index.
        )SlicedIndex)r&   itemr   r   r   __getitem__e  s    zIndex.__getitem__c             C   s2   t dd | jD }d| jj d| d| j dS )Nc             s   s   | ]}|j jV  qd S )N)r.   r/   )rN   r'   r   r   r   r]   v  s    z!Index.__repr__.<locals>.<genexpr><z	 columns=z data=>)r9   r   r   r   r   )r&   Z	col_namesr   r   r   __repr__u  s    zIndex.__repr__c                sL   t  | j}|jd| jd t| j||_| jdd |_||t| < |S )a  
        Return a deep copy of this index.

        Notes
        -----
        The default deep copy must be overridden to perform
        a shallow copy of the index columns, avoiding infinite recursion.

        Parameters
        ----------
        memo : dict
        N)r   )	super__new__r   r+   r   r   r   r   id)r&   memoindex)r   r   r   __deepcopy__y  s    zIndex.__deepcopy__)NF)T)rT   )rT   )r   r	   r
   r   r+   r,   r2   r3   r-   r=   rE   rM   rG   rQ   rS   rR   rB   r\   r_   ra   rb   re   rh   rn   __classcell__r   r   )r   r   r   /   s*   
6


r   c               @   s   e Zd ZdZd6ddZedd Zedd Zd	d
 Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zed1d2 Zed3d4 Zd5S )7rc   a  
    This class provides a wrapper around an actual Index object
    to make index slicing function correctly. Since numpy expects
    array slices to provide an actual data view, a SlicedIndex should
    retrieve data directly from the original index and then adapt
    it to the sliced coordinate system as appropriate.

    Parameters
    ----------
    index : Index
        The original Index reference
    index_slice : tuple, slice
        The slice to which this SlicedIndex corresponds
    original : bool
        Whether this SlicedIndex represents the original index itself.
        For the most part this is similar to index[:] but certain
        copying operations are avoided, and the slice retains the
        length of the actual index despite modification.
    Fc             C   sj   || _ || _d| _t|tr.|\| _| _| _n8t|tr^t	|j
d }||\| _| _| _ntdd S )NFr   z"index_slice must be tuple or slice)rm   original_frozenr   r9   start_stopsteprA   r    r   rC   r$   )r&   rm   Zindex_slicerp   r(   r   r   r   r+     s    

zSlicedIndex.__init__c             C   s   d| j | j d | j  S )Nr   )stoprr   rt   )r&   r   r   r   length  s    zSlicedIndex.lengthc             C   s   | j rt| jS | jS )zs
        The stopping position of the slice, or the end of the
        index if this is an original slice.
        )rp   r    rm   rs   )r&   r   r   r   ru     s    zSlicedIndex.stopc             C   s^   | j dkrt| jtddS || j \}}}| |}| |}| j| }t| j|||fS )z
        Returns another slice of this Index slice.

        Parameters
        ----------
        item : slice
            Index slice
        r   r   )rv   rc   rm   rA   rC   orig_coordsrt   )r&   rd   rr   ru   rt   	new_startZnew_stopZnew_stepr   r   r   re     s    	



zSlicedIndex.__getitem__c             C   s   | j r
|S t|}|| j }| jdkrBt|| jdk}|| }| jdkrf|dk|| j| j k @ }n|dk|| j| j k@ }|| | j S dS )a$  
        Convert the input rows to the sliced coordinate system.

        Parameters
        ----------
        rows : list
            Rows in the original coordinate system

        Returns
        -------
        sliced_rows : list
            Rows in the sliced coordinate system
        r   r   N)rp   r#   arrayrr   rt   modru   )r&   rK   Zrow0Zcorrect_modokr   r   r   sliced_coords  s    



zSlicedIndex.sliced_coordsc             C   s   | j r
|S | j|| j  S )a8  
        Convert the input row from sliced coordinates back
        to original coordinates.

        Parameters
        ----------
        row : int
            Row in the sliced coordinate system

        Returns
        -------
        orig_row : int
            Row in the original coordinate system
        )rp   rr   rt   )r&   rL   r   r   r   rw     s    zSlicedIndex.orig_coordsc             C   s   |  | j|S )N)r|   rm   rQ   )r&   r<   r   r   r   rQ     s    zSlicedIndex.findc             C   s   |  | j|S )N)r|   rm   where)r&   Zcol_mapr   r   r   r}     s    zSlicedIndex.wherec             C   s   |  | j||S )N)r|   rm   rB   )r&   rU   rV   r   r   r   rB     s    zSlicedIndex.rangec             C   s   |  | j|S )N)r|   rm   rS   )r&   r<   r   r   r   rS     s    zSlicedIndex.same_prefixc             C   s   |  | j S )N)r|   rm   rb   )r&   r   r   r   rb     s    zSlicedIndex.sorted_datac             C   s    | j s| j| ||| d S )N)rq   rm   r\   rw   )r&   rL   r'   r[   r   r   r   r\     s    zSlicedIndex.replacec             C   s   | j st| j| _| jS )N)rp   r   rm   )r&   r   r   r   get_index_or_copy  s    zSlicedIndex.get_index_or_copyc             C   s"   | j s|  | ||| d S )N)rq   r~   r=   rw   )r&   r:   r;   r   r   r   r   r=     s    zSlicedIndex.insert_rowc                s    fdd j |D S )Nc                s   g | ]}  |qS r   )rw   )rN   x)r&   r   r   rO     s    z1SlicedIndex.get_row_specifier.<locals>.<listcomp>)rm   rE   )r&   rD   r   )r&   r   rE     s    
zSlicedIndex.get_row_specifierc             C   s   | j s|  | d S )N)rq   r~   rM   )r&   rD   r   r   r   rM     s    zSlicedIndex.remove_rowsc                s$    j s  j fdd|D  d S )Nc                s   g | ]}  |qS r   )rw   )rN   r   )r&   r   r   rO   #  s    z,SlicedIndex.replace_rows.<locals>.<listcomp>)rq   rm   r_   )r&   r`   r   )r&   r   r_   !  s    zSlicedIndex.replace_rowsc             C   s   | j s|    d S )N)rq   r~   ra   )r&   r   r   r   ra   %  s    zSlicedIndex.sortc             C   sJ   | j r
dnd| j d| j d| j }d| jj d| j  | d| j dS )N z slice=:rf   z
 original=z index=rg   )rp   rr   ru   rt   r   r   rm   )r&   Z	slice_strr   r   r   rh   )  s    &zSlicedIndex.__repr__c             C   s   | j || d S )N)rm   r2   )r&   r0   r1   r   r   r   r2   .  s    zSlicedIndex.replace_colc             C   s   | j   d S )N)rm   r3   )r&   r   r   r   r3   1  s    zSlicedIndex.reloadc             C   s   | j |S )N)rm   r-   )r&   r5   r   r   r   r-   4  s    zSlicedIndex.col_positionc          	   C   s   ddl m} t| jdkrDt|g| jjd}| j|tdddddS || jdd	}|d
 || j	 }W dQ R X t|| jjd}| j|tdddddS )a  
        Return a newly created index from the given slice.

        Parameters
        ----------
        col_slice : Column object
            Already existing slice of a single column
        item : list or ndarray
            Slice for retrieval
        r   )r   )r   r   NT)rp   F)r   discard_on_copy)
r   r   r    r   r   r   r   rA   Z
index_modevalues)r&   r`   rd   r   rm   tZnew_colsr   r   r   	get_slice7  s    zSlicedIndex.get_slicec             C   s   | j jS )N)rm   r   )r&   r   r   r   r   M  s    zSlicedIndex.columnsc             C   s   | j jS )N)rm   r   )r&   r   r   r   r   Q  s    zSlicedIndex.dataN)F)r   r	   r
   r   r+   propertyrv   ru   re   r|   rw   rQ   r}   rB   rS   rb   r\   r~   r=   rE   rM   r_   ra   rh   r2   r3   r-   r   r   r   r   r   r   r   rc     s4   
rc   c             C   s   |dk	r|dk	rt d|dkr0|dkr0t d|dk	rBt|}n
t|j}|t| jksht | dx>|D ]6}x0| | jjD ] }tdd |jD |kr|S qW qnW dS )a  
    Inputs a table and some subset of its columns as table_copy.
    List or tuple containing names of columns as names,and returns an index
    corresponding to this subset or list or None if no such index exists.

    Parameters
    ----------
    table : `Table`
        Input table
    table_copy : `Table`, optional
        Subset of the columns in the ``table`` argument
    names : list, tuple, optional
        Subset of column names in the ``table`` argument

    Returns
    -------
    Index of columns or None

    NzBone and only one argument from "table_copy" or "names" is requiredz! is not a subset of table columnsc             S   s   g | ]}|j jqS r   )r.   r/   )rN   r'   r   r   r   rO   |  s    zget_index.<locals>.<listcomp>)r!   setr%   r.   rC   r   )r   Z
table_copynamesr/   rm   r   r   r   	get_indexV  s    


r   c             C   s<   t |}x.| jD ] }dd |jD }||kr|S qW dS dS )z
    Returns an index in ``table`` corresponding to the ``names`` columns or None
    if no such index exists.

    Parameters
    ----------
    table : `Table`
        Input table
    nmaes : tuple, list
        Column names
    c             S   s   g | ]}|j jqS r   )r.   r/   )rN   r'   r   r   r   rO     s    z&get_index_by_names.<locals>.<listcomp>N)r   rC   r   )r   r   rm   Zindex_namesr   r   r   get_index_by_names  s    r   c               @   s4   e Zd ZdZi Zdd Zdd Zdd Zdd	 Zd
S )_IndexModeContexta~  
    A context manager that allows for special indexing modes, which
    are intended to improve performance. Currently the allowed modes
    are "freeze", in which indices are not modified upon column modification,
    "copy_on_getitem", in which indices are copied upon column slicing,
    and "discard_on_copy", in which indices are discarded upon table
    copying/slicing.
    c             C   s,   || _ || _g | _|dkr(td|dS )a?  
        Parameters
        ----------
        table : Table
            The table to which the mode should be applied
        mode : str
            Either 'freeze', 'copy_on_getitem', or 'discard_on_copy'.
            In 'discard_on_copy' mode,
            indices are not copied whenever columns or tables are copied.
            In 'freeze' mode, indices are not modified whenever columns are
            modified; at the exit of the context, indices refresh themselves
            based on column values. This mode is intended for scenarios in
            which one intends to make many additions or modifications on an
            indexed column.
            In 'copy_on_getitem' mode, indices are copied when taking column
            slices as well as table slices, so col[i0:i1] will preserve
            indices.
        )freezer   copy_on_getitemzUExpected a mode of either 'freeze', 'discard_on_copy', or 'copy_on_getitem', got '{}'N)r   mode_orig_classesr!   r   )r&   r   r   r   r   r   r+     s    z_IndexModeContext.__init__c             C   sn   | j dkrd| j_nV| j dkrRxJ| jj D ] }| j|j | |j|_q,W nx| jj	D ]
}d|_
q\W d S )Nr   Fr   T)r   r   _copy_indicesr   r   r   r"   r   _get_copy_on_getitem_shimrC   rq   )r&   r'   rm   r   r   r   	__enter__  s    


z_IndexModeContext.__enter__c             C   sj   | j dkrd| j_nR| j dkrFxFt| jj D ]}| j |_q0W n x| jj	D ]}d|_
|  qPW d S )Nr   Tr   F)r   r   r   rH   r   r   r   popr   rC   rq   r3   )r&   exc_type	exc_value	tracebackr'   rm   r   r   r   __exit__  s    


z_IndexModeContext.__exit__c                sR    | j kr| j   S  fdd}d j d}tt| fd|i}|| j  < |S )a  
        This creates a subclass of the column's class which overrides that
        class's ``__getitem__``, such that when returning a slice of the
        column, the relevant indices are also copied over to the slice.

        Ideally, rather than shimming in a new ``__class__`` we would be able
        to just flip a flag that is checked by the base class's
        ``__getitem__``.  Unfortunately, since the flag needs to be a Python
        variable, this slows down ``__getitem__`` too much in the more common
        case where a copy of the indices is not needed.  See the docstring for
        ``astropy.table._column_mixins`` for more information on that.
        c                s4     | |}t|t| kr0| j||t| }|S )N)re   r   r.   Zslice_indicesr    )r&   rd   value)clsr   r   re     s    z@_IndexModeContext._get_copy_on_getitem_shim.<locals>.__getitem___ZWithIndexCopyre   )_col_subclassesr   r   str)r&   r   re   clsnameZnew_clsr   )r   r   r     s    


z+_IndexModeContext._get_copy_on_getitem_shimN)	r   r	   r
   r   r   r+   r   r   r   r   r   r   r   r     s   r   c                   s,   e Zd ZdZ fddZ fddZ  ZS )TableIndicesz
    A special list of table indices allowing
    for retrieval by column name(s).

    Parameters
    ----------
    lst : list
        List of indices
    c                s   t  | d S )N)ri   r+   )r&   lst)r   r   r   r+     s    zTableIndices.__init__c          	      s   t |tr|g}t |ttfrt|}xT| D ]L}y2x|D ]}|| q8W t|jt|kr`|S W q, tk
rv   Y q,X q,W td| t	 
|S )z
        Retrieve an item from the list of indices.

        Parameters
        ----------
        item : int, str, tuple, or list
            Position in list or name(s) of indexed column(s)
        zNo index found for )r   r   r   r9   r-   r    r   r!   
IndexErrorri   re   )r&   rd   rm   r/   )r   r   r   re     s    	



zTableIndices.__getitem__)r   r	   r
   r   r+   re   ro   r   r   )r   r   r     s   	r   c               @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )TableLocz
    A pseudo-list of Table rows allowing for retrieval
    of rows by indexed column values.

    Parameters
    ----------
    table : Table
        Indexed table to use
    c             C   s(   || _ |j| _t| jdkr$tdd S )Nr   z-Cannot create TableLoc object with no indices)r   rC   r    r!   )r&   r   r   r   r   r+   *  s    zTableLoc.__init__c             C   s   t |tr|\}}n| jj}| j| }t|jdkr<tdt |tr|j	dkrVt
 n|j	}|jdkrlt n|j}||f|f}nZt |ttjfs|g}g }x>|D ]6}||f}t|dkrtd| q|| qW |S )z=
        Retrieve Table rows indexes by value slice.
        r   z'Cannot use .loc on multi-column indicesNr   zNo matches found for key )r   r9   r   primary_keyrC   r    r   r!   rA   rr   r   ru   r   rB   r   r#   r@   rQ   KeyErrorextend)r&   rd   r<   rm   rr   ru   rK   pr   r   r   	_get_rows0  s&    




zTableLoc._get_rowsc             C   sJ   |  |}t|dkr&td| nt|dkr@| j|d  S | j| S )a  
        Retrieve Table rows by value slice.

        Parameters
        ----------
        item : column element, list, ndarray, slice or tuple
            Can be a value of the table primary index, a list/ndarray
            of such values, or a value slice (both endpoints are included).
            If a tuple is provided, the first element must be
            an index to use instead of the primary key, and the
            second element must be as above.
        r   zNo matches found for key r   )r   r    r   r   )r&   rd   rK   r   r   r   re   P  s    
zTableLoc.__getitem__c             C   s   |  |}t|dkr&td| ndt|dkrB|| j|d < nHt|t|krvx6t||D ]\}}|| j|< q^W ntdt| ddS )aP  
        Assign Table row's by value slice.

        Parameters
        ----------
        key : column element, list, ndarray, slice or tuple
              Can be a value of the table primary index, a list/ndarray
              of such values, or a value slice (both endpoints are included).
              If a tuple is provided, the first element must be
              an index to use instead of the primary key, and the
              second element must be as above.

        value : New values of the row elements.
                Can be a list of tuples/lists to update the row.
        r   zNo matches found for key r   zRight side should contain z valuesN)r   r    r   r   zipr!   )r&   r<   r   rK   rL   r[   r   r   r   __setitem__e  s    
zTableLoc.__setitem__N)r   r	   r
   r   r+   r   re   r   r   r   r   r   r     s
   	 r   c               @   s   e Zd Zdd ZdS )TableLocIndicesc             C   s>   |  |}t|dkr&td| nt|dkr:|d S |S )a  
        Retrieve Table row's indices by value slice.

        Parameters
        ----------
        item : column element, list, ndarray, slice or tuple
               Can be a value of the table primary index, a list/ndarray
               of such values, or a value slice (both endpoints are included).
               If a tuple is provided, the first element must be
               an index to use instead of the primary key, and the
               second element must be as above.
        r   zNo matches found for key r   )r   r    r   )r&   rd   rK   r   r   r   re     s    
zTableLocIndices.__getitem__N)r   r	   r
   re   r   r   r   r   r     s   r   c                   s(   e Zd ZdZ fddZdd Z  ZS )	TableILocz
    A variant of TableLoc allowing for row retrieval by
    indexed order rather than data values.

    Parameters
    ----------
    table : Table
        Indexed table to use
    c                s   t  | d S )N)ri   r+   )r&   r   )r   r   r   r+     s    zTableILoc.__init__c             C   sZ   t |tr|\}}n| jj}| j| }| | }| j| }t|dkrVtd| |S )Nr   zInvalid index for iloc: )r   r9   r   r   rC   rb   r    r   )r&   rd   r<   rm   rK   Ztable_slicer   r   r   re     s    



zTableILoc.__getitem__)r   r	   r
   r   r+   re   ro   r   r   )r   r   r     s   	r   )NN)r   copyr   numpyr#   bstr   r   Zsorted_arrayr   r!   r   r   rc   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s"     b H
,_)c