a
    Pf0@                     @   s   d dl m Z mZ d dlZd dlZd dlZd dl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mZ d dlmZ d dlmZ ejZG dd dZdd	 Zd
d ZdS )    )datetime	timedeltaN)PerformanceWarning)Categorical	DataFrameSeries	Timestamp
date_rangeoption_context)
NumpyBlockc                   @   s   e Z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ejddd Zd d! Zd"d# Zd$S )%TestDataFrameBlockInternalsc                 C   sx   t dddd}|d }td|i}|d jjd u s6J tj|jd< |d jjd u sVJ |jdksdJ |d |kstJ d S )	N20130101   
US/Eastern)periodstz   B)r   r   D)r	   r   Z_valuesfreqpdZNaTZiloc)selfZdtitsdf r   `/var/www/ai-form-bot/venv/lib/python3.9/site-packages/pandas/tests/frame/test_block_internals.py,test_setitem_invalidates_datetime_index_freq#   s    zHTestDataFrameBlockInternals.test_setitem_invalidates_datetime_index_freqc                 C   s   d}t jt|dd t|jtd}W d    n1 s80    Y  t|jtd}t || t jt|dd  t|jtj	d}W d    n1 s0    Y  t|jtj	d}t || d S )Nz#Passing a BlockManager to DataFrameF)matchZcheck_stackleveldtype)
tmassert_produces_warningDeprecationWarningr   _mgrintZ_seriesassert_frame_equalnpint32)r   float_framemsgZcastedexpectedr   r   r   test_cast_internals4   s    ,.z/TestDataFrameBlockInternals.test_cast_internalsc                 C   s   d|d< |  }t|jjdks$J |  }||us8J t|| d|d< t|jjdks`J | }|d u stJ t|jjdksJ d S )N      @Er   g       @Fr   )Z_consolidatelenr#   blocksr    r%   Z_consolidate_inplace)r   r(   ZconsolidatedZreconsZreturn_valuer   r   r   test_consolidateD   s    z,TestDataFrameBlockInternals.test_consolidatec                 C   s,   t tdtdD ]}t||t|< qd S )NAZ)rangeordchr)r   r(   letterr   r   r   test_consolidate_inplaceU   s    z4TestDataFrameBlockInternals.test_consolidate_inplacec                 C   s   |rVt jtdd d|jd< W d    n1 s20    Y  |jd dk sRJ d S d|jd< |jd dk svJ d|d< |d }d|jd< |jd dk rJ |dk sJ d S )Nz	read-onlyr      r,   r-         )pytestraises
ValueErrorvaluesall)r   r(   using_copy_on_writecolr   r   r   test_modify_valuesZ   s    (

z.TestDataFrameBlockInternals.test_modify_valuesc                 C   s<   d|d< |j  }d||dk< d||dk< t||j  d S )Nr,   r-      r   )r@   copyr    Zassert_almost_equal)r   r(   r*   r   r   r   test_boolean_set_unconsn   s
    
z3TestDataFrameBlockInternals.test_boolean_set_unconsc              	   C   s  t ddgi}|d }ttdgtjdd}t|| t ddgi}|d }ttdgtjdd}t|| t dtddddgi}|d }tttddddgtj	dd}t|| t dd dgi}|d }tttj
dgtjdd}t|| t ddd	gi}|d }ttdd	gtjdd}t|| t dd
dgi}|d }ttd
dgtjdd}t|| t dd
dgi}|d }ttd
dgtjdd}t|| t dd
dgi}|d }ttd
dgtj	dd}t|| t ddd gi}|d }ttdtj
gtjdd}t|| t dd
d gi}|d }ttd
tj
gtjdd}t|| t dg di}|d }ttg dtj	dd}t|| t dddtdddd gi}|d }ttddtdddd gtj	dd}t|| d S )Nr2   l    )namel            i  r   Tg      ?rE   y      ?       @r   g      @)       @r   TNrI   i  )r   r   r&   Zasarrayint64r    assert_series_equalZuint64r   Zobject_nanfloat64Z
complex128)r   r   resultr*   r   r   r   test_constructor_with_convertw   sh    z9TestDataFrameBlockInternals.test_constructor_with_convertc                 C   s   t dddtjt dddgt dddt dddt dddgg}t|}|j}tddi}t  |d< tddd	|d
< |d jdksJ |d
 jdksJ |j}ttdgd |stdndtdtdg t	dg d d}t
