o
    _~#gz                     @   st  d dl Z d dlZd dlZd dlmZmZ d dlm  m	Z
 d dlZd dlmZmZmZmZmZ d dlmZ d dlmZ ejddgdd d	d
d Zejdejde
ddgdd d	dd Zdd ZG dd dZG dd dZG dd dZe
dG dd dZ e
dG dd de Z!G dd de Z"G dd  d e!Z#G d!d" d"Z$G d#d$ d$Z%G d%d& d&Z&dS )'    N)NumExprClobberingErrorUndefinedVariableError)	DataFrameIndex
MultiIndexSeries
date_range)NUMEXPR_INSTALLEDpythonpandasc                 C      | S N xr   r   /var/www/static.ux5.de/https/Moving-Object-Detection-with-OpenCV/env/lib/python3.10/site-packages/pandas/tests/frame/test_query_eval.py<lambda>       r   )paramsidsc                 C      | j S r   paramrequestr   r   r   parser   s   r   numexpr)marksc                 C   r   r   r   r   r   r   r   r      r   c                 C   r   r   r   r   r   r   r   engine   s   r   c                 C   s    | dkrt d|   d S d S )Nr   zcannot evaluate with parser=)pytestskip)r   r   r   r   skip_if_no_pandas_parser%   s   r!   c                   @   sV   e Zd Zejdd Zejdd Zejdd Zdd Zd	d
 Z	dd Z
dd ZdS )
TestCompatc                 C   s   t dg diS )NA         r   selfr   r   r   df+   s   zTestCompat.dfc                 C   s   ||j dk S )Nr   r#   r*   r+   r   r   r   	expected1/   s   zTestCompat.expected1c                 C   s
   |j d S Nr%   r,   r-   r   r   r   	expected23   s   
zTestCompat.expected2c                 C   s4   | d}t|| |d}tj||dd d S )NA>0A+1Fcheck_namesquerytmassert_frame_equalevalassert_series_equalr*   r+   r.   r0   resultr   r   r   test_query_default7   s   

zTestCompat.test_query_defaultc                 C   s<   |j dd d}t|| |jdd d}tj||dd d S )Nr1   r   r2   Fr3   r5   r;   r   r   r   test_query_None?      zTestCompat.test_query_Nonec                 C   s<   |j ddd}t|| |jddd}tj||dd d S )Nr1   r
   r>   r2   Fr3   r5   r;   r   r   r   test_query_pythonE   r@   zTestCompat.test_query_pythonc                 C   s   t r |jddd}t|| |jddd}tj||dd d S d}tjt|d |jddd W d    n1 s;w   Y  tjt|d |jddd W d    d S 1 sZw   Y  d S )	Nr1   r   r>   r2   Fr3   z~'numexpr' is not installed or an unsupported version. Cannot use engine='numexpr' for query/eval if 'numexpr' is not installedmatch)	r	   r6   r7   r8   r9   r:   r   raisesImportError)r*   r+   r.   r0   r<   msgr   r   r   test_query_numexprK   s   "zTestCompat.test_query_numexprN)__name__
__module____qualname__r   fixturer+   r.   r0   r=   r?   rA   rG   r   r   r   r   r"   *   s    


r"   c                   @   sh   e Zd Zejdddgejdg ddd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd ZdS )TestDataFrameEvaln   i  zop_str,op,rop))+__add____radd__)-__sub____rsub__)*__mul____rmul__)/__truediv____rtruediv__c           
      C   s   t dt|tdd}d|jd< | }t t|j||dtdd}t	d| d	}t	d
| d	}	t
|	| |dv rNt|||}	t
|	| d S |dv rat|||}	t
|	| d S d S )Nr%   abcdindexcolumnsr&   r   r^   zbase z dfzm )rO   rU   )rR   rX   )r   rangelistilocmeannptilevaluesreshaper9   r7   r8   getattr)
r*   op_stroproprM   r+   mbaseexpectedr<   r   r   r   test_ops_   s    
zTestDataFrameEval.test_opsc                 C   sj   t dtjddi}tj|jdd< dt|jdd  }dt| jdd }t	|| d S )Nr#   r&   ia  r      r%      )
r   re   randomdefault_rngstandard_normalnanrc   isnanr7   r8   )r*   r+   ro   r<   r   r   r   test_dataframe_sub_numexpr_path   s
   z1TestDataFrameEval.test_dataframe_sub_numexpr_pathc                 C   s   t g dg dd}d}tjt|d |dd  W d    n1 s&w   Y  tjt|d |d W d    d S 1 sCw   Y  d S )	Nr$   )abrz   r#   Bz%expr must be a string to be evaluatedrB   c                 S   s
   | j dkS )Nrz   )r|   r   r   r   r   r      s   
 z6TestDataFrameEval.test_query_non_str.<locals>.<lambda>o   r   r   rD   
ValueErrorr6   r*   r+   rF   r   r   r   test_query_non_str   s   "z$TestDataFrameEval.test_query_non_strc                 C   sR   t dg di}d}tjt|d |d W d    d S 1 s"w   Y  d S )Nr#   r$   zexpr cannot be an empty stringrB    r~   r   r   r   r   test_query_empty_string   s
   "z)TestDataFrameEval.test_query_empty_stringc                 C   s~   t tjddtdd}ddi}ddi}|jd||gd	|d |d  ks*J tjd||gd	|d |d  ks=J d S )
Nr&   
   r&   abr`   ry   r%   rz   a + b	resolvers)r   re   rs   rt   ru   rb   r9   pd)r*   r+   dict1dict2r   r   r   test_eval_resolvers_as_list   s   &*z-TestDataFrameEval.test_eval_resolvers_as_listc                 C   s^   t tjddtdd}ddi}|jd|gd}|d |d	 |d   }t|| d S )
Nr&   r   r   r`   cz	a + b * cr   ry   rz   )	r   re   rs   rt   ru   rb   r9   r7   r:   )r*   r+   r   r<   ro   r   r   r   test_eval_resolvers_combined   s   z.TestDataFrameEval.test_eval_resolvers_combinedc                 C   s@   t dddgi}|d}t ddgddgd}t|| d S )Na1YNzc = ((a1 == 'Y') & True)TF)r   r   )r   r9   r7   r8   )r*   r+   resro   r   r   r   test_eval_object_dtype_binop   s   
z.TestDataFrameEval.test_eval_object_dtype_binopN)rH   rI   rJ   r   markparametrizerp   rx   r   r   r   r   r   r   r   r   r   rL   ]   s    		
rL   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
 TestDataFrameQueryWithMultiIndexc                 C   s4  t | tjdjddgdd}tjdjddgdd}tj||gdd	gd
}ttjdd|d}t	|j
dj|dd}|jd||d}|jd||d}	||dk }
t||
 t|	|
 |jd||d}|jd||d}	||dk }
