3
Êb_l9  ã               @   s:  d dl Zd dlZd dlmZ d dlZd dljZ	d dl
mZmZ ddlmZ ejddgd�d	d
„ ƒZejdd„ ƒZejdd„ ƒZejdd„ ƒZejdd„ ƒZejdd„ ƒZejdd„ ƒZejdd„ ƒZejdd„ ƒZejdd„ ƒZejjdƒZG dd„ dƒZG dd „ d eejƒZG d!d"„ d"eej ƒZ!G d#d$„ d$eej"ƒZ#G d%d&„ d&eej$ƒZ%G d'd(„ d(eej&ƒZ'G d)d*„ d*eej(ƒZ)G d+d,„ d,eej*ƒZ+eG d-d.„ d.eej,ƒƒZ-G d/d0„ d0eej.ƒZ/eG d1d2„ d2eej0ƒƒZ1eG d3d4„ d4eej2ƒƒZ3G d5d6„ d6eej4ƒZ5G d7d8„ d8eej6ƒZ7G d9d:„ d:eej8ƒZ9eG d;d<„ d<eej:ƒƒZ;dS )=é    N)Ú_np_version_under1p16)ÚPandasArrayÚPandasDtypeé   )ÚbaseÚfloatÚobject)Úparamsc             C   s   t tj| jƒƒS )N)r   ÚnpÚdtypeÚparam)Úrequest© r   úC/tmp/pip-build-jkwsxyiy/pandas/pandas/tests/extension/test_numpy.pyr      s    r   c             c   s,   | j ƒ �}|jtddƒ dV  W dQ R X dS )aó  
    A monkeypatch to tells pandas to let us in.

    By default, passing a PandasArray to an index / series / frame
    constructor will unbox that PandasArray to an ndarray, and treat
    it as a non-EA column. We don't want people using EAs without
    reason.

    The mechanism for this is a check against ABCPandasArray
    in each constructor.

    But, for testing, we need to allow them in pandas. So we patch
    the _typ of PandasArray, so that we evade the ABCPandasArray
    check.
    Z_typÚ	extensionN)ÚcontextÚsetattrr   )ZmonkeypatchÚmr   r   r   Úallow_in_pandas   s    
r   c             C   s:   |j dkr$tjdd„ tdƒD ƒƒjS ttjdd|jd�ƒS )Nr   c             S   s   g | ]
}|f‘qS r   r   )Ú.0Úir   r   r   ú
<listcomp>+   s    zdata.<locals>.<listcomp>éd   r   ée   )r   )	Únumpy_dtypeÚpdÚSeriesÚrangeÚarrayr   r
   ÚarangeÚ_dtype)r   r   r   r   r   Údata(   s    
r!   c             C   sD   |j dkr0trtjdƒ‚ttjtjdgtd�ƒS ttjtjdgƒƒS )Nr   zSkipping for NumPy <1.16r   )r   g      ð?)r   )	r   r   ÚpytestÚskipr   r
   r   Únanr   )r   r   r   r   r   Údata_missing/   s
    

r%   c               C   s   t jS )N)r
   r$   r   r   r   r   Úna_value:   s    r&   c              C   s   dd„ } | S )Nc             S   s   t j| ƒot j|ƒS )N)r
   Úisnan)ÚaÚbr   r   r   ÚcmpA   s    zna_cmp.<locals>.cmpr   )r*   r   r   r   Úna_cmp?   s    r+   c             C   s@   |j dkr,ttjf dd	d
gtd�dd… ƒS ttjdddgƒƒS )ziLength-3 array with a known sort order.

    This should be three items [B, C, A] with
    A < B < C
    r   é   é   r   )r   Nr   )r,   )r-   )r   )r   r   r
   r   r   )r   r   r   r   r   Údata_for_sortingG   s    
"r.   c             C   s:   |j dkr$ttjdtjdgtd�ƒS ttjdtjdgƒƒS )zvLength-3 array with a known sort order.

    This should be three items [B, NA, A] with
    A < B and NA missing.
    r   r   r   )r   )r   )r   )r   r   r
   r   r$   r   )r   r   r   r   r   Údata_missing_for_sortingU   s    
r/   c          
   C   sN   |j dkrd	\}}}ntjdƒ\}}}ttj||tjtj||||g|j d�ƒS )
z“Data for factorization, grouping, and unique tests.

    Expected to be like [B, B, NA, NA, A, A, B, C]

    Where A < B < C and NA is missing
    r   r   r,   r-   )r   ©r   ©r,   ©r-   )r0   r1   r2   )r   r
   r   r   r   r$   )r   r   r(   r)   Úcr   r   r   Údata_for_groupinga   s
    
