o
    ҷh/                     @   sh  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
j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
jG dd dZ e
j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   U/var/www/html/venv/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t|   d S d S )Nr   zcannot evaluate with parser )pytestskiprepr)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   	expected22   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/   r1   resultr   r   r   test_query_default6   s   

zTestCompat.test_query_defaultc                 C   s<   |j dd d}t|| |jdd d}tj||dd d S )Nr2   r   r3   Fr4   r6   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 )Nr2   r
   r?   r3   Fr4   r6   r<   r   r   r   test_query_pythonD   rA   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 )	Nr2   r   r?   r3   Fr4   z~'numexpr' is not installed or an unsupported version. Cannot use engine='numexpr' for query/eval if 'numexpr' is not installedmatch)	r	   r7   r8   r9   r:   r;   r   raisesImportError)r+   r,   r/   r1   r=   msgr   r   r   test_query_numexprJ   s   "zTestCompat.test_query_numexprN)__name__
__module____qualname__r   fixturer,   r/   r1   r>   r@   rB   rH   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 )rP   rV   )rS   rY   )r   rangelistilocmeannptilevaluesreshaper:   r8   r9   getattr)
r+   op_stroproprN   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   rf   randomdefault_rngstandard_normalnanrd   isnanr8   r9   )r+   r,   rp   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br{   r$   Bz%expr must be a string to be evaluatedrC   c                 S   s
   | j dkS )Nr{   )r}   r   r   r   r   r      s   
 z6TestDataFrameEval.test_query_non_str.<locals>.<lambda>o   r   r   rE   
ValueErrorr7   r+   r,   rG   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 stringrC    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'   abra   rz   r&   r{   a + b	resolvers)r   rf   rt   ru   rv   rc   r:   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   ra   cz	a + b * cr   rz   r{   )	r   rf   rt   ru   rv   rc   r:   r8   r;   )r+   r,   r   r=   rp   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   r:   r8   r9   )r+   r,   resrp   r   r   r   test_eval_object_dtype_binop   s   
z.TestDataFrameEval.test_eval_object_dtype_binopN)rI   rJ   rK   r   markparametrizerq   ry   r   r   r   r   r   r   r   r   r   rM   \   s    		
rM   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"   rf   rt   ru   choicer   from_arraysr   rv   r   r^   get_level_valuesrh   r7   r8   r9   isinr+   r   r   rz   r{   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"   rf   rt   ru   r   aranger   r   r   r   rv   r7   r   r^   r   rh   r8   r9   )
r+   r   r   rz   r{   r^   r,   r   r   r   r   r   r   *test_query_with_partially_named_multiindexJ  s4   

zKTestDataFrameQueryWithMultiIndex.test_query_with_partially_named_multiindexc                 C   s   t jdddddgd}| }dd }|j }|j|||jd||jd|d	}| D ]$\}}t|tr@|	|| s?J q-t|t
rNt |||  q-td
d S )Nr   r(   r'   spamr   )r_idx_nlevelsr_idx_namesc                 S   s   |  |}| }| |_|S r   )r   	to_seriesr^   )milevellevel_valuessr   r   r   r   o  s   
z]TestDataFrameQueryWithMultiIndex.test_query_multiindex_get_index_resolvers.<locals>.to_series)r^   r_   r   r   C0z object must be a Series or Index)r8   makeCustomDataframe_get_index_resolversr_   r   r^   items
isinstancer   is_r   r;   AssertionError)r+   r,   r   r   
col_seriesrp   kvr   r   r   )test_query_multiindex_get_index_resolversi  s&   




