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   g/var/www/html/visualizacion-main/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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   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   rf   randomrandn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brz   r$   Bz%expr must be a string to be evaluatedrC   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   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   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'   abra   ry   r&   rz   a + b	resolvers)r   rf   rt   ru   rc   r:   pd)r+   r,   dict1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   ra   cz	a + b * cr   ry   rz   )r   rf   rt   ru   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   rx   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   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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   choicer   from_arraysr   ru   r   r^   get_level_valuesrh   r7   r8   r9   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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   ratingr'   r   zrating == 1r   r   r&   zrating != 1r   r   r   )r"   rf   rt   r   aranger   r   r   r   ru   r7   r   r^   r   rh   r8   r9   )
r+   r   r   ry   rz   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   )r_idx_nlevelsr_idx_namesc                 S   s   |  |}| }| |_|S r   )r   	to_seriesr^   )milevellevel_valuessr   r   r   r   k  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_resolverse  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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 )Nrr   r(   1/1/2012periodsdates11/1/2013dates21/1/2014dates3z"@df.dates1 < 20130101 < @df.dates3r   20130101)r   r   r"   r   rf   rt   ru   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      | 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 )Nrr   r(   r   r   r   r   r   r   r   dates1 < 20130101 < dates3r   r   r   r   r   rf   rt   ru   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      | 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   rf   rt   ru   r   r   NaTlocrandr7   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      | 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inplacedropindex < 20130101 < dates3r   r   r   r   r   rf   rt   ru   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      | 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   rf   rt   ru   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   | 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   rf   rt   r   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   | 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   )datesnondatezdates == nondater   r   zdates != nondatez=Invalid comparison between dtype=datetime64\[ns\] and ndarray)<><=>=rC   zdates z nondate)r   r   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   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(      rO      )irP   rzinvalid syntaxrC   zi - +r   )r   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  | 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   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   r"   r   rf   rt   ru   rc   r7   ry   rz   r8   r9   r   rE   r   )r+   r   r   r,   ry   rz   r   rp   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   abcra   zname 'sin' is not definedrC   sin > 5r   )r   r   r   rf   rt   randintrc   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   | 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
  ra   sinzVariables in expression.+rC   r  r   )r   r   r   rf   rt   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   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(   ry   rz   r   ra   a < br   za + b > b * c)r   r   r   rf   rt   ru   r8   r9   r7   ry   rz   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   blobr   r  r]   z(blob < 5) & (a < b)r   rr   zblob < b)r   r   r   rf   rt   r  r   rb   r7   r^   ry   rz   r8   r9   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  r]   z	index < br   z	index < 5rr   )r   r   r   rf   rt   r  rb   r7   r^   rz   r8   r9   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 )
Nrr   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   rf   rt   ru   r7   r8   r9   r   r:   )r+   r   r   r,   df2rp   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 )Nrr   r(   name 'df' is not definedrC   zdf > 0r   )
r   rf   rt   ru   r   rE   r   r7   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   
abcdefghijra   r&   za < @br   r  )r"   r   r   r   rf   rt   ru   rc   ry   r7   r8   r9   rz   )r+   r   r   r,   rz   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  ra   z!a < b < c and a not in b not in cr   )r"   r   r   rc   r   rf   rt   ru   r   r7   ry   rz   r   r   r8   r9   )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   ry   r  r   rz   ry   rz   r&   z
b - 1 in ar   z@b - 1 in a)r   r   r"   r   rf   rt   r  r   r   rz   r   r7   r8   r9   )r+   r   r   ry   rz   r,   rp   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&   ry   )ry   ry   rz   rz   @cr'  z	a == "@c"r   r'  )r   r   r"   r   r7   ry   r8   r9   )r+   r   r   r   r,   r=   rp   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   ra   r  rC   za == @cr   )r   r   r"   r   rf   rt   r   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|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^   rz   r^   z	index > 5r   rr   zilevel_0 > 5r%  ry   za > 5)rf   r_r   rt   ru   r   r^   r   r7   r   r   r8   r9   r   ry   )r+   rN   ry   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   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   rf   rt   r   infr   ry   r7   r   r   r8   r9   )r+   rl   frN   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_fixturer8  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| 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%  ry   intr   za.astype('int') == 0r   r   za.notnull())r   rf   rt   r   r   r7   r   r   r8   r9   whererv   ru   notnull)r+   rN   r,   rp   r=   r   r   r   test_method_calls_in_query  s   "
"
z:TestDataFrameQueryNumExprPandas.test_method_calls_in_queryN)%rI   rJ   rK   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ner3  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   )Nrr   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   rL  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   rM  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 implementedrC   r   r   )r   r   r   rf   rt   ru   r   r   r   r   r   r   r   rE   NotImplementedErrorr7   )r+   r   r   rN   r,   r   rG   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'   rr   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   r:   r   rf   rt   ru   r   rE   r  r7   r   r8   r9   )	r+   r   r   r   r=   r,   r  rG   rp   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%   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_columns^  s   "zKTestDataFrameQueryNumExprPython.test_query_numexpr_with_min_and_max_columns)rI   rJ   rK   rA  r   r   r   r   r   r   r  rU  __classcell__r   r   rI  r   rE    s    rE  c                       (   e Zd Ze fddZdd Z  ZS )TestDataFrameQueryPythonPandasc                    rF  )Nr
   r   rG  r   rI  r   r   r   p  rK  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
  ra   r  rr   r  r   r   r   r   rf   rt   r  rc   r^   r   r7   r8   r9   r+   r   r   rN   rn   r,   rp   r=   r   r   r   r  v      z1TestDataFrameQueryPythonPandas.test_query_builtinrI   rJ   rK   rA  r   r  rV  r   r   rI  r   rX  o  s    rX  c                       rW  )TestDataFrameQueryPythonPythonc                    s   t    d | _| _d S )Nr
   rG  r   rI  r   r   r     s   
