U
    >vf#                     @   sp   d Z ddlZddlZddlZddlmZ ddl	m
Z
mZ ddlmZmZ ejdddZejjG dd	 d	ZdS )
z test feather-format compat     N)ArrowStringArrayStringArray)read_feather
to_featherpyarrowz1.0.1)Z
minversionc                   @   s   e Zd Zdd Zdd Zdi 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dd Zdd Zdd Zdd Zdd Zejjejd d!d"d#d$ Zd%d& Zd'd( ZdS ))TestFeatherc              
   C   s<   t j||d$ t }t|| W 5 Q R X W 5 Q R X d S )Nmatch)pytestraisestmensure_cleanr   )selfdfexcerr_msgpath r   @/tmp/pip-unpacked-wheel-vdrwu74i/pandas/tests/io/test_feather.pycheck_error_on_write   s    
z TestFeather.check_error_on_writec              
   C   s8   t t$ t  }t|| W 5 Q R X W 5 Q R X d S N)r   Zexternal_error_raised	Exceptionr   r   )r   r   r   r   r   r   check_external_error_on_write   s    
z)TestFeather.check_external_error_on_writeNc              	   K   sJ   |d kr|}t  ,}t||f| t|f|}t || W 5 Q R X d S r   )r   r   r   r   assert_frame_equal)r   r   expectedwrite_kwargsZread_kwargsr   resultr   r   r   check_round_trip#   s    
zTestFeather.check_round_tripc              	   C   sH   d}t dddgddt dtdddgfD ]}| |t| q0d S )Nz'feather only support IO with DataFrames         foo20130101)pdSeries	Timestampnparrayr   
ValueError)r   msgobjr   r   r   
test_error-   s    zTestFeather.test_errorc                 C   s(  t tdttddtdddtjddd	d
dtjdgdddgdtjdgt tdt j	tt j
dddd dt j	tt j
ddddd dt dt jt dgt j	tt j
ddddd dd}t jdddd|d< t jddd|d< t ddd|d< |jjjjdkst| | d S )Nabcr      r       u1g      @g      @Zfloat64dtypeg      ?TFr"   periods)freqz
US/Eastern)r3   tzZ20130103ns)r3   r4   )stringintZuintfloatZfloat_with_nullboolZbool_with_nullcatdtdttzZdt_with_nullZdtnsZ2013M)r4   r3   r3   z1 dayZ
timedeltasr   Z	intervals)r#   	DataFramelistranger&   arangeZastypenanZCategoricalZDatetimeIndex
date_ranger%   ZNaTZperiod_rangeZtimedelta_rangeZinterval_ranger=   r1   r5   zoneAssertionErrorr   r   r   r   r   r   
test_basic8   s@    

  zTestFeather.test_basicc                 C   s2   t jtdddtdd }| | d S )N   r-   r    Zaaacolumns)r#   r?   r&   rB   reshaper@   copyr   rG   r   r   r   test_duplicate_columns[   s    $z"TestFeather.test_duplicate_columnsc                 C   s2   t tddd }d}| |t| d S )NrI   r-   r    %feather must have string column names)r#   r?   r&   rB   rL   rM   r   r(   )r   r   r)   r   r   r   test_stringify_columnsa   s    z"TestFeather.test_stringify_columnsc              	   C   sP   t tdttddtdttddd}ddg}| j||| |d	 d S )
Nr,   r   r-   Zxyz   )col1Zcol2col3Zcol4rR   rS   )r   rK   )r#   r?   r@   rA   r   )r   r   rK   r   r   r   test_read_columnsf   s    zTestFeather.test_read_columnsc                 C   sB   t ddgddgddgd}|dd	g }| j||dd	gd
 d S )Nr   r   xyTF)ABCrX   rW   rJ   )r#   r?   r   )r   r   r   r   r   r   !test_read_columns_different_orders   s    z-TestFeather.test_read_columns_different_orderc                 C   s"   t ddddgi}| | d S )Nar          @)r#   r?   r   rG   r   r   r   test_unsupported_othery   s    z"TestFeather.test_unsupported_otherc                 C   s4   t dtdi}| j|dd | j|dd d S )NrW   i T)Zuse_threadsF)r#   r?   r&   rB   r   rG   r   r   r   test_rw_use_threads~   s    zTestFeather.test_rw_use_threadsc              	   C   s   t ddddgi}| | d}dddgt jdddtd	dddgt jd