t||
 t|	|
 |jd||d}|jd||d}	||dg }
t||
 t|	|
 |jd||d}|jd||d}	||dg  }
t||
 t|	|
 |jd||d}|jd||d}	||dg }
t||
 t|	|
 |jd||d}|jd||d}	||dg  }
t||
 t|	|
 d S )Nr&   redgreenr   sizeeggshamcolorfoodnamesr   r]   r]   namezcolor == "red"r   r   z"red" == colorzcolor != "red"z"red" != colorzcolor == ["red"]z["red"] == colorzcolor != ["red"]z["red"] != colorz["red"] in colorz"red" in colorz["red"] not in colorz"red" not in colorr!   re   rs   rt   choicer   from_arraysr   ru   r   r]   get_level_valuesrg   r6   r7   r8   isinr*   r   r   ry   rz   r]   r+   indres1res2expr   r   r    test_query_with_named_multiindex   sL   zATestDataFrameQueryWithMultiIndex.test_query_with_named_multiindexc                 C   s  t | tjdjddgdd}tjdjddgdd}t||g}ttjdd|d	}t	|j
d
j|d	}|jd||d}|jd||d}	||dk }
t||
 t|	|
 |jd||d}|jd||d}	||dk }
t||
 t|	|
 |jd||d}|jd||d}	||dg }
t||
 t|	|
 |jd||d}|jd||d}	||dg  }
t||
 t|	|
 |jd||d}|jd||d}	||dg }
t||
 t|	|
 |jd||d}|jd||d}	||dg  }
t||
 t|	|
 t	|j
dj|d	}|jd||d}|jd||d}	||dk }
t||
 t|	|
 |jd||d}|jd||d}	||dk }
t||
 t|	|
 |jd||d}|jd||d}	||dg }
t||
 t|	|
 |jd||d}|jd ||d}	||dg  }
t||
 t|	|
 |jd!||d}|jd"||d}	||dg }
t||
 t|	|
 |jd#||d}|jd$||d}	||dg  }
t||
 t|	|
 d S )%Nr&   r   r   r   r   r   r   r   r   r   ilevel_0 == "red"r   z"red" == ilevel_0ilevel_0 != "red"z"red" != ilevel_0zilevel_0 == ["red"]z["red"] == ilevel_0zilevel_0 != ["red"]z["red"] != ilevel_0z["red"] in ilevel_0z"red" in ilevel_0z["red"] not in ilevel_0z"red" not in ilevel_0r%   zilevel_1 == "eggs"z"eggs" == ilevel_1zilevel_1 != "eggs"z"eggs" != ilevel_1zilevel_1 == ["eggs"]z["eggs"] == ilevel_1zilevel_1 != ["eggs"]z["eggs"] != ilevel_1z["eggs"] in ilevel_1z"eggs" in ilevel_1z["eggs"] not in ilevel_1z"eggs" not in ilevel_1r   r   r   r   r   "test_query_with_unnamed_multiindex   s   zCTestDataFrameQueryWithMultiIndex.test_query_with_unnamed_multiindexc           
      C   s^  t | tjdjddgdd}td}t||g}d dg|_t	tjd
d|d}|jd	||d
}t|jdj|dd}||dk }	t||	 |jd||d
}t|jdj|dd}||dk }	t||	 |jd||d
}t|jdj|d}||dk }	t||	 |jd||d
}t|jdj|d}||dk }	t||	 d S )Nr&   r   r   r   r   ratingr   r   zrating == 1r   r   r%   zrating != 1r   r   r   )r!   re   rs   rt   r   aranger   r   r   r   ru   r6   r   r]   r   rg   r7   r8   )
r*   r   r   ry   rz   r]   r+   r   r   r   r   r   r   *test_query_with_partially_named_multiindexK  s4   

zKTestDataFrameQueryWithMultiIndex.test_query_with_partially_named_multiindexc                 C   s   t tdtjdd tdD ddgdd}| }d	d
 }|j }|j	|||j	d||j	d|d}|
 D ]$\}}t|trL||| sKJ q9t|trZt|||  q9tdd S )Nr   r'   c                 S   s   g | ]}t d qS )r   )ra   ).0_r   r   r   
<listcomp>n  s    z^TestDataFrameQueryWithMultiIndex.test_query_multiindex_get_index_resolvers.<locals>.<listcomp>r&   spamr   r   r   c                 S   s   |  |}| }| |_|S r   )r   	to_seriesr]   )milevellevel_valuessr   r   r   r   s  s   
z]TestDataFrameQueryWithMultiIndex.test_query_multiindex_get_index_resolvers.<locals>.to_series)r]   r^   r   r   clevel_0z object must be a Series or Index)r   re   onesr   r   ra   _get_index_resolversr^   r   r]   items
isinstancer   is_r   r7   r:   AssertionError)r*   r+   r   r   
col_seriesro   kvr   r   r   )test_query_multiindex_get_index_resolversj  s,   




zJTestDataFrameQueryWithMultiIndex.test_query_multiindex_get_index_resolversN)rH   rI   rJ   r   r   r   r   r   r   r   r   r      s
    2Xr   c                   @   s  e Zd Zejdd Zej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+d, Zd-d. Zd/d0 Zejd1d2ej gd3ej!ggd4d5 Z"d6d7 Z#d8d9 Z$d:S );TestDataFrameQueryNumExprPandasc                 C      dS Nr   r   r)   r   r   r   r        z&TestDataFrameQueryNumExprPandas.enginec                 C   r   Nr   r   r)   r   r   r   r     r   z&TestDataFrameQueryNumExprPandas.parserc                 C   s   t | ttjdd}tddd|d< tddd|d< td	dd|d