r4   c             C   s   | dkrt jdƒ‚dS )aŒ  
    Tests for PandasArray with nested data. Users typically won't create
    these objects via `pd.array`, but they can show up through `.array`
    on a Series with nested data. Many of the base tests fail, as they aren't
    appropriate for nested data.

    This fixture allows these tests to be skipped when used as a usefixtures
    marker to either an individual test or a test class.
    r   zSkipping for object dtype.N)r"   r#   )r   r   r   r   Úskip_numpy_objectr   s    r5   c               @   s   e Zd ZdS )ÚBaseNumPyTestsN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r6   „   s   r6   c                   s    e Zd Ze‡ fdd„ƒZ‡  ZS )ÚTestCastingc                s   t ƒ j|ƒ d S )N)ÚsuperÚtest_astype_str)Úselfr!   )Ú	__class__r   r   r<   ‰   s    zTestCasting.test_astype_str)r7   r8   r9   Úskip_nestedr<   Ú__classcell__r   r   )r>   r   r:   ˆ   s   r:   c                   s6   e Zd Zejjdd�dd„ ƒZe‡ fdd„ƒZ‡  Z	S )ÚTestConstructorszWe don't register our dtype)Úreasonc             C   s   d S )Nr   )r=   r!   r   r   r   Útest_from_dtype�   s    z TestConstructors.test_from_dtypec                s   t ƒ j|ƒ d S )N)r;   Útest_array_from_scalars)r=   r!   )r>   r   r   rD   •   s    z(TestConstructors.test_array_from_scalars)