z*TestDataFrameQueryPythonPython.setup_classc                 C   rY  rZ  r[  r\  r   r   r   r    r]  z1TestDataFrameQueryPythonPython.test_query_builtinr^  r   r   rI  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&   rz   ra   
aabbccddeestringsry   r   z"a"r'   r`   r-  r.  $'(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   r   rc   rb  zipr   rE   rO  r7   r8   r9   r   r+   r   r   r,   r   collstlhsrhsrC  rD  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 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&   rz   ra   ra  rb  ry   r   z
["a", "b"]r'   r`   rc  rd  re  rC   r   zstrings == ["a", "b"]z["a", "b"] == stringszstrings != ["a", "b"]z["a", "b"] != strings)r   rf   rt   ru   r   rc   rb  r   rg  r   rE   rO  r7   r8   r9   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aabbccddeeffrr      r   	   ry   rz   r   r   r   za in br   za in b and c < drd  rC   rN  )r   rc   rf   rt   r  r7   ry   r   rz   r8   r9   r   r   r   rE   rO  )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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  rr   rs  r   rt  ru  za == br   za != b)
r   rc   rf   rt   r  r7   ry   rz   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 re  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30srQ  )r7  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)ry   rz   test & testr%   r%  za == "test & test"r   r  )r"   r   r7   ry   r8   r9   )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 )Nabcder  )Xr   zX z "d"r   r   )r   rf   rt   r   rc   r   r   r   r7   r  r8   r9   )
r+   r   r   rl   funcry   rz   r,   r   rp   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)bidbidsizeaskasksizer'   r&   r   ra   z	bid & askr   )rf   rt   r  r   r   boolr   r7   r  r  r8   r9   )r+   r   r   r_   datar,   r   rp   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 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_variable@  s   z;TestDataFrameQueryStrings.test_query_string_scalar_variableN)rI   rJ   rK   ro  rp  rv  rw  r  r  r   r   r   rB  ltgtleger  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   rc   r*   r   r   r   frameO  s   z TestDataFrameEvalWithFrame.framec                 C   s,   |j d||d}|j|j }t|| d S )Nr   r   r:   ry   rz   r8   r;   r+   r  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&   r  r  r   r   r   test_bool_arith_exprX  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   r/  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  N  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 CrP  C  C)r~  rO   r(   C_C)   rt  r   D_D D)   r&   r*  E.E)rQ  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   rQ  r   )r   r  r&    A)rO   r~  rt    r&   r'   r&   )rQ  r(   r&   )rt  r&   r  )r  r~  rQ  )r'   rO   rr   )rr   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  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   s   dS 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   ry   rz   r_   dtypez	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_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(   rO   ry   rz   Float64r  z	c = b - 1r  )r(   rO   r(   r  r  )r+   r,   r  r=   rp   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(   rO   ry   rz   r  z	c = 2 - 1r  r   r  )r   r:   r   r  r8   r9   )r+   r  r,   r=   rp   r   r   r   #test_ea_dtypes_and_scalar_operation!  s   
zETestDataFrameQueryBacktickQuoting.test_ea_dtypes_and_scalar_operationr  )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  pyarrowry   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_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'   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_comparison=  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  f  sB    
!
r  )'rB  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   rE  rX  r_  r`  r  r  r   r   r   r   <module>   s@    

3_ H  r{ ;