< |jd||d}||jdk d|j	k @  }t
|| d S )Nr&   rq   r'   1/1/2012rq   periodsdates11/1/2013dates21/1/2014dates3z"@df.dates1 < 20130101 < @df.dates3r   r   20130101)r!   r   re   rs   rt   ru   r   r6   r   r   r7   r8   r*   r   r   r+   r   expecr   r   r   %test_date_query_with_attribute_access  s   zETestDataFrameQueryNumExprPandas.test_date_query_with_attribute_accessc                 C   ~   t tjdd}tddd|d< tddd|d< td	dd|d
< |jd||d}||jdk d|jk @  }t	
|| d S )Nr&   r   r   rq   r   r   r   r   r   r   dates1 < 20130101 < dates3r   r   r   re   rs   rt   ru   r   r6   r   r   r7   r8   r   r   r   r   #test_date_query_no_attribute_access  s   zCTestDataFrameQueryNumExprPandas.test_date_query_no_attribute_accessc                 C      d}t tjd|df}td|d|d< td|d|d< td	|d|d
< tj|jtjd|dkdf< tj|jtjd|dkd
f< |j	d||d}||j
dk d|jk @  }t|| d S )Nr   r&   r'   r   r   r   r   r   r   r         ?r   r   r   r   re   rs   rt   ru   r   r   NaTlocr6   r   r   r7   r8   r*   r   r   rM   r+   r   r   r   r   r   test_date_query_with_NaT  s   ""z8TestDataFrameQueryNumExprPandas.test_date_query_with_NaTc                 C      d}t tjd|df}td|d|d< td|d|d< |jdd	d	d
}|d u s-J |jd||d}||jdk d|j	k @  }t
|| d S )Nr   r&   r'   r   r   r   r   r   Tinplacedropindex < 20130101 < dates3r   r   r   re   rs   rt   ru   r   	set_indexr6   r]   r   r7   r8   r*   r   r   rM   r+   return_valuer   r   r   r   r   test_date_index_query  s   z5TestDataFrameQueryNumExprPandas.test_date_index_queryc                 C      d}t tjd|dfdti}td|d|d< td|d|d	< tj	|j
d
< |jdddd}|d u s8J |jd||d}||jdk d|jk @  }t|| d S )Nr   r&   r'   r   r   r   r   r   r   r   r   Tr   r   r   r   r   re   rs   rt   ru   astypeobjectr   r   r   rc   r   r6   r]   r   r7   r8   r   r   r   r   test_date_index_query_with_NaT  s   z>TestDataFrameQueryNumExprPandas.test_date_index_query_with_NaTc           	      C   s   d}i }t d|d|d< t d|d|d< t|}tj|jtjd|dkdf< |jdd	d	d
}|d u s7J |j	d||d}||j
 dk d|jk @  }t|| d S )Nr   r   r   r   r   r   r&   r   Tr   r   r   r   )r   r   r   r   r   re   rs   rt   r   r6   r]   r   r   r7   r8   )	r*   r   r   rM   dr+   r   r   r   r   r   r   )test_date_index_query_with_NaT_duplicates  s   "zITestDataFrameQueryNumExprPandas.test_date_index_query_with_NaT_duplicatesc              	   C   s   d}t td|dt|d}|jd||d}t|dks J |jd||d}t|| d	}d
D ]%}tj	t
|d |jd| d||d W d    n1 sRw   Y  q2d S )Nr   r   r   )datesnondatezdates == nondater   r   zdates != nondatez=Invalid comparison between dtype=datetime64\[ns\] and ndarray)<><=>=rB   zdates z nondate)r   r   re   r   r6   lenr7   r8   r   rD   	TypeError)r*   r   r   rM   r+   r<   rF   rk   r   r   r   test_date_query_with_non_date  s   z=TestDataFrameQueryNumExprPandas.test_date_query_with_non_datec                 C   sh   t tdtddtddd}d}tjt|d |jd	||d
 W d    d S 1 s-w   Y  d S )Nr   r'      rN      )irO   rzinvalid syntaxrB   zi - +r   )r   ra   r   rD   SyntaxErrorr6   )r*   r   r   r+   rF   r   r   r   test_query_syntax_error  s
    "z7TestDataFrameQueryNumExprPandas.test_query_syntax_errorc                 C   s  t | ttjddtdd}d\}}|jd||d}||j|j	k }t
|| |jd||d}|||j	k }t
|| tjtd	d
 |jd||d W d    n1 s\w   Y  tjtdd
 |jd||d W d    d S 1 s|w   Y  d S )Nr&   )   r&   r   r`   )r%   r&   za > br   z@a > b!local variable 'c' is not definedrB   z@a > b > @czname 'c' is not definedz
@a > b > c)r!   r   re   rs   rt   ru   rb   r6   ry   rz   r7   r8   r   rD   r   )r*   r   r   r+   ry   rz   r   ro   r   r   r   test_query_scope  s&   "z0TestDataFrameQueryNumExprPandas.test_query_scopec                 C   sr   d }}t tjdj||dfdtdd}tjtdd |j	d	||d
 W d    d S 1 s2w   Y  d S )Nr   r&   r'   r   abcr`   zname 'sin' is not definedrB   sin > 5r   )
