o
    d`                     @   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   V/app/.heroku/python/lib/python3.10/site-packages/pandas/tests/frame/test_query_eval.py<lambda>       r   )paramsZidsc                 C      | j S r   paramrequestr   r   r   parser   s   r   numexpr)Z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Zcheck_namesquerytmassert_frame_equalevalassert_series_equalr)   r*   r-   r/   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 )Nr0   r   r1   Fr2   r3   r9   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 )Nr0   r
   r<   r1   Fr2   r3   r9   r   r   r   test_query_pythonD   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 )	Nr0   r   r<   r1   Fr2   z~'numexpr' is not installed or an unsupported version. Cannot use engine='numexpr' for query/eval if 'numexpr' is not installedmatch)	r	   r4   r5   r6   r7   r8   r   raisesImportError)r)   r*   r-   r/   r:   msgr   r   r   test_query_numexprJ   s   "zTestCompat.test_query_numexprN)__name__
__module____qualname__r   fixturer*   r-   r/   r;   r=   r?   rE   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 )rM   rS   )rP   rV   )r   rangelistilocmeannpZtilevaluesZreshaper7   r5   r6   getattr)
r)   Zop_stropZroprK   r*   mbaseexpectedr:   r   r   r   test_ops^   s    
zTestDataFrameEval.test_opsc                 C   sd   t dtjdi}tj|jdd< dt|jdd  }dt| jdd }t|| d S )Nr"   ia  r      r$      )	r   rc   randomrandnnanra   isnanr5   r6   )r)   r*   ri   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brs   r"   Bz%expr must be a string to be evaluatedr@   c                 S   s
   | j dkS )Nrs   )ru   r   r   r   r   r      s   
 z6TestDataFrameEval.test_query_non_str.<locals>.<lambda>o   r   r   rB   
ValueErrorr4   r)   r*   rD   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 stringr@    rw   ry   r   r   r   test_query_empty_string   s
   "z)TestDataFrameEval.test_query_empty_stringc                 C   sz   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 )
N
   r%   abr^   rr   r$   rs   a + b	resolvers)r   rc   rm   rn   r`   r7   pd)r)   r*   dict1Zdict2r   r   r   test_eval_resolvers_as_list   s
   &*z-TestDataFrameEval.test_eval_resolvers_as_listc                 C   sZ   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   rr   rs   )r   rc   rm   rn   r`   r7   r5   r8   )r)   r*   r   r:   ri   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   r7   r5   r6   )r)   r*   resri   r   r   r   test_eval_object_dtype_binop   s   
z.TestDataFrameEval.test_eval_object_dtype_binopN)rF   rG   rH   r   markparametrizerj   rq   rz   r|   r   r   r   r   r   r   r   rJ   \   s    		rJ   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
 TestDataFrameQueryWithMultiIndexc                 C   s$  t | tjjddgdd}tj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 )Nredgreenr}   sizeeggshamcolorZ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    rc   rm   choicer   from_arraysr   rn   r   r[   get_level_valuesrd   r4   r5   r6   isinr)   r   r   rr   rs   r[   r*   indZres1Zres2expr   r   r    test_query_with_named_multiindex   sL   zATestDataFrameQueryWithMultiIndex.test_query_with_named_multiindexc                 C   s  t | tjjddgdd}tj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   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   sT  t | tj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   Zratingr%   r   zrating == 1r   r   r$   zrating != 1r   r   r   )r    rc   rm   r   aranger   r   r   r   rn   r4   r   r[   r   rd   r5   r6   )
r)   r   r   rr   rs   r[   r*   r   r   r   r   r   r   *test_query_with_partially_named_multiindexF  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   )Zr_idx_nlevelsZr_idx_namesc                 S   s   |  |}| }| |_|S r   )r   	to_seriesr[   )milevelZlevel_valuessr   r   r   r   k  s   
z]TestDataFrameQueryWithMultiIndex.test_query_multiindex_get_index_resolvers.<locals>.to_series)r[   r\   r   r   ZC0z object must be a Series or Index)r5   ZmakeCustomDataframeZ_get_index_resolversr\   r   r[   items
isinstancer   is_r   r8   AssertionError)r)   r*   r   r   Z
col_seriesri   kvr   r   r   )test_query_multiindex_get_index_resolverse  s&   




zJTestDataFrameQueryWithMultiIndex.test_query_multiindex_get_index_resolversN)rF   rG   rH   r   r   r   r   r   r   r   r   r      s
    2Xr   c                   @   s
  e Z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+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   s   d| _ d| _d S )Nr   r   r   r   clsr   r   r   setup_class  s   
z+TestDataFrameQueryNumExprPandas.setup_classc                 C   s   | ` | `d S r   r   r   r   r   r   teardown_class  s   z.TestDataFrameQueryNumExprPandas.teardown_classc                 C   s   | j | j}}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 )Nrk   r&   1/1/2012periodsdates11/1/2013dates21/1/2014dates3z"@df.dates1 < 20130101 < @df.dates3r   20130101)r   r   r    r   rc   rm   rn   r   r4   r   r   r5   r6   r)   r   r   r*   r   expecr   r   r   %test_date_query_with_attribute_access  s   zETestDataFrameQueryNumExprPandas.test_date_query_with_attribute_accessc                 C      | j | j}}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 )Nrk   r&   r   r   r   r   r   r   r   dates1 < 20130101 < dates3r   r   r   r   r   rc   rm   rn   r   r4   r   r   r5   r6   r   r   r   r   #test_date_query_no_attribute_access  s   zCTestDataFrameQueryNumExprPandas.test_date_query_no_attribute_accessc                 C      | j | j}}d}ttj|d}td|d|d< td|d|d< td|d|d	< tj|j	tj
|d
kdf< tj|j	tj
|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   r   rc   rm   rn   r   r   NaTlocrandr4   r   r   r5   r6   r)   r   r   rK   r*   r   r   r   r   r   test_date_query_with_NaT  s   z8TestDataFrameQueryNumExprPandas.test_date_query_with_NaTc                 C      | j | j}}d}ttj|d}td|d|d< td|d|d< |jdddd	}|d u s0J |jd
||d}||j	dk d|j
k @  }t|| d S )Nr}   r&   r   r   r   r   r   TZinplaceZdropindex < 20130101 < dates3r   r   r   r   r   rc   rm   rn   r   	set_indexr4   r[   r   r5   r6   r)   r   r   rK   r*   return_valuer   r   r   r   r   test_date_index_query  s   z5TestDataFrameQueryNumExprPandas.test_date_index_queryc                 C      | j | j}}d}ttj|ddti}td|d|d< td|d|d< t	j
|j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   r   r   Tr   r   r   r   r   r   r   rc   rm   rn   astypeobjectr   r   r   ra   r   r4   r[   r   r5   r6   r   r   r   r   test_date_index_query_with_NaT  s   z>TestDataFrameQueryNumExprPandas.test_date_index_query_with_NaTc           	      C   s   | j | j}}d}i }td|d|d< td|d|d< t|}tj|jtj	|dkdf< |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   Tr   r   r   r   )r   r   r   r   r   r   r   rc   rm   r   r   r4   r[   r   r   r5   r6   )	r)   r   r   rK   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   | j | j}}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 sYw   Y  q9d S )Nr}   r   r   )datesZnondatezdates == nondater   r   zdates != nondatez=Invalid comparison between dtype=datetime64\[ns\] and ndarray)<><=>=r@   zdates z nondate)r   r   r   r   rc   r   r4   lenr5   r6   r   rB   	TypeError)r)   r   r   rK   r*   r:   rD   rf   r   r   r   test_date_query_with_non_date  s    z=TestDataFrameQueryNumExprPandas.test_date_query_with_non_datec                 C   sv   | j | j}}ttdtddtddd}d}tjt|d |jd	||d
 W d    d S 1 s4w   Y  d S )Nr}   r&      rL      )irM   rzinvalid syntaxr@   zi - +r   )r   r   r   r_   r   rB   SyntaxErrorr4   )r)   r   r   r*   rD   r   r   r   test_query_syntax_error  s    "z7TestDataFrameQueryNumExprPandas.test_query_syntax_errorc                 C   s  | j | j}}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 saw   Y  tjtdd
 |jd||d W d    d S 1 sw   Y  d S )N   r%   r~   r^   )r$   r%   za > br   z@a > b!local variable 'c' is not definedr@   z@a > b > @czname 'c' is not definedz
@a > b > c)r   r   r    r   rc   rm   rn   r`   r4   rr   rs   r5   r6   r   rB   r   )r)   r   r   r*   rr   rs   r   ri   r   r   r   test_query_scope  s$   "z0TestDataFrameQueryNumExprPandas.test_query_scopec                 C   sz   | j | j}}d }}ttjj||dfdtdd}tjt	dd |j
d||d	 W d    d S 1 s6w   Y  d S )
Nr}   r&   r   abcr^   zname 'sin' is not definedr@   sin > 5r   )r   r   r   rc   rm   randintr`   r   rB   r   r4   )r)   r   r   rK   rg   r*   r   r   r   test_query_doesnt_pickup_local  s    "z>TestDataFrameQueryNumExprPandas.test_query_doesnt_pickup_localc                 C   s   | j | j}}d }}ttjj||dfdtdd}d|j_d}t	j
t|d |jd	||d
 W d    d S 1 s<w   Y  d S )Nr}   r&   r   r   r^   sinzVariables in expression.+r@   r   r   )r   r   r   rc   rm   r   r`   r[   r   r   rB   r   r4   )r)   r   r   rK   rg   r*   rD   r   r   r   test_query_builtin  s    "z2TestDataFrameQueryNumExprPandas.test_query_builtinc                 C   s   | j | j}}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&   rr   rs   r   r^   a < br   za + b > b * c)r   r   r   rc   rm   rn   r5   r6   r4   rr   rs   r   r)   r   r   r*   r   r   r   