r7   r8   r9   r"   Úmarkr#   rC   r?   rD   r@   r   r   )r>   r   rA   �   s   rA   c               @   s"   e Zd Zejjdd�dd„ ƒZdS )Ú	TestDtypezIncorrect expected.)rB   c             C   s   d S )Nr   )r=   r!   r   r   r   Útest_check_dtypeœ   s    zTestDtype.test_check_dtypeN)r7   r8   r9   r"   rE   r#   rG   r   r   r   r   rF   ›   s   rF   c                   sJ   e Zd Ze‡ fdd„ƒZe‡ fdd„ƒZejjdd�‡ fdd„ƒZ	‡  Z
S )	ÚTestGetitemc                s   t ƒ j|ƒ d S )N)r;   Útest_getitem_scalar)r=   r!   )r>   r   r   rI   £   s    zTestGetitem.test_getitem_scalarc                s   t ƒ j|ƒ d S )N)r;   Útest_take_series)r=   r!   )r>   r   r   rJ   ¨   s    zTestGetitem.test_take_seriesz#astype doesn't recognize data.dtype)rB   c                s   t ƒ j|ƒ d S )N)r;   Ú test_loc_iloc_frame_single_dtype)r=   r!   )r>   r   r   rK   ­   s    z,TestGetitem.test_loc_iloc_frame_single_dtype)r7   r8   r9   r?   rI   rJ   r"   rE   ÚxfailrK   r@   r   r   )r>   r   rH   ¢   s   rH   c                   s    e Zd Ze‡ fdd„ƒZ‡  ZS )ÚTestGroupbyc                s   t ƒ j||ƒ d S )N)r;   Útest_groupby_extension_apply)r=   r4   Zgroupby_apply_op)r>   r   r   rN   ³   s    z(TestGroupby.test_groupby_extension_apply)r7   r8   r9   r?   rN   r@   r   r   )r>   r   rM   ²   s   rM   c                   s    e Zd Ze‡ fdd„ƒZ‡  ZS )ÚTestInterfacec                s   t ƒ j|ƒ d S )N)r;   Útest_array_interface)r=   r!   )r>   r   r   rP   º   s    z"TestInterface.test_array_interface)r7   r8   r9   r?   rP   r@   r   r   )r>   r   rO   ¹   s   rO   c            	       s6  e Zd Zejjdd�dd„ ƒZejjdd�‡ fdd„ƒZe‡ fdd	„ƒZ	e‡ fd
d„ƒZ
eejjdejdd„ gƒejjddd„ ejgƒ‡ fdd„ƒƒƒZe‡ fdd„ƒZe‡ fdd„ƒZe‡ fdd„ƒZe‡ fdd„ƒZe‡ fdd„ƒZeejjdddd dd d!ggƒ‡ fd"d#„ƒƒZejjd$d�‡ fd%d&„ƒZ‡  ZS )'ÚTestMethodszTODO: remove?)rB   c             C   s   d S )Nr   )r=   Zall_dataZdropnar   r   r   Útest_value_countsÁ   s    zTestMethods.test_value_countszIncorrect expectedc                s   t ƒ j|ƒ d S )N)r;   Útest_combine_le)r=   Údata_repeated)r>   r   r   rS   Å   s    zTestMethods.test_combine_lec                s   t ƒ j|ƒ d S )N)r;   Útest_combine_add)r=   rT   )r>   r   r   rU   Ë   s    zTestMethods.test_combine_addc                s   t ƒ j|ƒ d S )N)r;   Útest_shift_fill_value)r=   r!   )r>   r   r   rV   Ð   s    z!TestMethods.test_shift_fill_valueÚboxc             C   s   | S )Nr   )Úxr   r   r   Ú<lambda>Ö   s    zTestMethods.<lambda>Úmethodc             C   s   | j ƒ S )N)Úunique)rX   r   r   r   rY   ×   s    c                s   t ƒ j|||ƒ d S )N)r;   Útest_unique)r=   r!   rW   rZ   )r>   r   r   r\   Õ   s    zTestMethods.test_uniquec                s   t ƒ j|ƒ d S )N)r;   Útest_fillna_copy_frame)r=   r%   )r>   r   r   r]   Ü   s    z"TestMethods.test_fillna_copy_framec                s   t ƒ j|ƒ d S )N)r;   Útest_fillna_copy_series)r=   r%   )r>   r   r   r^   á   s    z#TestMethods.test_fillna_copy_seriesc                s   t ƒ j||ƒ d S )N)r;   Útest_hash_pandas_object_works)r=   r!   Úas_frame)r>   r   r   r_   æ   s    z)TestMethods.test_hash_pandas_object_worksc                s   t ƒ j||ƒ d S )N)r;   Útest_searchsorted)r=   r.   Ú	as_series)r>   r   r   ra   ë   s    zTestMethods.test_searchsortedc                s   t ƒ j|||ƒ d S )N)r;   Útest_where_series)r=   r!   r&   r`   )r>   r   r   rc   ð   s    zTestMethods.test_where_seriesÚrepeatsr   r   r,   r-   c                s   t ƒ j||||ƒ d S )N)r;   Útest_repeat)r=   r!   rd   rb   Z	use_numpy)r>   r   r   re   õ   s    zTestMethods.test_repeatz"PandasArray.diff may fail on dtypec                s   t ƒ j||ƒS )N)r;   Ú	test_diff)r=   r!   Zperiods)r>   r   r   rf   û   s    zTestMethods.test_diff)r7   r8   r9   r"   rE   r#   rR   rS   r?   rU   rV   Úparametrizer   r   r[   r\   r]   r^   r_   ra   rc   re   rL   rf   r@   r   r   )r>   r   rQ   À   s   (*rQ   c                   sT   e Zd ZdZdZdZdZdd„ Zej	j
dƒdd„ ƒZ‡ fdd„Z‡ fd	d
„Z‡  ZS )ÚTestArithmeticsNc             C   s    t j|ƒ}| j|t|d d� d S )N)Úexc)r   r   Z_check_divmod_opÚdivmod)r=   r!   Úsr   r   r   Útest_divmod_series_array  s    
z(TestArithmetics.test_divmod_series_arrayzWe implement opsc             C   s   d S )Nr   )r=   r!   Úall_arithmetic_operatorsr   r   r   Ú
test_error  s    zTestArithmetics.test_errorc                s   t ƒ j||ƒ d S )N)r;   Útest_arith_series_with_scalar)r=   r!   rm   )r>   r   r   ro     s    z-TestArithmetics.test_arith_series_with_scalarc                s   t ƒ j||ƒ d S )N)r;   Útest_arith_series_with_array)r=   r!   rm   )r>   r   r   rp     s    z,TestArithmetics.test_arith_series_with_array)r7   r8   r9   Z
divmod_excZseries_scalar_excZframe_scalar_excZseries_array_excrl   r"   rE   r#   rn   ro   rp   r@   r   r   )r>   r   rh      s   rh   c               @   s   e Zd ZdS )ÚTestPrintingN)r7   r8   r9   r   r   r   r   rq     s   rq   c               @   s   e Zd Zdd„ ZdS )ÚTestNumericReducec             C   s:   t ||ƒ|d�}t |j|jjƒ|ƒ|d�}tj||ƒ d S )N)Úskipna)ÚgetattrZastyper   r    ÚtmZassert_almost_equal)r=   rk   Zop_namers   ÚresultÚexpectedr   r   r   Úcheck_reduce  s    zTestNumericReduce.check_reduceN)r7   r8   r9   rx   r   r   r   r   rr     s   rr   c               @   s   e Zd ZdS )ÚTestBooleanReduceN)r7   r8   r9   r   r   r   r   ry   #  s   ry   c                   sP   e Zd Ze‡ fdd„ƒZe‡ fdd„ƒZe‡ fdd„ƒZe‡ fdd„ƒZ‡  ZS )	ÚTestMissingc                s   t ƒ j|ƒ d S )N)r;   Útest_fillna_scalar)r=   r%   )r>   r   r   r{   )  s    zTestMissing.test_fillna_scalarc                s   t ƒ j||ƒ d S )N)r;   Útest_fillna_series_method)r=   r%   Zfillna_method)r>   r   r   r|   .  s    z%TestMissing.test_fillna_series_methodc                s   t ƒ j|ƒ d S )N)r;   Útest_fillna_series)r=   r%   )r>   r   r   r}   3  s    zTestMissing.test_fillna_seriesc                s   t ƒ j|ƒ d S )N)r;   Útest_fillna_frame)r=   r%   )r>   r   r   r~   8  s    zTestMissing.test_fillna_frame)	r7   r8   r9   r?   r{   r|   r}   r~   r@   r   r   )r>   r   rz   (  s   rz   c                   sh   e Zd Zejjdƒ‡ fdd„ƒZe‡ fdd„ƒZe‡ fdd„ƒZ	e‡ fdd	„ƒZ
e‡ fd
d„ƒZ‡  ZS )ÚTestReshapingzIncorrect parent testc                s   t ƒ j|ƒ d S )N)r;   Útest_concat_mixed_dtypes)r=   r!   )r>   r   r   r€   ?  s    z&TestReshaping.test_concat_mixed_dtypesc                s   t ƒ j||ƒ d S )N)r;   Ú
test_merge)r=   r!   r&   )r>   r   r   r�   D  s    zTestReshaping.test_mergec                s   t ƒ j|ƒ d S )N)r;   Útest_merge_on_extension_array)r=   r!   )r>   r   r   r‚   I  s    z+TestReshaping.test_merge_on_extension_arrayc                s   t ƒ j|ƒ d S )N)r;   Ú(test_merge_on_extension_array_duplicates)r=   r!   )r>   r   r   rƒ   N  s    z6TestReshaping.test_merge_on_extension_array_duplicatesc                s   t ƒ j|ƒ d S )N)r;   Útest_transpose)r=   r!   )r>   r   r   r„   S  s    zTestReshaping.test_transpose)r7   r8   r9   r"   rE   r#   r€   r?   r�   r‚   rƒ   r„   r@   r   r   )r>   r   r   >  s
   r   c                   s  e Zd Ze‡ fdd„ƒZe‡ fdd„ƒZe‡ fdd„ƒZe‡ fdd„ƒZe‡ fd	d