r   re   rs   rt   integersrb   r   rD   r   r6   )r*   r   r   rM   rm   r+   r   r   r   test_query_doesnt_pickup_local  s   "z>TestDataFrameQueryNumExprPandas.test_query_doesnt_pickup_localc                 C   s~   d }}t tjdj||dfdtdd}d|j_d}tj	t
|d	 |jd
||d W d    d S 1 s8w   Y  d S )Nr   r&   r'   r   r  r`   sinzVariables in expression.+rB   r  r   )r   re   rs   rt   r  rb   r]   r   r   rD   r   r6   )r*   r   r   rM   rm   r+   rF   r   r   r   test_query_builtin  s   "z2TestDataFrameQueryNumExprPandas.test_query_builtinc                 C   sv   t tjddg dd}t|jd||d||j|j	k   t|jd||d||j|j	 |j	|j
 k  d S )Nr&   r   ry   rz   r   r`   a < br   za + b > b * c)r   re   rs   rt   ru   r7   r8   r6   ry   rz   r   r*   r   r   r+   r   r   r   
test_query"  s   z*TestDataFrameQueryNumExprPandas.test_queryc                 C   s   t tjdjdddttdddg dd}|jd	||d
}||jdk |j	|j
k @  }t|| |jd||d
}||j|j
k  }t|| d S )Nr&   r   r   r   blobr   r  r\   z(blob < 5) & (a < b)r   rq   zblob < b)r   re   rs   rt   r  r   ra   r6   r]   ry   rz   r7   r8   r   r   r   r   test_query_index_with_name/  s   z:TestDataFrameQueryNumExprPandas.test_query_index_with_namec                 C   s   t tjdjdddtdg dd}|jd||d}||j|jk  }t	
|| |jd	||d}||jd
k  }t	
|| d S )Nr&   r   r   r   r  r\   z	index < br   z	index < 5rq   )r   re   rs   rt   r  ra   r6   r]   rz   r7   r8   r   r   r   r   test_query_index_without_name>  s   z=TestDataFrameQueryNumExprPandas.test_query_index_without_namec                 C   s   t | ttjdd}ttjdd}||dk|dk@  }|jd||d}t|| t	j
d||d}t|| t	j
d||d}||dk|dk@ ||dk dk@  }t|| t	j
d||d}|jd	||d}t|| d S )
Nr&   r   r   z(@df > 0) & (@df2 > 0)r   zdf[df > 0 and df2 > 0]z)df[df > 0 and df2 > 0 and df[df > 0] > 0]zdf[(df>0) & (df2>0)](@df>0) & (@df2>0))r!   r   re   rs   rt   ru   r6   r7   r8   r   r9   )r*   r   r   r+   df2ro   r<   r   r   r   test_nested_scopeO  s    $z1TestDataFrameQueryNumExprPandas.test_nested_scopec                 C   sZ   t tjdd}tjtdd |jd||d W d    d S 1 s&w   Y  d S )Nr&   r   name 'df' is not definedrB   zdf > 0r   )	r   re   rs   rt   ru   r   rD   r   r6   r  r   r   r   *test_nested_raises_on_local_self_referencef  s   "zJTestDataFrameQueryNumExprPandas.test_nested_raises_on_local_self_referencec                 C   s   t | ttjddtdd}d}||j|k  }|jd||d}t	
|| ||j|jk  }|jd||d}t	
|| d S )	Nr&   )d   r   
abcdefghijr`   r%   za < @br   r  )r!   r   re   rs   rt   ru   rb   ry   r6   r7   r8   rz   )r*   r   r   r+   rz   expectr<   r   r   r   test_local_syntaxm  s   z1TestDataFrameQueryNumExprPandas.test_local_syntaxc                 C   s   t | td}ttjddt|f|d}|jd||d}|j	|j
k |j
|jk @ |j
|j	 @ |j|j
 @ }|| }t|| d S )Nr  r&   r!  r`   z!a < b < c and a not in b not in cr   )r!   rb   r   re   rs   rt   ru   r  r6   ry   rz   r   r   r7   r8   )r*   r   r   colsr+   r   r   r   r   r   r   test_chained_cmp_and_in}  s   8z7TestDataFrameQueryNumExprPandas.test_chained_cmp_and_inc                 C   s   t | ttjdjddddd}ttjdjddddd}t||d	}|j|jd
 	| }|j
d||d}t|| ttjdjddddd}|j|d
 	| }|j
d||d}t|| d S )Nr&   r'      r   ry   r  r   rz   ry   rz   r%   z
b - 1 in ar   z@b - 1 in a)r!   r   re   rs   rt   r  r   r   rz   r   r6   r7   r8   )r*   r   r   ry   rz   r+   ro   r<   r   r   r   test_local_variable_with_in  s   z;TestDataFrameQueryNumExprPandas.test_local_variable_with_inc                 C   sJ   t | d}tdg di}|jd||d}||jdk }t|| d S )Nr%   ry   )ry   ry   rz   rz   @cr*  z	a == "@c"r   r*  r!   r   r6   ry   r7   r8   )r*   r   r   r   r+   r<   ro   r   r   r   test_at_inside_string  s   z5TestDataFrameQueryNumExprPandas.test_at_inside_stringc                 C   sx   | j | j}}t| ttjddtdd}tj	t
dd |jd||d W d    d S 1 s5w   Y  d S )	Nr&   r   r   r`   r  rB   za == @cr   )r   r   r!   r   re   rs   rt   rb   r   rD   r   r6   r  r   r   r   test_query_undefined_local  s   "z:TestDataFrameQueryNumExprPandas.test_query_undefined_localc                 C   s<  d}t jddd }t|t jd|jd}d|j_|j	d||d}||d d	k }t
|| t|t jd|jd}|j	d
||d}|j|j|jd	k  }t
|| t|t jd|jd}d|j_|j	d||d}||jd	k }t
|| |j	d||d}|j|j|jd	k  }t
|| d S )Nr%   r  e   r&   )r]   rz   r]   z	index > 5r   rq   zilevel_0 > 5r(  ry   za > 5)re   r_r   rs   rt   ru   r   r]   r   r6   r7   r8   r   ry   )r*   r   r   rM   ry   r+   r<   ro   r   r   r   :test_index_resolvers_come_after_columns_with_the_same_name  s.   zZTestDataFrameQueryNumExprPandas.test_index_resolvers_come_after_columns_with_the_same_namezop, f==!=c           
      C   s   d}t tjd|tjd|d}tj|jd d ddf< d| d}|||jtj }|j|||d}	t	|	| d S )Nr   r&   r(  r   a z infr   )
r   re   rs   rt   infr   ry   r6   r7   r8   )
r*   rk   fr   r   rM   r+   qro   r<   r   r   r   test_inf  s   z(TestDataFrameQueryNumExprPandas.test_infc                 C   sf   |}t ddd|dd}t|d}t|d}|d}t|| t|}| d}t|| d S )Nz
2019-01-011dr   time)startfreqr   tzr   r   z"2018-01-03 00:00:00+00" < time)r   r   r6   r7   r8   reset_index)r*   tz_aware_fixturer<  df_indexro   r+   r<   r   r   r   test_check_tz_aware_index_query  s   