test_query   s   z*TestDataFrameQueryNumExprPandas.test_queryc                 C   s   | j | j}}ttj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   Zblobr   r   rZ   z(blob < 5) & (a < b)r   rk   zblob < b)r   r   r   rc   rm   r   r   r_   r4   r[   rr   rs   r5   r6   r   r   r   r   test_query_index_with_name,  s   z:TestDataFrameQueryNumExprPandas.test_query_index_with_namec                 C   s   | j | j}}ttj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   rZ   z	index < br   z	index < 5rk   )r   r   r   rc   rm   r   r_   r4   r[   rs   r5   r6   r   r   r   r   test_query_index_without_name<  s   z=TestDataFrameQueryNumExprPandas.test_query_index_without_namec                 C   s   | j }| j}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 )
Nrk   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   r    r   rc   rm   rn   r4   r5   r6   r   r7   )r)   r   r   r*   df2ri   r:   r   r   r   test_nested_scopeN  s$   $z1TestDataFrameQueryNumExprPandas.test_nested_scopec                 C   sZ   t tjdd}tjtdd |jd| j| j	d W d    d S 1 s&w   Y  d S )Nrk   r&   name 'df' is not definedr@   zdf > 0r   )
r   rc   rm   rn   r   rB   r   r4   r   r   r,   r   r   r   *test_nested_raises_on_local_self_referenceh  s   "zJTestDataFrameQueryNumExprPandas.test_nested_raises_on_local_self_referencec                 C   s   t | j | j| j}}ttjddtdd}d}||j|k  }|j	d||d}t
|| ||j|jk  }|j	d||d}t
|| d S )	Nd   r}   Z
abcdefghijr^   r$   za < @br   r   )r    r   r   r   rc   rm   rn   r`   rr   r4   r5   r6   rs   )r)   r   r   r*   rs   expectr:   r   r   r   test_local_syntaxo  s   
z1TestDataFrameQueryNumExprPandas.test_local_syntaxc                 C   s   t | j | j| j}}td}ttjdt||d}|j	d||d}|j
|jk |j|jk @ |j|j
 @ |j|j @ }|| }t|| d S )Nr   r  r^   z!a < b < c and a not in b not in cr   )r    r   r   r`   r   rc   rm   rn   r   r4   rr   rs   r   r   r5   r6   )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   | j | j}}t| ttjjddddd}ttjjddddd}t||d}|j|j	d	 