zJTestDataFrameQueryWithMultiIndex.test_query_multiindex_get_index_resolversN)rI   rJ   rK   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'   rr   r(   1/1/2012rr   periodsdates11/1/2013dates21/1/2014dates3z"@df.dates1 < 20130101 < @df.dates3r   r   20130101)r"   r   rf   rt   ru   rv   r   r7   r   r   r8   r9   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   rr   r   r   r   r   r   r   dates1 < 20130101 < dates3r   r   r   rf   rt   ru   rv   r   r7   r   r   r8   r9   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   rf   rt   ru   rv   r   r   NaTlocr7   r   r   r8   r9   r+   r   r   rN   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   rf   rt   ru   rv   r   	set_indexr7   r^   r   r8   r9   r+   r   r   rN   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   rf   rt   ru   rv   astypeobjectr   r   r   rd   r   r7   r^   r   r8   r9   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   rf   rt   ru   r   r7   r^   r   r   r8   r9   )	r+   r   r   rN   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)<><=>=rC   zdates z nondate)r   r   rf   r   r7   lenr8   r9   r   rE   	TypeError)r+   r   r   rN   r,   r=   rG   rl   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(      rO      )irP   rzinvalid syntaxrC   zi - +r   )r   rb   r   rE   SyntaxErrorr7   )r+   r   r   r,   rG   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   ra   )r&   r'   za > br   z@a > b!local variable 'c' is not definedrC   z@a > b > @czname 'c' is not definedz
@a > b > c)r"   r   rf   rt   ru   rv   rc   r7   rz   r{   r8   r9   r   rE   r   )r+   r   r   r,   rz   r{   r   rp   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   abcra   zname 'sin' is not definedrC   sin > 5r   )
r   rf   rt   ru   integersrc   r   rE   r   r7   )r+   r   r   rN   rn   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  ra   sinzVariables in expression.+rC   r  r   )r   rf   rt   ru   r  rc   r^   r   r   rE   r   r7   )r+   r   r   rN   rn   r,   rG   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   r(   rz   r{   r   ra   a < br   za + b > b * c)r   rf   rt   ru   rv   r8   r9   r7   rz   r{   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   rr   zblob < b)r   rf   rt   ru   r  r   rb   r7   r^   rz   r{   r8   r9   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 < 5rr   )r   rf   rt   ru   r  rb   r7   r^   r{   r8   r9   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   rf   rt   ru   rv   r7   r8   r9   r   r:   )r+   r   r   r,   df2rp   r=   r   r   r   test_nested_scopeK  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 definedrC   zdf > 0r   )	r   rf   rt   ru   rv   r   rE   r   r7   r  r   r   r   *test_nested_raises_on_local_self_referenceb  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   
abcdefghijra   r&   za < @br   r  )r"   r   rf   rt   ru   rv   rc   rz   r7   r8   r9   r{   )r+   r   r   r,   r{   expectr=   r   r   r   test_local_syntaxi  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   ra   z!a < b < c and a not in b not in cr   )r"   rc   r   rf   rt   ru   rv   r   r7   rz   r{   r   r   r8   r9   )r+   r   r   colsr,   r   r   r   r   r   r   test_chained_cmp_and_iny  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   rz   r  r   r{   rz   r{   r&   z
b - 1 in ar   z@b - 1 in a)r"   r   rf   rt   ru   r  r   r   r{   r   r7   r8   r9   )r+   r   r   rz   r{   r,   rp   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&   rz   )rz   rz   r{   r{   @cr)  z	a == "@c"r   r)  r"   r   r7   rz   r8   r9   )r+   r   r   r   r,   r=   rp   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   ra   r
  rC   za == @cr   )r   r   r"   r   rf   rt   ru   rc   r   rE   r   r7   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^   r{   r^   z	index > 5r   rr   zilevel_0 > 5r'  rz   za > 5)rf   r_r   rt   ru   rv   r   r^   r   r7   r8   r9   r   rz   )r+   r   r   rN   rz   r,   r=   rp   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   rf   rt   ru   infr   rz   r7   r8   r9   )
r+   rl   fr   r   rN   r,   qrp   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   r7   r8   r9   reset_index)r+   tz_aware_fixturer;  df_indexrp   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'  rz   intr   za.astype('int') == 0r   r   za.notnull())r   rf   rt   ru   r   r7   r8   r9   whererw   rv   notnull)r+   r   r   rN   r,   rp   r=   r   r   r   test_method_calls_in_query  s*   
z:TestDataFrameQueryNumExprPandas.test_method_calls_in_queryN)%rI   rJ   rK   r   rL   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   operatoreqner6  r?  rC  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   rr   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   rI  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   rJ  r   r   r   r   r   r   r   r   0  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 implementedrC   r   r   )r   rf   rt   ru   rv   r   r   r   r   r   r   rE   NotImplementedErrorr7   )r+   r   r   rN   r,   r   rG   r   r   r   r   A  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 parserrC   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   r:   r   rf   rt   ru   rv   r   rE   r  r7   r   r8   r9   )	r+   r   r   r   r=   r,   r  rG   rp   r   r   r   r  M  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%   rO   rr      )minmaxzMVariables in expression \"\(min\) == \(1\)\" overlap with builtins: \('min'\)rC   zmin == 1zMVariables in expression \"\(max\) == \(1\)\" overlap with builtins: \('max'\)zmax == 1)r   r   rE   r   r7   )r+   r,   regex_to_matchr   r   r   +test_query_numexpr_with_min_and_max_columnsh  s   "zKTestDataFrameQueryNumExprPython.test_query_numexpr_with_min_and_max_columnsN)rI   rJ   rK   r   rL   r   r   r   r   r   r   r   r  rR  r   r   r   r   rG     s    