|| d S )N  r   r:   rE   i  r   zdatetime64[us]r   )dayssecondsr   zM8[us]zm8[us]rM      objectstringztimedelta64[us]ZABCD)Zfoor   r   index)r   r&   rL   r   dtypesr   nowr   r   listr    rK   )r   float_string_frameZusing_infer_stringdatar   rN   r*   r   r   r   test_construction_with_mixed   s*     	z8TestDataFrameBlockInternals.test_construction_with_mixedc                 C   s   t jg ddd}ttdd}||d< tdtjdddd	itdd}t|d  | tt	d
t
d
ddddtdd}|jd dksJ |jd dksJ ttdd}t d|d< t jg ddd|d< t|| d S )Nr   rE   r   ztimedelta64[s]r   r   rV   r2   z00:00:01s)r   r   r   )r   zM8[s])dt1dt2r`   ra   
2013-01-01)rb   z
2013-01-02z
2013-01-03zdatetime64[D])r&   arrayr   r4   r   Ztimedelta_ranger    Zassert_numpy_array_equalZto_numpyr   r	   astyperX   Z
datetime64r%   )r   arrr   r*   r   r   r   "test_construction_with_conversions   s*    	
z>TestDataFrameBlockInternals.test_construction_with_conversionsc                 C   s   dd }d}t jt|d |g d W d    n1 s<0    Y  t jtdd |d W d    n1 sr0    Y  t jtdd |d W d    n1 s0    Y  d	}t jt|d |d
 W d    n1 s0    Y  d S )Nc                 S   s0   t ttdddddfd}t|g d| dS )NrP   r   Zaa   	   )r2   r   C)r\   columnsr   )rZ   	itertoolsrepeatr   r   )r   r\   r   r   r   f   s    zGTestDataFrameBlockInternals.test_constructor_compound_dtypes.<locals>.fz@compound dtypes are not implemented in the DataFrame constructorr9   ))r2   zdatetime64[h])r   str)ri   r'   zargument must berJ   rM   zD^Unknown datetime string format, unable to parse: aa, at position 0$zM8[ns])r=   r>   NotImplementedError	TypeErrorr?   )r   rm   r)   r   r   r    test_constructor_compound_dtypes   s    *&&z<TestDataFrameBlockInternals.test_constructor_compound_dtypesc                 C   sP   t  }t|}t|| |jj t|}t| t|}t|| d S N)r   r    Zround_trip_pickler%   r#   ndimrepr)r   r[   Ztimezone_frameZempty_frameZ	unpickledr   r   r   test_pickle  s    


z'TestDataFrameBlockInternals.test_picklec                 C   s   t tg dtg dg dd}|j}|j|_|d}|d}d|j_|j	}|j|_|d}|d}d|j_|j|_|j|_	t
t|j|j t
t|j	|j d S )	N)z2012-06-21 00:002012-06-23 07:002012-06-23 16:302012-06-25 08:002012-06-26 12:00)rv   rw   rx   ry   z2012-06-27 08:00)M   A   rz   r   rz   )startingendingmeasurer   UTCr|   r}   )r   r   to_datetimer|   r@   rW   Ztz_localizeZ
tz_convertrH   r}   r    Zassert_index_equalZDatetimeIndex)r   r   Zser_startingZ
ser_endingr   r   r   test_consolidate_datetime64$  s0    		



z7TestDataFrameBlockInternals.test_consolidate_datetime64c                 C   s   |j r
J |j sJ d S rr   )Z_is_mixed_type)r   r(   r[   r   r   r   test_is_mixed_typeQ  s    
z.TestDataFrameBlockInternals.test_is_mixed_typec              	   C   s   t dd  ttjddddd}t| |d d|d< t  tj	|d	 d
< W d    n1 sn0    Y  t| |
  |d	 
  |rt|d	 d
 rJ nt|d	 d
 sJ W d    n1 s0    Y  d S )NZchained_assignmentrE   )rS   rS   abcd)erm   gh)rW   rj   r   rT   r   r   )r
   r   r&   randomZdefault_rngrt   rd   r    raises_chained_assignment_errorrL   sumr   isna)r   rB   Zwarn_copy_on_writeYr   r   r    test_stale_cached_series_bug_473U  s     
,z<TestDataFrameBlockInternals.test_stale_cached_series_bug_473z.ignore:Setting a value on a view:FutureWarningc           
      C   s  t ddgd}tj|d< i }tjtddn t|jD ]R\}}tddD ]>}||vrfd||< tj||< |rz||j	||f< qH||| |< qHq6W d    n1 s0    Y  d}t
|j	t|| |gf }t
|j	t|| |gf }	||	  krdksn J d S )Nr   r   rV   F)Zraise_on_extra_warningsd      )r   r&   rL   r    r!   r   	enumeraterW   r4   locr/   r   r   )
r   rB   r   ZwasColidtrC   Zmyidfirstsecondr   r   r   $test_strange_column_corruption_issuei  s$    

.z@TestDataFrameBlockInternals.test_strange_column_corruption_issuec                 C   sb   t g dj}td|i}tdg di}t|| t|jjd tsLJ |jjd j	s^J d S )Nr^   r2   r   )
r   rc   r   r    r%   
isinstancer#   r0   r   Z
is_numeric)r   re   rN   r*   r   r   r    test_constructor_no_pandas_array  s    z<TestDataFrameBlockInternals.test_constructor_no_pandas_arrayc              	   C   s   t g dg dd}tjtjg dtd|d< t g dg dtjtjg dtdd}t|d jj	d t
ks~J |d jj	d jsJ t|d jj	d t
ksJ |d jj	d jsJ t|| d S )	N)r   rE   r   rS   r   )r   r   )r   rE   Nr   r   r   )r   r   r   r   )r   r   ZarraysZNumpyExtensionArrayr&   rc   rT   typer#   r0   r   Z	is_objectr    r%   )r   r   Zdf2r   r   r   !test_add_column_with_pandas_array  s    	z=TestDataFrameBlockInternals.test_add_column_with_pandas_arrayN)__name__
__module____qualname__r   r+   r1   r8   rD   rG   rO   r]   rf   rq   ru   r   r   r   r=   markfilterwarningsr   r   r   r   r   r   r   r   "   s$   	C#-


r   c                 C   s   t dtg dddi}| rXt " |d jddd W d    q1 sL0    Y  n@tjtdd	" |d jddd W d    n1 s0    Y  t|jj	d
 j
tsJ | s|   d
ksJ d S )Nr   )r   rE   Ncategoryr   r   T)Zinplacezinplace methodr9   r   )r   r   r    r   Zfillnar!   FutureWarningr   r#   r0   r@   r   Zisnullr   )rB   r   r   r   r   +test_update_inplace_sets_valid_block_values  s    
20r   c                  C   s   t  } tdgtd| d< tdgtd| d< | d dk | | d dk  d| jd< t dgdgdtd}t| | | jd dksJ d S )	Nr   r   col1r   col2r2   )r   r   )r   r   )r   r   rT   atr    r%   )r   r*   r   r   r   $test_nonconsolidated_item_cache_take  s    
r   )r   r   rk   numpyr&   r=   Zpandas.errorsr   Zpandas.util._test_decoratorsutilZ_test_decoratorstdZpandasr   r   r   r   r   r	   r
   Zpandas._testingZ_testingr    Zpandas.core.internals.blocksr   Zskip_array_manager_invalid_testZ
pytestmarkr   r   r   r   r   r   r   <module>   s        