z?TestDataFrameQueryNumExprPandas.test_check_tz_aware_index_queryc                 C   s   d}t dtjd| tjd|d}||d ddk }|jd||d}t|| t ttjd|d	k tj	tjd
|tjd
|d}||d   }|jd
||d}t|| d S )Nr   r&   r(  ry   intr   za.astype('int') == 0r   r   za.notnull())r   re   rs   rt   r   r6   r7   r8   whererv   ru   notnull)r*   r   r   rM   r+   ro   r<   r   r   r   test_method_calls_in_query  s*   
z:TestDataFrameQueryNumExprPandas.test_method_calls_in_queryN)%rH   rI   rJ   r   rK   r   r   r   r   r   r   r   r   r  r
  r  r  r  r  r  r  r  r   r$  r&  r)  r,  r-  r0  r   r   operatoreqner7  r@  rD  r   r   r   r   r     s>    

	


r   c                   @   s`   e Zd Zejdd Zej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S )TestDataFrameQueryNumExprPythonc                 C   r   r   r   r)   r   r   r   r     r   z&TestDataFrameQueryNumExprPython.enginec                 C   r   Nr
   r   r)   r   r   r   r   
  r   z&TestDataFrameQueryNumExprPython.parserc                 C   r   )Nr&   r   r   rq   r   r   r   r   r   r   )(dates1 < 20130101) & (20130101 < dates3)r   r   r   r   r   r   r   r     s   zCTestDataFrameQueryNumExprPython.test_date_query_no_attribute_accessc                 C   r   )Nr   r&   r'   r   r   r   r   r   r   r   r   rJ  r   r   r   r   r   r   r   r     s   ""z8TestDataFrameQueryNumExprPython.test_date_query_with_NaTc                 C   r   )Nr   r&   r'   r   r   r   r   r   Tr   ((index < 20130101) & (20130101 < dates3)r   r   r   r   r   r   r   r   '  s   z5TestDataFrameQueryNumExprPython.test_date_index_queryc                 C   r   )Nr   r&   r'   r   r   r   r   r   r   r   Tr   rK  r   r   r   r   r   r   r   r   4  s   z>TestDataFrameQueryNumExprPython.test_date_index_query_with_NaTc                 C   s   d}t tjd|df}td|d|d< td|d|d< tj|jtjd|d	kdf< |j	dd
d
d}|d u s>J d}t
jt|d |jd||d W d    d S 1 s[w   Y  d S )Nr   r&   r'   r   r   r   r   r   r   Tr   "'BoolOp' nodes are not implementedrB   r   r   )r   re   rs   rt   ru   r   r   r   r   r   r   rD   NotImplementedErrorr6   )r*   r   r   rM   r+   r   rF   r   r   r   r   E  s   ""zITestDataFrameQueryNumExprPython.test_date_index_query_with_NaT_duplicatesc           	      C   s@  d}t jd||d}|dksJ ttjdd}ttjdd}d}tjt	|d |j
d||d W d    n1 sBw   Y  tjtd	d |j
d
||d W d    n1 saw   Y  ||dk|dk@  }t jd||d}t|| ||dk|dk@ ||dk dk@  }t jd||d}t|| d S )Nr%   zx + 1r   r&   r   z5The '@' prefix is only supported by the pandas parserrB   r  r  z(df>0) & (df2>0)r   zdf[(df > 0) & (df2 > 0)]z+df[(df > 0) & (df2 > 0) & (df[df > 0] > 0)])r   r9   r   re   rs   rt   ru   r   rD   r	  r6   r   r7   r8   )	r*   r   r   r   r<   r+   r  rF   ro   r   r   r   r  Q  s(   $z1TestDataFrameQueryNumExprPython.test_nested_scopec                 C   s   t g dg dd}d}tjt|d |d W d    n1 s$w   Y  d}tjt|d |d W d    d S 1 sCw   Y  d S )	Nr$   rN   rq      )minmaxzMVariables in expression \"\(min\) == \(1\)\" overlap with builtins: \('min'\)rB   zmin == 1zMVariables in expression \"\(max\) == \(1\)\" overlap with builtins: \('max'\)zmax == 1)r   r   rD   r   r6   )r*   r+   regex_to_matchr   r   r   +test_query_numexpr_with_min_and_max_columnsl  s   "zKTestDataFrameQueryNumExprPython.test_query_numexpr_with_min_and_max_columnsN)rH   rI   rJ   r   rK   r   r   r   r   r   r   r   r  rS  r   r   r   r   rH    s    

rH  c                   @   0   e Zd Zejdd Zejdd Zdd ZdS )TestDataFrameQueryPythonPandasc                 C   r   rI  r   r)   r   r   r   r   ~  r   z%TestDataFrameQueryPythonPandas.enginec                 C   r   r   r   r)   r   r   r   r     r   z%TestDataFrameQueryPythonPandas.parserc                 C   d   d }}t tjdj||dfdtdd}d|j_||jdk }|jd	||d
}t	
|| d S Nr   r&   r'   r   r  r`   r  rq   r  r   r   re   rs   rt   r  rb   r]   r   r6   r7   r8   r*   r   r   rM   rm   r+   ro   r<   r   r   r   r       z1TestDataFrameQueryPythonPandas.test_query_builtinNrH   rI   rJ   r   rK   r   r   r  r   r   r   r   rU  }      

rU  c                   @   rT  )TestDataFrameQueryPythonPythonc                 C   r   rI  r   r)   r   r   r   r     r   z%TestDataFrameQueryPythonPython.enginec                 C   r   rI  r   r)   r   r   r   r     r   z%TestDataFrameQueryPythonPython.parserc                 C   rV  rW  rX  rY  r   r   r   r    rZ  z1TestDataFrameQueryPythonPython.test_query_builtinNr[  r   r   r   r   r]    r\  r]  c                
   @   s   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Ze	j
ddejgdejgdejgdejggdd Zdd Zdd Ze	j
dg dg dg dg dg dg dgdd  Zd!S )"TestDataFrameQueryStringsc              	   C   s  t tjdddgd}ttd|d< ||jdk }|dkrd}d	}|gd |gd  }|d d d
 }d\}	}
