K iH ddlmZddlZddlmZddlZ ddlZddl m Z ddl mZmZmZddlmZ ddlZej*j-dgdej*j-dgd d Zej*j-d d gd dgdfgddfgddfgdZdZej*j&ej*j-dej4ej6ej8gej*j-dej:ej<ej>ej@gej*j-dejBdfejDdfgdZ#ej*j&dZ$ej*j&dZ%ej*j&dZ&ej*j&dZ'ej*j&ej*j-dgddZ(ej*j&ej*j-d ddgd!Z)ej*j&d"Z*ej*jej*j-dej4ej6ej8gej*j-dej:ej<ej>ej@gej*j-dejVd#fejBdfejDdfgej*j-dgdej*j-dgd$ej*j-d%gd&d'Z,ej*j-d%gd(d)Z-ej*j\d*Z/d+Z0ej*j&d,Z1ej*j&d-Z2d.Z3y#e $rdZYwxYw#e $rYwxYw)/datetimeN)Version)_PyArrowColumnColumnNullType DtypeKind)_from_dataframeunit)smsusnstz)America/New_York+07:30-04:30c tdddtddddg}tjdtj|tj||i}|j j d}|jdk(sJ|jd k(sJ|jd k(sJ|jd tjk(sJ|jtjd fk(sJy) N Artyper)dtpatablearray timestamp __dataframe__get_column_by_namesizeoffset null_countdtyperDATETIME describe_nullr USE_BITMASK)r rdt_arrr!cols o/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/pyarrow/tests/interchange/test_conversion.py test_datetimer0+sq"or$2 5F HHc288Fdr1JKL ME     2 23 7C 88:?? ::?? >>Q   99Q<9-- -- -   !;!;Q ? ?? ? test_datakindfoobar)g?g@g @rr7rctj|}t|}|j|k(sJ|j t |k(sJ|j d|k(sJ|jdk(sJ|jdk(sJ|jdJt t|jdk(sJ|jD] }||k(r Jy)Nrrvalidity) r r"r_colr&lenr) num_chunksr( get_bufferslist get_chunks)r2r3arr arr_columnchunks r/test_array_to_pyarrowcolumnrE9s ((9 C$J ??c !! ! ?? I .. .   A $ && &  "a '' '  A %% %  ! ! #J / 77 7 tJ))+, - 22 2&&(# """#r1ctjgd}|jdd}tj|gdg}tj|gdg}|j j d}|j dk(sJt|j }|j|sJ|j|rJy)Nr8r7rBnames arr_slicedr) r r"slicer!r$ get_columnr'r equals)rBrIr! table_slicedr.results r/test_offset_of_sliced_arrayrOQs ((< C1aJ HHcU5' *E88ZL?L  $ $ & 1 1! 4C ::?? \779 :F   v && &||F## ##r1uintintzfloat, np_float_strfloat32float64c Jttjtdkrtjdgd}t j t j||t j||t jtj|tj||gdd}dd l m }||}tj|}|j|sJ|j} |j} | j| jk(sJ| j!| j!k(sJ| j#| j#k(sJ| j%| j%k(sJy) N1.5.0&__dataframe__ added to pandas in 1.5.0)rr7rrr)TFT)abcdrfrom_dataframe)rpd __version__pytestskipr r!r"npr)pandas.api.interchanger^pirLr$ num_columnsnum_rowsr> column_names) rPrQfloat np_float_strrBr!pandas_from_dataframe pandas_dfrNtable_protocolresult_protocols r/test_pandas_roundtriprojsOr~~!11 <= C HH#D)#C("((3bhh|.DEER$   E&e,I   y )F <<  ((*N**,O  % % '?+F+F+H HH H  " " $(@(@(B BB B  $ $ &/*D*D*F FF F  & & (O,H,H,J JJ Jr1c|ttjtdkrtjdgd}t j dt j|i}ddlm }||}tj|}|dj|djk(sJt jj|djsJt jj|djsJ|j!}|j!}|j#|j#k(sJ|j%|j%k(sJ|j'|j'k(sJ|j)|j)k(sJy)N1.6Column.size() bug in pandasrYrr[rYrr])rr_r`rarbr r!r"rdr^re to_pylisttypes is_stringris_large_stringr$rfrgr>rhrBr!rkrlrNrmrns r/test_pandas_roundtrip_stringrysmr~~/ 12 C HHc288C=) *E&e,I   y )F #; "eCj&:&:&< << < 88  eCjoo .. . 88 # #F3K$4$4 55 5((*N**,O  % % '?+F+F+H HH H  " " $(@(@(B BB B  $ $ &/*D*D*F FF F  & & (O,H,H,J JJ Jr1c^ttjtdkrtjdgd}t j dt j|t ji}ddl m }ttjtdk\rO||}tj|}|dj|djk(sJt jj|djsJt jj|djsJ|j!}|j!}|j#|j#k(sJ|j%|j%k(sJ|j'|j'k(sJ|j)|j)k(sJytj*t,5||dddy#1swYyxYw) Nrqrrrsa_largerrr]z2.0.1)rr_r`rarbr r!r" large_stringrdr^rertrurwrr$rfrgr>rhraisesAssertionErrorrxs r/"test_pandas_roundtrip_large_stringrsr~~/ 12 C HHi#BOO4E!FG HEr~~''"22)%0 ""9-i **,i0@0J0J0LLLLxx''i(8(=(=>>>xx''y(9(>(>???,,. ..0))+/J/J/LLLL&&(O,D,D,FFFF((*o.H.H.JJJJ**,0L0L0NNNN ]]> * ) !% ( ) ) )s  H##H,c8ttjtdkrtjdgd}t j t j|t j|t jd}ddl m }ttjtdk\r(||}tj|}|d j|d jk(sJt jj|d jsJt jj!|d jsJ|d j|d jk(sJt jj!|d jsJt jj!|d jsJytj"t$5||dddy#1swYyxYw) Nrqrr)rYrr[Nr)rYr{rr]2.0.2rYr{)rr_r`rarbr r!r"r|rdr^rertrurvrrwr}NotImplementedError)rBr!rkrlrNs r/)test_pandas_roundtrip_string_with_missingrsr~~/ 12 C HH288C=!##BOO4E!FH IEr~~''"22)%0 ""9-c{$$&%**>*>*@@@@xx!!%*//222xx''s (8(8999i **,i0@0J0J0LLLLxx''i(8(=(=>>>xx''y(9(>(>???]]. / ) !% ( ) ) )s > HHcttjtdkrtjdgd}t j dt j|ji}ddl m }||}tj|}|dj|djk(sJt jj|djsJt jj|djsJt jj!|dj#dj$jsJt jj'|dj#dj$jsJt jj)|dj#dj*jsJt jj-|dj#dj*jsJ|j/}|j/}|j1|j1k(sJ|j3|j3k(sJ|j5|j5k(sJ|j7|j7k(sJ|j9d}|j9d}|j:dt<j>k(sJ|j:d|j:dk(sJ|jA|jAk(sJ|jB|jBk(sJ|jD} |jD} | d| dk(sJ| d| dk(sJtG| d jHt jJsJy) Nrz;Bitmasks not supported in pandas interchange implementation) MonTuerWedrThuFriSatNweekdayrr] is_ordered is_dictionary categories)&rr_r`rarbr r!r"dictionary_encoderdr^rertrurrrvrD dictionaryrwis_int32indicesis_int8r$rfrgr>rhrKr)r CATEGORICALr&r'describe_categorical isinstancer<Array) rBr!rkrlrNrmrn col_table col_resultdesc_cat_tabledesc_cat_results r/!test_pandas_roundtrip_categoricalrsJr~~!11 QR HC HH BHHSM3356 E&e,I   y )F )  & & (E),<,F,F,H HH H 88 ! !% "2"7"7 88 8 88 ! !&"3"8"8 99 9 88  eI.44Q7BBGG HH H 88 # #F9$5$;$;A$>$I$I$N$N OO O 88  U9-33A6>>CC DD D 88  F9-33A6>>CC DD D((*N**,O  % % '?+F+F+H HH H  " " $(@(@(B BB B  $ $ &/*D*D*F FF F  & & (O,H,H,J JJ J))!,I ++A.J   A )"7"7 77 7   A )//!"4 44 4 ??   0 00 0    0 0 00 044N 55O , '?<+H HH H / *oo.N NN N ol388"(( CC Cr1c ttjtdkrtjdddlm}|ddd|ddd|ddd g}t jd t j|t j| i}ttjtd kr@t jd t j|t jd  i}n|}ddl m }||}tj|}|j|sJ|j}|j} |j| jk(sJ|j!| j!k(sJ|j#| j#k(sJ|j%| j%k(sJy)NrUrVrrrrrrrYrrqrr])rr_r`rarbrr r!r"r#rdr^rerLr$rfrgr>rh) r rr-r!expectedrkrlrNexpected_protocolrns r/test_pandas_roundtrip_datetimer'sr~~!11 <='q"or$24B @F HHc288Fd1CDE FEr~~/88S"((6 T8J"KLM&e,I   y )F ??6 "" " ..0**,O  ( ( *o.I.I.K KK K  % % '?+C+C+E EE E  ' ' )_-G-G-I II I  ) ) +/K/K/M MM Mr1rjc xttjtdkrtjdt j dt jdgt j|}dtdddtddd g}tj|t j |d d }tjtj |d tj |tjdd }tj|}|j!|sJy)NrUrVrr7rWrrrrzdatetime64[ns])rYrT) from_pandasrr)rr_r`rarbrcr"nanr)r DataFramer r!r#rer^rL)rjnp_arraydatetime_arraydfrrNs r/#test_pandas_to_pyarrow_with_missingrKs r~~!11 <=xxBFFAbhh|.DEHBtQORa_=N  hh~-=>  B xx XXhD 1hh~BLL,>?H  r "F == "" "r1cttjtdkrtjdt j dt jdgt j}tjd|i}tjt5tj|dddy#1swYyxYw)NrUrVrr7rWrY)rr_r`rarbrcr"rfloat16rr}rrer^)rrs r/+test_pandas_to_pyarrow_float16_with_missingrdsr~~!11 <= xxBFFAbjj9H sHo &B * + "s !CC r)rrrzoffset, length))rrrr7)rr7r7rcddlm}gd} |dddd|dddg} tjtj| |tj| |tjt j| t j | |d gd gd gdtj| tj ||d} | j||} t| j} | j| sJ| j} | j}| j|jk(sJ| j|jk(sJ| j|jk(sJ| j|jk(sJy)Nrr)rr7NrrrrrrWT)rrrX)TFN)rYNr[r)rYrZr[r\efg)rr r!r"rcr)r#rJr r$rLrfrgr>rh)rPrQrirjr rr'lengthrrBr-r!rNrmrns r/test_pyarrow_roundtriprsss(( Cq"otRa_ 5F HH#D)#C("((3bhh|.DE$$8$$!&r||DR'@A  E KK 'E U002 3F <<  ((*N**,O  % % '?+F+F+H HH H  " " $(@(@(B BB B  $ $ &/*D*D*F FF F  & & (O,H,H,J JJ Jr1))r r)rrrcLgd}tjdtj|ji}|j ||}t |j }|j|sJ|j }|j }|j|jk(sJ|j|jk(sJ|j|jk(sJ|j|jk(sJ|jd}|jd}|jdtjk(sJ|jd|jdk(sJ|j!|j!k(sJ|j"|j"k(sJ|j$} |j$} | d| dk(sJ| d| dk(sJt'| dj(tj*sJy)N) rrrrrrrNSunrrrrr)r r!r"rrJr r$rLrfrgr>rhrKr)rrr&r'rrr<r) r'rrBr!rNrmrnrrrrs r/"test_pyarrow_roundtrip_categoricalrs HC HH BHHSM3356 E KK 'E U002 3F <<  ((*N**,O  % % '?+F+F+H HH H  " " $(@(@(B BB B  $ $ &/*D*D*F FF F  & & (O,H,H,J JJ J))!,I ++A.J   A )"7"7 77 7   A )//!"4 44 4 ??   0 00 0    0 0 00 033N 55O , '?<+H HH H / *oo.N NN N ol388"(( CC Cr1c6tjdgdzd}tj|tj}tj|gdg}t |j }|j jd}|jdk(sJtjj|djsJtjj|djsJ|j|sJy) Nsxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx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+ +A .C 88: "" " 88 # #E!HMM 22 2 88 # #F1INN 33 3 <<  r1ctjdgdi}tjt5|j ddddy#1swYyxYw)NrYr8T) nan_as_null)r r!rar} RuntimeErrorr$)r!s r/test_nan_as_nullrsG HHc<( )E | $. -...s AAc ttjtdkrtjdtj dgdi}tj t5tj|ddddtj ddtdd d tdd d gi}tj t5tj|ddddy#1swYsxYw#1swYyxYw) NrUrVrY)rg?g@F allow_copyrrrrr) rr_r`rarbrr}rrer^rrs r/test_allow_copy_falsersr~~!11 <= sM* +B | $0 "/0  tRa_bq"o6 B | $0 "/00 00 00s-C7D7DD cttjtdkrtjdtj dgdi}tj t5tj|ddddtj dgdi}tj t5tj|ddddtj dgd i}|jd }tj t5tj|ddddtj dgd i}|jd }tj t5tj|ddddy#1swY#xYw#1swYxYw#1swYxYw#1swYyxYw) NrUrVrY)NFTFrrXr)rYrZNcategory)rYrZr[) rr_r`rarbrr}rrer^astypers r/&test_allow_copy_false_bool_categoricalrsar~~!11 <= s/0 1B | $0 "/0 s/0 1B | $0 "/0 y"23 4B : B | $0 "/0 y/2 3B : B | $0 "/000000 00 00s0-F&?F3"F?G &F03F<?G Gctjdtjfg}tjgg|}|j }t j ||k(sJy)Ncol1)schema)r rint8r!r$rer^)rrdfis r/test_empty_dataframer sW YY+, -F 2$v &B   C  S !R '' 'r1)4rrpyarrowr pyarrow.vendored.versionrranumpyrc ImportErrorpyarrow.interchange interchangerepyarrow.interchange.columnrrr"pyarrow.interchange.from_dataframer pandasr_mark parametrizer0rErOuint8uint16uint32rint16int32int64rRrSroryrrrrrrrrr large_memoryrrrrrr1r/rs$$, ! ?  !89KL @M: @&  ! q # # $2 XRXXZibiik 2 GBGGIxrxxz8288:xrxxz : y! y!KK:KK8!)!)H))>,D,D^!89N:NDY *##*   XRXXZibiik 2 GBGGIxrxxz8288:xrxxz : y! y! y!!89GH)+KLKMI:"KB)+LMDNDD    . 00$004(i B  s"SSSSSS