o
    թZh0@                     @   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 sJ tj|jd< |d jjd u s+J |jdks2J |d |ks:J 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/html/lang_env/lib/python3.10/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 sw   Y  t|jtd}t || t jt|dd t|jtj	d}W d    n1 sIw   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sJ t|| d|d< t|jjdks0J | }|d u s:J t|jjdksDJ d S )N      @Er   g       @Fr   )Z_consolidatelenr#   blocksr    r%   Z_consolidate_inplace)r   r(   ZconsolidatedZrecons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   |r+t jtdd d|jd< W d    n1 sw   Y  |jd dk s)J d S d|jd< |jd dk s;J d|d< |d }d|jd< |jd dk rSJ |dk s[J 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   )rA   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 )Nr3   l    )namel            i  r   Tg      ?rF   y      ?       @r   g      @)       @r   TNrJ   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sCJ |d
 jdksLJ |j}ttdgd |s^tdndtdtdg t	dg d d}t
|| d S )N  r   r;   rF   i  r   zdatetime64[us]r   )dayssecondsr   zM8[us]zm8[us]rN      objectstringztimedelta64[us]ZABCD)Zfoor   r   index)r   r&   rM   r   dtypesr   nowr   r   listr    rL   )r   float_string_frameZusing_infer_stringdatar   rO   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sKJ |jd dksTJ ttdd}t d|d< t jg ddd|d< t|| d S )Nr   rF   r   ztimedelta64[s]r   r   rW   r3   z00:00:01s)r   r   r   )r   zM8[s])dt1dt2ra   rb   
2013-01-01)rc   z
2013-01-02z
2013-01-03zdatetime64[D])r&   arrayr   r5   r   Ztimedelta_ranger    Zassert_numpy_array_equalZto_numpyr   r	   astyperY   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w   Y  t jtdd |d W d    n1 s9w   Y  t jtdd |d W d    n1 sTw   Y  d	}t jt|d |d
 W d    d S 1 srw   Y  d S )Nc                 S   s0   t ttdddddfd}t|g d| dS )NrQ   r   Zaa   	   )r3   r   C)r]   columnsr   )r[   	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 constructorr:   ))r3   zdatetime64[h])r   str)rj   r'   zargument must berK   rN   zD^Unknown datetime string format, unable to parse: aa, at position 0$zM8[ns])r>   r?   NotImplementedError	TypeErrorr@   )r   rn   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)rw   rx   ry   rz   z2012-06-27 08:00)M   A   r{   r   r{   )startingendingmeasurer   UTCr}   r~   )r   r   to_datetimer}   rA   rX   Ztz_localizeZ
tz_convertrI   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 rs   )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 n ttjddddd}t| |d d|d< t  tj	|d	 d
< W d    n1 s7w   Y  t| |
  |d	 
  |rXt|d	 d
 rWJ nt|d	 d
 scJ W d    d S W d    d S 1 svw   Y  d S )NZchained_assignmentrF   )rT   rT   abcd)ern   gh)rX   rk   r   rU   r   r   )r
   r   r&   randomZdefault_rngru   re   r    raises_chained_assignment_errorrM   sumr   isna)r   rC   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7 t|jD ])\}}tddD ]}||vr3d||< tj||< |r=||j	||f< q$||| |< q$qW d    n1 sOw   Y  d}t
|j	t|| |gf }t
|j	t|| |gf }	||	  krdksJ  J d S )Nr   r   rW   F)Zraise_on_extra_warningsd      )r   r&   rM   r    r!   r   	enumeraterX   r5   locr/   r   r   )
r   rC   r   ZwasColidtrD   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s&J |jjd j	s/J d S )Nr_   r3   r   )
r   rd   r   r    r%   
isinstancer#   r0   r   Z
is_numeric)r   rf   rO   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sJJ t|d jj	d t
ksXJ |d jj	d jscJ t|| d S )	N)r   rF   r   rT   r   )r   r   )r   rF   Nr   r   r   )r   r   r   r   )r   r   ZarraysZNumpyExtensionArrayr&   rd   rU   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+   r2   r9   rE   rH   rP   r^   rg   rr   rv   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}| r,t  |d jddd W d    n1 s&w   Y  n tjtdd	 |d jddd W d    n1 sGw   Y  t|jj	d
 j
tsXJ | sf|   d
kshJ d S d S )Nr   )r   rF   Ncategoryr   r   T)Zinplacezinplace methodr:   r   )r   r   r    r   Zfillnar!   FutureWarningr   r#   r0   rA   r   Zisnullr   )rC   r   r   r   r   +test_update_inplace_sets_valid_block_values  s   
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sBJ d S )	Nr   r   col1r   col2r3   )r   r   )r   r   )r   r   rU   atr    r%   )r   r*   r   r   r   $test_nonconsolidated_item_cache_take  s   
r   )r   r   rl   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"        