d|	g|
g  }d}t|||D ]2\}}}| d| d| }t	j
t|d |j|||d|jid W d    n1 sww   Y  qJd S |jd||d}t|| |jd||d}t|| t|||jdg  ||jdk }|jd||d}t|| |jd||d}t|| t|||jdg   d S )Nr&   r   r%   rz   r`   
aabbccddeestringsry   r   z"a"r_   r1  r2  $'(Not)?In' nodes are not implemented rB   )r   r   
local_dictz"a" == stringsr   zstrings == "a"zstrings != "a"z"a" != strings)r   re   rs   rt   ru   r   rb   ra  zipr   rD   rM  r6   r7   r8   r   r*   r   r   r+   r#  collstlhsrhsrF  rG  opsrF   rk   exr   r   r   r   test_str_query_method  sD   
 z/TestDataFrameQueryStrings.test_str_query_methodc              	   C   s  t tjdddgd}ttd|d< ||jddg }|dkr~d}d	}|gd |gd  }|d d d
 }d\}	}
d|	g|
g  }d}t	|||D ].\}}}| d| d| }t
jt|d |j|||d W d    n1 svw   Y  qMd S |jd||d}t|| |jd||d}t|| ||jddg  }|jd||d}t|| |jd||d}t|| d S )Nr&   r_  rz   r`   r`  ra  ry   r   z
["a", "b"]r_   rb  rc  rd  rB   r   zstrings == ["a", "b"]z["a", "b"] == stringszstrings != ["a", "b"]z["a", "b"] != strings)r   re   rs   rt   ru   r   rb   ra  r   rf  r   rD   rM  r6   r7   r8   rg  r   r   r   test_str_list_query_method  s6   z4TestDataFrameQueryStrings.test_str_list_query_methodc                 C   s<  t tdtdtjdjdddtjdjdddd}|d	krY|jd
||d}||j|j	 }t
|| |jd||d}||j|j	|j|jk @  }t
|| d S d}tjt|d |jd
||d W d    n1 suw   Y  d}tjt|d |jd||d W d    d S 1 sw   Y  d S )Naaaabbbbccccaabbccddeeffr&   rq      r   	   ry   rz   r   r   r   za in br   za in b and c < drc  rB   rL  )r   rb   re   rs   rt   r  r6   ry   r   rz   r7   r8   r   r   r   rD   rM  )r*   r   r   r+   r   r   rF   r   r   r   test_query_with_string_columns  s,   "z8TestDataFrameQueryStrings.test_query_with_string_columnsc                 C   s  t tdtdtjdjdddtjdjdddd}|r'|d	kr'tnd }t| |j	d
||d}W d    n1 sAw   Y  ||j
|jk }t|| t| |j	d||d}W d    n1 slw   Y  ||j
|jk }t|| d S )Nrp  rq  r&   rq   rr  r   rs  rt  r   za == br   za != b)r   rb   re   rs   rt   r  RuntimeWarningr7   assert_produces_warningr6   ry   rz   r8   )r*   r   r   using_infer_stringr+   warningr   r   r   r   r   test_object_array_eq_ne  s$   z1TestDataFrameQueryStrings.test_object_array_eq_nec           	      C   s   t | dd tddD d }tdddd	}td
ddd	}ttddd|||d}||jdk }|j	d||d}t
|| d S )Nc                 S   s&   g | ]}d D ]
}d| d| qqS ))loadexitzpage rd  r   )r   rM   actr   r   r   r   $  s
    zLTestDataFrameQueryStrings.test_query_with_nested_strings.<locals>.<listcomp>r%   rN   r&   z2014-01-01 0:00:0130srO  )r;  r   z2014-02-01 1:00:01   )idevent	timestampz"page 1 load"z'"page 1 load"' in eventr   )r!   ra   r   r   re   r   repeatappendr  r6   r7   r8   )	r*   r   r   eventsstamps1stamps2r+   ro   r   r   r   r   test_query_with_nested_strings"  s"   z8TestDataFrameQueryStrings.test_query_with_nested_stringsc                 C   sL   t | tg dg dd}|jd||d}||jdk }t|| d S )N)ry   rz   test & testr$   r(  za == "test & test"r   r  r+  )r*   r   r   r+   r   r   r   r   r   (test_query_with_nested_special_character5  s
   zBTestDataFrameQueryStrings.test_query_with_nested_special_characterzop, funcr   r   r   r  c                 C   s   t tjdtdd}t t|j}t||d}|r%|dkr%t	nd }	t
|	 |jd| d||d}
W d    n1 sCw   Y  |||jd	 }t
|
| d S )
Nr&   abcder  )Xr   r   zX z "d"r   r   )r   re   rs   rt   r   rb   r   r   r   rv  r7   rw  r6   r  r8   )r*   r   r   rk   funcrx  ry   rz   r+   ry  r   ro   r   r   r   test_query_lex_compare_strings<  s   z8TestDataFrameQueryStrings.test_query_lex_compare_stringsc                 C   sd   d}t jdjddt|fdt}t||d}|jd||d}||j	|j
@  }t|| d S )N)bidbidsizeaskasksizer&   r%   r   r`   z	bid & askr   )re   rs   rt   r  r  r   boolr   r6   r  r  r7   r8   )r*   r   r   r^   datar+   r   ro   r   r   r   "test_query_single_element_booleansR  s   $z<TestDataFrameQueryStrings.test_query_single_element_booleansc                 C   sP   t | tg dg dd}||jdk }d}|jd||d}t|| d S )N)BUD USr  IBM USr  )gl[@gGzn[@gf@g33333f@)SymbolPricer  zSymbol == @symbr   )r!   r   r  r6   r7   r8   )r*   r   r   r+   esymbr  r   r   r   !test_query_string_scalar_variableZ  s   z;TestDataFrameQueryStrings.test_query_string_scalar_variablein_list)Nasdfghjk)r  Nr  )r  r  N)NNr  )r  NN)NNNc           
      C   s   d}d}dd t |D }td|idd}|jd|_td|idd}|jd	||d
}||d dk }|jd||d
}	t|| t|| t||	 t||	 d S )Nr   r
   c                 S   s   i | ]\}}|d kr||qS )r  r   )r   r  valuer   r   r   