| }|jd
||d}t|| ttjjddddd}|j|d	 
| }|jd||d}t|| d S )Nr&      r   rr   r   r}   rs   rr   rs   r$   z
b - 1 in ar   z@b - 1 in a)r   r   r    r   rc   rm   r   r   r   rs   r   r4   r5   r6   )r)   r   r   rr   rs   r*   ri   r:   r   r   r   test_local_variable_with_in  s   z;TestDataFrameQueryNumExprPandas.test_local_variable_with_inc                 C   sX   | j | j}}t| d}tdg di}|jd||d}||jdk }t|| d S )Nr$   rr   )rr   rr   rs   rs   @cr  z	a == "@c"r   r  )r   r   r    r   r4   rr   r5   r6   )r)   r   r   r   r*   r:   ri   r   r   r   test_at_inside_string  s   z5TestDataFrameQueryNumExprPandas.test_at_inside_stringc                 C   st   | j | j}}t| ttjddtdd}tj	t
dd |jd||d W d    d S 1 s3w   Y  d S )	Nr}   r%   r~   r^   r   r@   za == @cr   )r   r   r    r   rc   rm   r   r`   r   rB   r   r4   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|jd}d|j_|jd| j	| j
d}||d dk }t|| t|t j|jd}|jd	| j	| j
d}|j|j|jdk  }t|| t|t j|jd
}d|j_|jd| j	| j
d}||jdk }t|| |jd| j	| j
d}|j|j|jdk  }t|| d S )Nr$   r   e   )r[   rs   r[   z	index > 5r   rk   zilevel_0 > 5r  rr   za > 5)rc   Zr_r   rm   rn   r   r[   r   r4   r   r   r5   r6   r   rr   )r)   rK   rr   r*   r:   ri   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   sz   d}t tj|tj|d}tj|jd d ddf< d| d}|||jtj }|j|| j| j	d}t
|| d S )Nr}   r  r%   r   a z infr   )r   rc   rm   r   infr   rr   r4   r   r   r5   r6   )r)   rf   frK   r*   qri   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-01Z1dr}   time)startfreqr   tzr   r   z"2018-01-03 00:00:00+00" < time)r   r   r4   r5   r6   Zreset_index)r)   Ztz_aware_fixturer  Zdf_indexri   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| tj|d}||d ddk }|jd| j| jd}t	|| t t
tj|d	k tjtj|tj|d}||d   }|jd
| j| jd}t	|| d S )Nr}   r%   r  rr   intr   za.astype('int') == 0r   r   za.notnull())r   rc   rm   r   r   r4   r   r   r5   r6   wherero   rn   Znotnull)r)   rK   r*   ri   r:   r   r   r   test_method_calls_in_query  s   "
"
z:TestDataFrameQueryNumExprPandas.test_method_calls_in_queryN)%rF   rG   rH   classmethodr   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ner  r  r"  r   r   r   r   r     s>    


		

	r   c                       sX   e Zd Ze fddZdd Zdd Zdd Zd	d
 Zdd Z	dd Z
dd Z  ZS )TestDataFrameQueryNumExprPythonc                       t    d| _d| _d S )Nr   r
   superr   r   r   r   	__class__r   r   r        

z+TestDataFrameQueryNumExprPython.setup_classc                 C   r   )Nrk   r&   r   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.  r   r   r   r   r   r   r   r     s   z8TestDataFrameQueryNumExprPython.test_date_query_with_NaTc                 C   r   )Nr}   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   Tr   r/  r   r   r   r   r   r   r   r   $  s   z>TestDataFrameQueryNumExprPython.test_date_index_query_with_NaTc                 C   s   | j | j}}d}ttj|d}td|d|d< td|d|d< tj|j	tj
|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   Tr   "'BoolOp' nodes are not implementedr@   r   r   )r   r   r   rc   rm   rn   r   r   r   r   r   r   r   rB   NotImplementedErrorr4   )r)   r   r   rK   r*   r   rD   r   r   r   r   4  s   "zITestDataFrameQueryNumExprPython.test_date_index_query_with_NaT_duplicatesc           	      C   sD  | j }| j}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 sDw   Y  tj	td
d |jd||d W d    n1 scw   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%   rk   r&   z5The '@' prefix is only supported by the pandas parserr@   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   r7   r   rc   rm   rn   r   rB   r   r4   r   r5   r6   )	r)   r   r   r   r:   r*   r  rD   ri   r   r   r   r  A  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#   rL   rk      )minmaxzMVariables in expression \"\(min\) == \(1\)\" overlap with builtins: \('min'\)r@   zmin == 1zMVariables in expression \"\(max\) == \(1\)\" overlap with builtins: \('max'\)zmax == 1)r   r   rB   r   r4   )r)   r*   Zregex_to_matchr   r   r   +test_query_numexpr_with_min_and_max_columns^  s   "zKTestDataFrameQueryNumExprPython.test_query_numexpr_with_min_and_max_columns)rF   rG   rH   r#  r   r   r   r   r   r   r  r6  __classcell__r   r   r+  r   r'    s    r'  c                       (   e Zd Ze fddZdd Z  ZS )TestDataFrameQueryPythonPandasc                    r(  )Nr
   r   r)  r   r+  r   r   r   p  r-  z*TestDataFrameQueryPythonPandas.setup_classc                 C   l   | j | j}}d }}ttjj||dfdtdd}d|j_||jdk }|j	d||d	}t
|| d S 
Nr}   r&   r   r   r^   r   rk   r   r   r   r   r   rc   rm   r   r`   r[   r   r4   r5   r6   r)   r   r   rK   rg   r*   ri   r:   r   r   r   r   v      z1TestDataFrameQueryPythonPandas.test_query_builtinrF   rG   rH   r#  r   r   r7  r   r   r+  r   r9  o  s    r9  c                       r8  )TestDataFrameQueryPythonPythonc                    s   t    d | _| _d S )Nr
   r)  r   r+  r   r   r     s   