ddgfD ]}||_| |t	| qXdddg|_d|j_
d}| |t	| dddg|_t jd
g|_d}| |t	| d S )NrW   r   r   r    zsfeather does not support serializing .* for the index; you can \.reset_index\(\) to make the index into column\(s\)r-   r"   r2   r,   )r[   r   )r[   r   )br   r   r!   z=feather does not serialize index meta-data on a default indexrO   )r#   r?   r   rD   r@   Z
MultiIndexfrom_tuplesindexr   r(   namerK   )r   r   r)   ra   r   r   r   test_write_with_index   s(    
z!TestFeather.test_write_with_indexc                 C   s*   t   }t |jt}t || d S r   )r   makeDataFramereset_indexZround_trip_pathlibr   r   r   r   r   r   r   r   r   test_path_pathlib   s    zTestFeather.test_path_pathlibc                 C   s*   t   }t |jt}t || d S r   )r   rd   re   Zround_trip_localpathr   r   r   rf   r   r   r   test_path_localpath   s    zTestFeather.test_path_localpathc                 C   s"   t   }| j|ddid d S )Nversionr   )r   )r   rd   re   r   rG   r   r   r   test_passthrough_keywords   s    z%TestFeather.test_passthrough_keywordskhttps://raw.githubusercontent.com/pandas-dev/pandas/main/pandas/tests/io/data/feather/feather-0_3_1.featherT)urlZcheck_before_testc                 C   s$   d}t |}t |}t|| d S )Nrk   )r   r   r   )r   Zfeather_filerl   r   resr   r   r   test_http_path   s
    zTestFeather.test_http_pathc                    s  t dttjdtjdgddtjdddgddtjdtjdgd	dtjdd
dgd	dddd gdddgdddgddd gd}|dkrttjdddgtj	d}ttjddtj
gtj	d}n(tdddg}tddd g}t 4}t|| td| t||d}W 5 Q R X W 5 Q R X ttjdtjdgddtjdddgddtjdtjdgd	dtjdd
dgd	dtjddtj
gddtjdddgdd||d|dkrddlm  t fddjD t| d S )Nr   r   r    ZInt64r0   r   g      ?g      @ZFloat64r\   TFr[   r_   c)r[   r_   ro   defghpythonzmode.string_storagedtype_backendbooleanr   )ArrowExtensionArrayc                    s$   i | ]}| j | d dqS )T)Zfrom_pandas)r'   ).0colry   r   par   r   
<dictcomp>   s    z?TestFeather.test_read_feather_dtype_backend.<locals>.<dictcomp>)r
   importorskipr#   r?   r$   r&   rC   r   r'   Zobject_ZNAr   r   r   r   Zoption_contextr   Zpandas.arraysry   rK   r   )r   Zstring_storagerw   r   Zstring_arrayZstring_array_nar   r   r   r|   r   test_read_feather_dtype_backend   sP    


 
z+TestFeather.test_read_feather_dtype_backendc              
   C   sf   d}t dttddi}td6}|| tjt	|d t
|dd W 5 Q R X W 5 Q R X d S )	NzPdtype_backend numpy is invalid, only 'numpy_nullable' and 'pyarrow' are allowed.r8   r   r-   ztmp.featherr   numpyrv   )r#   r?   r@   rA   r   r   r   r
   r   r(   r   )r   r)   r   r   r   r   r   test_invalid_dtype_backend   s    
z&TestFeather.test_invalid_dtype_backend)__name__
__module____qualname__r   r   r   r+   rH   rN   rP   rT   rZ   r]   r^   rc   rg   rh   rj   r
   marknetworkr   rn   r   r   r   r   r   r   r      s.   
#
6r   )__doc__r   r&   r
   Zpandasr#   Zpandas._testingZ_testingr   Zpandas.core.arraysr   r   Zpandas.io.feather_formatr   r   r   r   r   Z
single_cpur   r   r   r   r   <module>   s   