<dictcomp>v  s    zMTestDataFrameQueryStrings.test_query_string_null_elements.<locals>.<dictcomp>ry   stringdtypeint64za == 'asdf'r   r  za <= 'asdf')	enumerater   r]   r   r6   r7   r8   )
r*   r  r   r   ro   df_expectedr+   r   r   res3r   r   r   test_query_string_null_elementsg  s   z9TestDataFrameQueryStrings.test_query_string_null_elementsN)rH   rI   rJ   rn  ro  ru  rz  r  r  r   r   r   rE  ltgtleger  r  r  r  r   r   r   r   r^    s:    )#
	r^  c                   @   sD   e Zd Zejdd Zdd Zdd Zej	dg dd	d
 Z
dS )TestDataFrameEvalWithFramec                 C   s   t tjddtddS )Nr&   r   r  r`   )r   re   rs   rt   ru   rb   r)   r   r   r   frame  s   z TestDataFrameEvalWithFrame.framec                 C   s,   |j d||d}|j|j }t|| d S )Nr   r   r9   ry   rz   r7   r:   r*   r  r   r   r   r#  r   r   r   test_simple_expr  s   z+TestDataFrameEvalWithFrame.test_simple_exprc                 C   s6   |j d||d}|j|jdk  |j }t|| d S )Nza[a < 1] + br   r%   r  r  r   r   r   test_bool_arith_expr  s   z/TestDataFrameEvalWithFrame.test_bool_arith_exprrk   )rO   rR   rU   rX   c                 C   sf   t ddgddgd}d}tjt|d |jd| d	||d
 W d    d S 1 s,w   Y  d S )Nr%   r&   r   r   r(  z:unsupported operand type\(s\) for .+: '.+' and '.+'|CannotrB   r3  z br   )r   r   rD   r  r9   )r*   r   r   rk   r+   rF   r   r   r   %test_invalid_type_for_operator_raises  s
   "z@TestDataFrameEvalWithFrame.test_invalid_type_for_operator_raisesN)rH   rI   rJ   r   rK   r  r  r  r   r   r  r   r   r   r   r    s    
r  c                   @   s0  e Zd Zej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)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zej d5g d6d7d8 Z!ej d9d:d;gej d5g d6d<d= Z"d>d? Z#d@S )A!TestDataFrameQueryBacktickQuotingc                 c   s    t i dg ddg ddg ddg dd	g d
dg ddg ddg ddg ddg ddg ddg ddg ddg ddg ddg d d!g d"g d#g d$g d%g d&g d'd(V  d)S )*z
        Yields a dataframe with strings that may or may not need escaping
        by backticks. The last two columns cannot be escaped by backticks
        and should raise a ValueError.
        r#   r$   B B)r'   r&   r%   C CrN  C  C)r  rN   r'   C_C)   rs  r   D_D D)   r%   r.  E.E)rO  r'   rq   F-F)r  r%   r   1e1)r&   rN   r  def)r   r  r&   A (x))rN   r%   r'   zB(x))r%   r%   rq   zB (x))r&   r  rN        &^ :!€$?(} >    <++*''  )r&   rq   rO  r   )r   r  r%    A)rN   r  rs    r%   r&   r%   )rO  r'   r%   )rs  r%   r  )r  r  rO  )r&   rN   rq   )rq   r  rs  )zit'szthat'su   ☺zfoo#barr%   Nr(   r)   r   r   r   r+     sX   	

z$TestDataFrameQueryBacktickQuoting.dfc                 C   s*   | d}|d|d k  }t|| d S )Nz	1 < `B B`r%   r  r6   r7   r8   r*   r+   r   r#  r   r   r   #test_single_backtick_variable_query     
zETestDataFrameQueryBacktickQuoting.test_single_backtick_variable_queryc                 C   s6   | d}|d|d k d|d k @  }t|| d S )Nz1 < `B B` and 4 < `C C`r%   r  rN   r  r  r  r   r   r   !test_two_backtick_variables_query  s   
zCTestDataFrameQueryBacktickQuoting.test_two_backtick_variables_queryc                 C   *   | d}|d |d  }t|| d S )Nz	A + `B B`r#   r  r9   r7   r:   r  r   r   r   "test_single_backtick_variable_expr  r  zDTestDataFrameQueryBacktickQuoting.test_single_backtick_variable_exprc                 C   r  )Nz`B B` + `C C`r  r  r  r  r   r   r    test_two_backtick_variables_expr  r  zBTestDataFrameQueryBacktickQuoting.test_two_backtick_variables_exprc                 C   r  )Nz	`C_C` + Ar  r#   r  r  r   r   r    test_already_underscore_variable  r  zBTestDataFrameQueryBacktickQuoting.test_already_underscore_variablec                 C   r  )NzC_C + `C C`r  r  r  r  r   r   r   test_same_name_but_underscores  r  z@TestDataFrameQueryBacktickQuoting.test_same_name_but_underscoresc                 C   r  )NzA + `D_D D`r#   r  r  r  r   r   r   !test_mixed_underscores_and_spaces  r  zCTestDataFrameQueryBacktickQuoting.test_mixed_underscores_and_spacesc                 C   r  )Nz	A + `C_C`r#   r  r  r  r   r   r   'test_backtick_quote_name_with_no_spaces  r  zITestDataFrameQueryBacktickQuoting.test_backtick_quote_name_with_no_spacesc                 C   s2   | d}|d |d  |d  }t|| d S )Nz`E.E` + `F-F` - Ar  r  r#   r  r  r   r   r   test_special_characters  s   
z9TestDataFrameQueryBacktickQuoting.test_special_charactersc                 C   r  )Nz	A + `1e1`r#   r  r  r  r   r   r   test_start_with_digit  r  z7TestDataFrameQueryBacktickQuoting.test_start_with_digitc                 C   r  )Nz	A + `def`r#   r  r  r  r   r   r   test_keyword  r  z.TestDataFrameQueryBacktickQuoting.test_keywordc                 C   *   | d}||d dk }t|| d S )Nz`A` > 2r#   r&   r  r  r   r   r   test_unneeded_quoting  r  z7TestDataFrameQueryBacktickQuoting.test_unneeded_quotingc                 C   r  )Nz`A (x)` > 2r  r&   r  r  r   r   r   test_parenthesis  r  z2TestDataFrameQueryBacktickQuoting.test_parenthesisc                 C   r  )Nz`` > 5r   rq   r  r  r   r   r   test_empty_string  r  z3TestDataFrameQueryBacktickQuoting.test_empty_stringc                 C   r  )Nz
`C  C` > 5r  rq   r  r  r   r   r   test_multiple_spaces  r  z6TestDataFrameQueryBacktickQuoting.test_multiple_spacesc                 C   r  )Nz` A` + `  `r  r  r  r  r   r   r   test_start_with_spaces  r  z8TestDataFrameQueryBacktickQuoting.test_start_with_spacesc                 C   r  )Nu"   `  &^ :!€$?(} >    <++*''  ` > 4r  rN   r  r  r   r   r   test_lots_of_operators_string  r  z?TestDataFrameQueryBacktickQuoting.test_lots_of_operators_stringc                 C   B   d}t jt|d |d W d    d S 1 sw   Y  d S )Nz(module 'pandas' has no attribute 'thing'rB   z	@pd.thing)r   rD   AttributeErrorr9   )r*   r+   messager   r   r   test_missing_attribute     "z8TestDataFrameQueryBacktickQuoting.test_missing_attributec                 C   r  )N6(Could not convert ).*( to a valid Python identifier.)rB   z`it's` > `that's`r   rD   r	  r6   r   r   r   r   test_failing_quote  r  z4TestDataFrameQueryBacktickQuoting.test_failing_quotec                 C   r  )Nr  rB   u	   `☺` > 4r  r   r   r   r   $test_failing_character_outside_range   r  zFTestDataFrameQueryBacktickQuoting.test_failing_character_outside_rangec                 C   r  )NzFailed to parse backticksrB   z`foo#bar` > 4r  r   r   r   r   test_failing_hashtag%  r  z6TestDataFrameQueryBacktickQuoting.test_failing_hashtagc                 C   s   dd }|g}| d tjtdd | d W d   n1 s#w   Y  tjtdd | d W d   dS 1 s@w   Y  dS )	z
        Only attributes and variables ('named functions') can be called.
        .__call__() is not an allowed attribute because that would allow
        calling anything.
        https://github.com/pandas-dev/pandas/pull/32460
        c                  W   r   r/   r   )r   r   r   r   r  2  s   zNTestDataFrameQueryBacktickQuoting.test_call_non_named_expression.<locals>.funcz@func()z"Only named functions are supportedrB   z@funcs[0]()Nz@funcs[0].__call__())r9   r   rD   r  )r*   r+   r  funcsr   r   r   test_call_non_named_expression*  s   