rG  c                   @   0   e Zd Zejdd Zejdd Zdd ZdS )TestDataFrameQueryPythonPandasc                 C   r   rH  r   r*   r   r   r   r   z  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  ra   r  rr   r  r   r   rf   rt   ru   r  rc   r^   r   r7   r8   r9   r+   r   r   rN   rn   r,   rp   r=   r   r   r   r       z1TestDataFrameQueryPythonPandas.test_query_builtinNrI   rJ   rK   r   rL   r   r   r  r   r   r   r   rT  y      

rT  c                   @   rS  )TestDataFrameQueryPythonPythonc                 C   r   rH  r   r*   r   r   r   r     r   z%TestDataFrameQueryPythonPython.enginec                 C   r   rH  r   r*   r   r   r   r     r   z%TestDataFrameQueryPythonPython.parserc                 C   rU  rV  rW  rX  r   r   r   r    rY  z1TestDataFrameQueryPythonPython.test_query_builtinNrZ  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&   r{   ra   
aabbccddeestringsrz   r   z"a"r`   r0  r1  $'(Not)?In' nodes are not implemented rC   )r   r   
local_dictz"a" == stringsr   zstrings == "a"zstrings != "a"z"a" != strings)r   rf   rt   ru   rv   r   rc   r`  zipr   rE   rL  r7   r8   r9   r   r+   r   r   r,   r"  collstlhsrhsrE  rF  opsrG   rl   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^  r{   ra   r_  r`  rz   r   z
["a", "b"]r`   ra  rb  rc  rC   r   zstrings == ["a", "b"]z["a", "b"] == stringszstrings != ["a", "b"]z["a", "b"] != strings)r   rf   rt   ru   rv   r   rc   r`  r   re  r   rE   rL  r7   r8   r9   rf  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'   rr      r   	   rz   r{   r   r   r   za in br   za in b and c < drb  rC   rK  )r   rc   rf   rt   ru   r  r7   rz   r   r{   r8   r9   r   r   r   rE   rL  )r+   r   r   r,   r   r   rG   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}|jd	||d
}||j|jk }t	
|| |jd||d
}||j|jk }t	
|| d S )Nro  rp  r'   rr   rq  r   rr  rs  za == br   za != b)r   rc   rf   rt   ru   r  r7   rz   r{   r8   r9   )r+   r   r   r,   r   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 rc  r   ).0rN   actr   r   r   
<listcomp>  s
    zLTestDataFrameQueryStrings.test_query_with_nested_strings.<locals>.<listcomp>r&   rO   r'   z2014-01-01 0:00:0130srN  )r:  r   z2014-02-01 1:00:01   )idevent	timestampz"page 1 load"z'"page 1 load"' in eventr   )r"   rb   r   r   rf   r   repeatappendr~  r7   r8   r9   )	r+   r   r   eventsstamps1stamps2r,   rp   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)rz   r{   test & testr%   r'  za == "test & test"r   r  r*  )r+   r   r   r,   r   r   r   r   r   (test_query_with_nested_special_character.  s
   zBTestDataFrameQueryStrings.test_query_with_nested_special_characterzop, funcr   r   r   r   c           
      C   sr   t tjdtdd}t t|j}t||d}|j	d| d||d}|||j
d }	t||	 d S )	Nr'   abcder	  )Xr   zX z "d"r   r   )r   rf   rt   ru   r   rc   r   r   r   r7   r  r8   r9   )
r+   r   r   rl   funcrz   r{   r,   r   rp   r   r   r   test_query_lex_compare_strings5  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   ra   z	bid & askr   )rf   rt   ru   r  r   r   boolr   r7   r  r  r8   r9   )r+   r   r   r_   datar,   r   rp   r   r   r   "test_query_single_element_booleansG  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  r7   r8   r9   )r+   r   r   r,   esymbr  r   r   r   !test_query_string_scalar_variableO  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   )rx  r  valuer   r   r   
<dictcomp>k  s    zMTestDataFrameQueryStrings.test_query_string_null_elements.<locals>.<dictcomp>rz   stringdtypeint64za == 'asdf'r   r  za <= 'asdf')	enumerater   r^   r   r7   r8   r9   )
r+   r  r   r   rp   df_expectedr,   r   r   res3r   r   r   test_query_string_null_elements\  s   z9TestDataFrameQueryStrings.test_query_string_null_elementsN)rI   rJ   rK   rm  rn  rt  ru  r  r  r   r   r   rD  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  ra   )r   rf   rt   ru   rv   rc   r*   r   r   r   framez  s   z TestDataFrameEvalWithFrame.framec                 C   s,   |j d||d}|j|j }t|| d S )Nr   r   r:   rz   r{   r8   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_exprrl   )rP   rS   rV   rY   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'  z3unsupported operand type\(s\) for .+: '.+' and '.+'rC   r2  z br   )r   r   rE   r  r:   )r+   r   r   rl   r,   rG   r   r   r   %test_invalid_type_for_operator_raises  s
   "z@TestDataFrameEvalWithFrame.test_invalid_type_for_operator_raisesN)rI   rJ   rK   r   rL   r  r  r  r   r   r  r   r   r   r   r  y  s    