„ƒZe‡ fdd„ƒZ	e‡ fdd„ƒZ
e‡ fdd„ƒZeejjdddgƒ‡ fdd„ƒƒZe‡ fdd„ƒZeejjdejdddddgƒejdddddgdd�gddgd�‡ fd d!„ƒƒZe‡ fd"d#„ƒZeejjd$d%d&d'gejd%d&d'gd(d�ejd%d&d'gƒgd)d*dgd�‡ fd+d,„ƒƒZeejjd-d%d&d'ejgdfejd%d&d'ejgdejjd.�ejd%d&d'ejgd(d�dfejd%d&d'ejgd(d�dfgd/d0d1d2gd�‡ fd3d4„ƒƒZe‡ fd5d6„ƒZe‡ fd7d8„ƒZ‡  ZS )9ÚTestSetitemc                s   t ƒ j||ƒ d S )N)r;   Útest_setitem_scalar_series)r=   r!   Úbox_in_series)r>   r   r   r†   Y  s    z&TestSetitem.test_setitem_scalar_seriesc                s   t ƒ j||ƒ d S )N)r;   Útest_setitem_sequence)r=   r!   r‡   )r>   r   r   rˆ   ^  s    z!TestSetitem.test_setitem_sequencec                s   t ƒ j||ƒ d S )N)r;   Ú.test_setitem_sequence_mismatched_length_raises)r=   r!   Zas_array)r>   r   r   r‰   d  s    z:TestSetitem.test_setitem_sequence_mismatched_length_raisesc                s   t ƒ j||ƒ d S )N)r;   Ú test_setitem_sequence_broadcasts)r=   r!   r‡   )r>   r   r   rŠ   i  s    z,TestSetitem.test_setitem_sequence_broadcastsc                s   t ƒ j|ƒ d S )N)r;   Útest_setitem_loc_scalar_mixed)r=   r!   )r>   r   r   r‹   o  s    z)TestSetitem.test_setitem_loc_scalar_mixedc                s   t ƒ j|ƒ d S )N)r;   Ú,test_setitem_loc_scalar_multiple_homogoneous)r=   r!   )r>   r   r   rŒ   t  s    z8TestSetitem.test_setitem_loc_scalar_multiple_homogoneousc                s   t ƒ j|ƒ d S )N)r;   Útest_setitem_iloc_scalar_mixed)r=   r!   )r>   r   r   r�   y  s    z*TestSetitem.test_setitem_iloc_scalar_mixedc                s   t ƒ j|ƒ d S )N)r;   Ú-test_setitem_iloc_scalar_multiple_homogoneous)r=   r!   )r>   r   r   rŽ   ~  s    z9TestSetitem.test_setitem_iloc_scalar_multiple_homogoneousÚsetterÚlocNc                s   t ƒ j||ƒ d S )N)r;   Útest_setitem_mask_broadcast)r=   r!   r�   )r>   r   r   r‘   ƒ  s    z'TestSetitem.test_setitem_mask_broadcastc                s   t ƒ j|ƒ d S )N)r;   Ú&test_setitem_scalar_key_sequence_raise)r=   r!   )r>   r   r   r’   Š  s    z2TestSetitem.test_setitem_scalar_key_sequence_raiseÚmaskTFÚboolean)r   znumpy-arrayzboolean-array)Úidsc                s   t ƒ j|||ƒ d S )N)r;   Útest_setitem_mask)r=   r!   r“   r‡   )r>   r   r   r–   ’  s    
zTestSetitem.test_setitem_maskc                s   t ƒ j||ƒ d S )N)r;   Útest_setitem_mask_raises)r=   r!   r‡   )r>   r   r   r—   ž  s    z$TestSetitem.test_setitem_mask_raisesÚidxr   r   r,   ZInt64Úlistzinteger-arrayc                s   t ƒ j|||ƒ d S )N)r;   Útest_setitem_integer_array)r=   r!   r˜   r‡   )r>   r   r   rš   ¢  s    z&TestSetitem.test_setitem_integer_arrayzidx, box_in_series)Zmarksz
list-Falsez	list-Truezinteger-array-Falsezinteger-array-Truec                s   t ƒ j|||ƒ d S )N)r;   Ú(test_setitem_integer_with_missing_raises)r=   r!   r˜   r‡   )r>   r   r   r›   «  s    z4TestSetitem.test_setitem_integer_with_missing_raisesc                s   t ƒ j||ƒ d S )N)r;   Útest_setitem_slice)r=   r!   r‡   )r>   r   r   rœ   ¹  s    zTestSetitem.test_setitem_slicec                s   t ƒ j|ƒ d S )N)r;   Útest_setitem_loc_iloc_slice)r=   r!   )r>   r   r   r�   ½  s    z'TestSetitem.test_setitem_loc_iloc_slice)r7   r8   r9   r?   r†   rˆ   r‰   rŠ   r‹   rŒ   r�   rŽ   r"   rE   rg   r‘   r’   r
   r   r   r–   r—   rš   ZNAr   rL   r›   rœ   r�   r@   r   r   )r>   r   r…   X  sB    *r…   c               @   s   e Zd ZdS )ÚTestParsingN)r7   r8   r9   r   r   r   r   rž   Â  s   rž   )<Znumpyr
   r"   Zpandas.compat.numpyr   Zpandasr   Zpandas._testingZ_testingru   Zpandas.core.arrays.numpy_r   r   Ú r   Zfixturer   r   r!   r%   r&   r+   r.   r/   r4   r5   rE   Zusefixturesr?   r6   ZBaseCastingTestsr:   ZBaseConstructorsTestsrA   ZBaseDtypeTestsrF   ZBaseGetitemTestsrH   ZBaseGroupbyTestsrM   ZBaseInterfaceTestsrO   ZBaseMethodsTestsrQ   ZBaseArithmeticOpsTestsrh   ZBasePrintingTestsrq   ZBaseNumericReduceTestsrr   ZBaseBooleanReduceTestsry   ZBaseMissingTestsrz   ZBaseReshapingTestsr   ZBaseSetitemTestsr…   ZBaseParsingTestsrž   r   r   r   r   Ú<module>   sJ   
@j