"z@TestDataFrameQueryBacktickQuoting.test_call_non_named_expressionc                 C   s   t ddgddggddg|d}trtnd }t| |d}W d    n1 s*w   Y  t g d	g d
gg d|d}t|| d S )Nr%   r&   r'   rN   ry   rz   r^   r  z	c = b - ar  )r'   rN   r%   r  r   r	   rv  r7   rw  r9   r8   )r*   any_numeric_ea_and_arrow_dtyper+   ry  r<   ro   r   r   r   test_ea_dtypes?  s   z0TestDataFrameQueryBacktickQuoting.test_ea_dtypesc                 C   s   t ddgddggddgdd}trtnd }t| |d	}W d    n1 s*w   Y  t g d
g dgg ddd}t|| d S )Nr%   r&   r'   rN   ry   rz   Float64r  z	c = b - 1r  )r'   rN   r'   r  r  )r*   r+   ry  r<   ro   r   r   r   test_ea_dtypes_and_scalarN  s   z;TestDataFrameQueryBacktickQuoting.test_ea_dtypes_and_scalarc                 C   sr   t ddgddggddg|d}|d}t tddg|d	tddg|d	tddg|d
 jd	d}t|| d S )Nr%   r&   r'   rN   ry   rz   r  z	c = 2 - 1r  r   r  )r   r9   r   r  r7   r8   )r*   r  r+   r<   ro   r   r   r   #test_ea_dtypes_and_scalar_operationY  s   
zETestDataFrameQueryBacktickQuoting.test_ea_dtypes_and_scalar_operationr  )r  Int64int64[pyarrow]c                 C   s   |dkr	t d tdtddg|di}dh}|dkr tr tnd }t| |d}W d    n1 s7w   Y  tdtdg|dgd	i}t	|| d S )
Nr  pyarrowry   r%   r&   r  r  z	a in @refr  r]   )
r   importorskipr   r   r	   rv  r7   rw  r6   r8   )r*   r  r+   refry  r<   ro   r   r   r   test_query_ea_dtypesh  s   
z6TestDataFrameQueryBacktickQuoting.test_query_ea_dtypesr   r
   r   c                 C   s   |dkrt nd }|dkrtstd |dkrtd ttg dddtg d|dd	}t| |j	d
|d}W d    n1 sFw   Y  ttddgdddgdtddg|ddgdd	}t
|| d S )Nr   znumexpr not installedr  r  )r%   r%   r&   r  r  )r%   r&   r&   r{   zA == Br>   r%   r&   r   r  )rv  r	   r   r    r  r   r   r7   rw  r6   r8   )r*   r  r   ry  r+   r<   ro   r   r   r   !test_query_ea_equality_comparisonu  s"   

 zCTestDataFrameQueryBacktickQuoting.test_query_ea_equality_comparisonc                 C   sT   t jd}tdt jd d gdditd}|d}tdg itd}t|| d S )NUTCry   T)utcr  za > @now)	r   	Timestampnowr   to_datetimer   r6   r7   r8   )r*   r  r+   r<   ro   r   r   r   test_all_nat_in_object  s
   
z8TestDataFrameQueryBacktickQuoting.test_all_nat_in_objectN)$rH   rI   rJ   r   rK   r+   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r   r  r  r  r   r   r   r   r    sD    
!
r  )'rE  numpyre   r   pandas.errorsr   r   pandas.util._test_decoratorsutil_test_decoratorstdr   r   r   r   r   r   r   pandas._testing_testingr7   pandas.core.computation.checkr	   rK   r   r   
skip_if_nor   r!   r"   rL   r   r   rH  rU  r]  r^  r  r  r   r   r   r   <module>   sB    

3c K  {x ^