r  c                   @   s(  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>S )?!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 CrM  C  C)r|  rO   r(   C_C)   rr  r   D_D D)   r&   r-  E.E)rN  r(   rr   F-F)r  r&   r   1e1)r'   rO   r  def)r   r  r'   A (x))rO   r&   r(   zB(x))r&   r&   rr   zB (x))r'   r|  rO        &^ :!€$?(} >    <++*''  )r'   rr   rN  r   )r   r  r&    A)rO   r|  rr    r&   r'   r&   )rN  r(   r&   )rr  r&   r  )r  r|  rN  )r'   rO   rr   )rr   r|  rr  )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  r7   r8   r9   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  rO   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  r:   r8   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   rr   r  r  r   r   r   test_empty_string  r  z3TestDataFrameQueryBacktickQuoting.test_empty_stringc                 C   r  )Nz
`C  C` > 5r  rr   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  rO   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'rC   z	@pd.thing)r   rE   AttributeErrorr:   )r+   r,   messager   r   r   test_missing_attribute     "z8TestDataFrameQueryBacktickQuoting.test_missing_attributec                 C   r  )N6(Could not convert ).*( to a valid Python identifier.)rC   z`it's` > `that's`r   rE   r  r7   r   r   r   r   test_failing_quote  r  z4TestDataFrameQueryBacktickQuoting.test_failing_quotec                 C   r  )Nr  rC   u	   `☺` > 4r  r   r   r   r   $test_failing_character_outside_range  r  zFTestDataFrameQueryBacktickQuoting.test_failing_character_outside_rangec                 C   r  )NzFailed to parse backticksrC   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   r0   r   )_r   r   r   r  '  s   zNTestDataFrameQueryBacktickQuoting.test_call_non_named_expression.<locals>.funcz@func()z"Only named functions are supportedrC   z@funcs[0]()Nz@funcs[0].__call__())r:   r   rE   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(   rO   rz   r{   r_   r  z	c = b - ar  )r(   rO   r&   r  r   r	   RuntimeWarningr8   assert_produces_warningr:   r9   )r+   any_numeric_ea_and_arrow_dtyper,   warningr=   rp   r   r   r   test_ea_dtypes4  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(   rO   rz   r{   Float64r  z	c = b - 1r  )r(   rO   r(   r  r  )r+   r,   r  r=   rp   r   r   r   test_ea_dtypes_and_scalarC  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(   rO   rz   r{   r  z	c = 2 - 1r  r   r  )r   r:   r   r  r8   r9   )r+   r  r,   r=   rp   r   r   r   #test_ea_dtypes_and_scalar_operationN  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  pyarrowrz   r&   r'   r  r  z	a in @refr  r^   )
r   importorskipr   r   r	   r  r8   r  r7   r9   )r+   r  r,   refr  r=   rp   r   r   r   test_query_ea_dtypes]  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  )r  r	   r   r    r  r   r   r8   r  r7   r9   )r+   r  r   r  r,   r=   rp   r   r   r   !test_query_ea_equality_comparisonj  s"   

 zCTestDataFrameQueryBacktickQuoting.test_query_ea_equality_comparisonN)#rI   rJ   rK   r   rL   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    sB    
!
r  )'rD  numpyrf   r   pandas.errorsr   r   pandas.util._test_decoratorsutil_test_decoratorstdr   r   r   r   r   r   r   pandas._testing_testingr8   pandas.core.computation.checkr	   rL   r   r   skip_if_no_ner   r"   r#   rM   r   r   rG  rT  r\  r]  r  r  r   r   r   r   <module>   s@    

3c H  {x W