z*TestDataFrameQueryPythonPython.setup_classc                 C   r:  r;  r<  r=  r   r   r   r     r>  z1TestDataFrameQueryPythonPython.test_query_builtinr?  r   r   r+  r   r@    s    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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 suw   Y  qHd 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$   rs   r^   
aabbccddeestringsrr   r   z"a"r%   r]   r  r  $'(Not)?In' nodes are not implemented r@   )r   r   Z
local_dictz"a" == stringsr   zstrings == "a"zstrings != "a"z"a" != strings)r   rc   rm   rn   r   r`   rC  zipr   rB   r1  r4   r5   r6   r   r)   r   r   r*   r  collstlhsrhsr%  r&  opsrD   rf   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 stw   Y  qKd 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$   rs   r^   rB  rC  rr   r   z
["a", "b"]r%   r]   rD  rE  rF  r@   r   zstrings == ["a", "b"]z["a", "b"] == stringszstrings != ["a", "b"]z["a", "b"] != strings)r   rc   rm   rn   r   r`   rC  r   rG  r   rB   r1  r4   r5   r6   rH  r   r   r   test_str_list_query_method  s6   z4TestDataFrameQueryStrings.test_str_list_query_methodc                 C   s0  t tdtdtjjdddtjjdddd}|dkrS|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 sow   Y  d}tjt|d |jd||d
 W d    d S 1 sw   Y  d S )Naaaabbbbccccaabbccddeeffrk      r   	   rr   rs   r   r   r   za in br   za in b and c < drE  r@   r0  )r   r`   rc   rm   r   r4   rr   r   rs   r5   r6   r   r   r   rB   r1  )r)   r   r   r*   r   r   rD   r   r   r   test_query_with_string_columns  s,   "z8TestDataFrameQueryStrings.test_query_with_string_columnsc                 C   s   t tdtdtjjdddtjjdddd}|jd||d	}||j|jk }t	|| |jd
||d	}||j|jk }t	|| d S )NrQ  rR  rk   rS  r   rT  rU  za == br   za != b)
r   r`   rc   rm   r   r4   rr   rs   r5   r6   )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 rF  r   ).0rK   Zactr   r   r   
<listcomp>  s
    zLTestDataFrameQueryStrings.test_query_with_nested_strings.<locals>.<listcomp>r$   rL   r%   z2014-01-01 0:00:01Z30sr3  )r  r   z2014-02-01 1:00:01   )idevent	timestampz"page 1 load"z'"page 1 load"' in eventr   )r    r_   r   r   rc   r   repeatappendr^  r4   r5   r6   )	r)   r   r   eventsZstamps1Zstamps2r*   ri   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)rr   rs   test & testr#   r  za == "test & test"r   rd  )r    r   r4   rr   r5   r6   )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   sl   t tjtdd}t t|j}t||d}|jd| d||d}|||j	d }	t
||	 d S )NZabcder   )Xr   zX z "d"r   r   )r   rc   rm   r   r`   r   r   r   r4   rf  r5   r6   )
r)   r   r   rf   funcrr   rs   r*   r   ri   r   r   r   test_query_lex_compare_strings&  s   
z8TestDataFrameQueryStrings.test_query_lex_compare_stringsc                 C   s^   d}t jjddt|fdt}t||d}|jd||d}||j|j	@  }t
|| d S )N)bidZbidsizeaskZasksizer%   r$   r   r^   z	bid & askr   )rc   rm   r   r   r   boolr   r4   ri  rj  r5   r6   )r)   r   r   r\   datar*   r   ri   r   r   r   "test_query_single_element_booleans8  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 USrn  IBM USro  )gl[@gGzn[@gf@g33333f@)SymbolZPricern  zSymbol == @symbr   )r    r   rp  r4   r5   r6   )r)   r   r   r*   eZsymbr   r   r   r   !test_query_string_scalar_variable@  s   z;TestDataFrameQueryStrings.test_query_string_scalar_variableN)rF   rG   rH   rO  rP  rV  rW  rc  re  r   r   r   r$  ltgtlegerh  rm  rr  r   r   r   r   rA    s$    )#
		rA  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   rc   rm   rn   r`   r(   r   r   r   frameO  s   z TestDataFrameEvalWithFrame.framec                 C   s,   |j d||d}|j|j }t|| d S )Nr   r   r7   rr   rs   r5   r8   r)   rx  r   r   r   r  r   r   r   test_simple_exprS  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$   ry  rz  r   r   r   test_bool_arith_exprX  s   z/TestDataFrameEvalWithFrame.test_bool_arith_exprrf   )rM   rP   rS   rV   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 '.+'r@   r  z br   )r   r   rB   r   r7   )r)   r   r   rf   r*   rD   r   r   r   %test_invalid_type_for_operator_raises]  s
   "z@TestDataFrameEvalWithFrame.test_invalid_type_for_operator_raisesN)rF   rG   rH   r   rI   rx  r{  r|  r   r   r}  r   r   r   r   rw  N  s    
rw  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 Cr2  C  C)r\  rL   r&   C_C)   rT  r}   D_D D)   r$   r  E.E)r3  r&   rk   F-F)r  r$   r}   1e1)r%   rL   r  def)r}   r  r%   A (x))rL   r$   r&   zB(x))r$   r$   rk   zB (x))r%   r\  rL        &^ :!€$?(} >    <++*''  )r%   rk   r3  r{   )r}   r  r$    A)rL   r\  rT    r$   r%   r$   )r3  r&   r$   )rT  r$   r  )r  r\  r3  )r%   rL   rk   )rk   r\  rT  )zit'szthat'su   ☺zfoo#barr$   Nr'   r(   r   r   r   r*   g  sX   	

z$TestDataFrameQueryBacktickQuoting.dfc                 C   s*   | d}|d|d k  }t|| d S )Nz	1 < `B B`r$   r  r4   r5   r6   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  rL   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  r7   r5   r8   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{   rk   r  r  r   r   r   test_empty_string  r  z3TestDataFrameQueryBacktickQuoting.test_empty_stringc                 C   r  )Nz
`C  C` > 5r  rk   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  rL   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'r@   z	@pd.thing)r   rB   AttributeErrorr7   )r)   r*   messager   r   r   test_missing_attribute     "z8TestDataFrameQueryBacktickQuoting.test_missing_attributec                 C   r  )N6(Could not convert ).*( to a valid Python identifier.)r@   z`it's` > `that's`r   rB   r   r4   ry   r   r   r   test_failing_quote  r  z4TestDataFrameQueryBacktickQuoting.test_failing_quotec                 C   r  )Nr  r@   u	   `☺` > 4r  ry   r   r   r   $test_failing_character_outside_range  r  zFTestDataFrameQueryBacktickQuoting.test_failing_character_outside_rangec                 C   r  )NzFailed to parse backticksr@   z`foo#bar` > 4r  ry   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   s   dS r.   r   )_r   r   r   rg    s   zNTestDataFrameQueryBacktickQuoting.test_call_non_named_expression.<locals>.funcz@func()z"Only named functions are supportedr@   z@funcs[0]()Nz@funcs[0].__call__())r7   r   rB   r   )r)   r*   rg  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&   rL   rr   rs   r\   dtypez	c = b - ar  )r&   rL   r$   r   r   r	   RuntimeWarningr5   assert_produces_warningr7   r6   )r)   any_numeric_ea_and_arrow_dtyper*   warningr:   ri   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&   rL   rr   rs   ZFloat64r  z	c = b - 1r  )r&   rL   r&   r   r  )r)   r*   r  r:   ri   r   r   r   test_ea_dtypes_and_scalar  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&   rL   rr   rs   r  z	c = 2 - 1r  r   r   )r   r7   r   r  r5   r6   )r)   r  r*   r:   ri   r   r   r   #test_ea_dtypes_and_scalar_operation!  s   
zETestDataFrameQueryBacktickQuoting.test_ea_dtypes_and_scalar_operationr  )Zint64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  pyarrowrr   r$   r%   r  r  z	a in @refr  r[   )
r   importorskipr   r   r	   r  r5   r  r4   r6   )r)   r  r*   refr  r:   ri   r   r   r   test_query_ea_dtypes0  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%   rt   zA == Br<   r$   r%   r   r  )r  r	   r   r   r  r   r   r5   r  r4   r6   )r)   r  r   r  r*   r:   ri   r   r   r   !test_query_ea_equality_comparison=  s"   

 zCTestDataFrameQueryBacktickQuoting.test_query_ea_equality_comparisonN)#rF   rG   rH   r   rI   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~  f  sB    
!
r~  )'r$  numpyrc   r   Zpandas.errorsr   r   Zpandas.util._test_decoratorsutilZ_test_decoratorstdr   r   r   r   r   r   r   Zpandas._testingZ_testingr5   Zpandas.core.computation.checkr	   rI   r   r   Zskip_if_no_ner   r    r!   rJ   r   r   r'  r9  r@  rA  rw  r~  r   r   r   r   <module>   s@    

3_ H  r{ ;