L i\9 UddlmZddlZddlZddlZddlZddlmZmZddl m Z ddl m Z ddl mZmZmZmZmZmZmZmZddlmcmZddlmZddlmZd d lmZd d l m!Z!m"Z"m#Z#m$Z$m%Z%d d l&m'Z'ejPd k\r ddl m)Z)m*Z*nddl+m)Z)m*Z*ejPdk\r ddl m,Z,m-Z-nddl+m,Z,m-Z-er@ejPd k\rddl m.Z.nddl+m.Z.ejPdk\rddl m/Z/nddl+m/Z/ddl0Z1ddl2Z3e*Gdde)Z4ee5eefee4e!fZ6de7d<ede6Z8edeZ9e5e:ee:e5eefe;e5eefffZd>dZ?e-dZ@ede!>r,rAceZdZdZy) MaxRowsErrorz+Raised when a data model has too many rows.N)r'r(r)__doc__r+r,r-rPrPss5r,rPcyNr+datamax_rowss r- limit_rowsrWwsILr,cyrSr+rTs r-rWrWysHKr,cL|ttSt|fd}t|tr/|j ddk(r|j d}n6|j }n)t|t r d|vr|d}n|St|}|}t|kDr||S)z Raise MaxRowsError if the data model has more than max_rows. If max_rows is None, then do not perform any check. )rVc&dd}t|)NzHThe number of rows in your dataset is greater than the maximum allowed (aG). Try enabling the VegaFusion data transformer which raises this limit by pre-evaluating data transformations in Python. >> import altair as alt >> alt.data_transformers.enable("vegafusion") Or, see https://altair-viz.github.io/user_guide/large_datasets.html for additional information on how to plot large datasets.)rP)msgrVs r-raise_max_rows_errorz(limit_rows..raise_max_rows_errors) ))1 3- - 3r,typeFeatureCollectionfeaturesvalues) rrWcheck_data_typer7r$r%r8rlen)rUrVr\r`s ` r-rWrW{s |zH55D  $,-  ! !& )-@ @++J7F++F D$  t (^FK*40F h 6 Kr,cyrSr+rUnfracs r-samplergsr,cyrSr+rds r-rgrgsr,cyrSr+rds r-rgrgsr,c(|tt||St|t|r|j||St |t rLd|vrG|d}|s&| d}t |t|t|z}tj||}d|iSytj|d}|s&| d}t |t|t|z}tjtt||}||jS)zBReduce the size of the data model by sampling without replacement.N)rerfr`z9frac cannot be None if n is None and data is a dictionaryT eager_onlyz:frac cannot be None if n is None with this data input type)rrgrarr7r8 ValueErrorintrbrandomr9r:range to_native)rUrerfr`r[indicess r-rgrgs  |v..D4 {{QT{** D$  t (^F<UC$S/)s6{*+]]61-Ff% % >>$4 0D <NCS/ ! s4y !mmE#d),a0G = " " $$r,)csvjsonceZdZUded<y) _FormatDict _FormatTyper]Nr&r+r,r-rvrvs r,rvc"eZdZUded<ded<y)_ToFormatReturnUrlDictstrurlrvformatNr&r+r,r-ryrys H r,rycyrSr+rUprefix extensionfilenameurlpaths r-to_jsonrr,cyrSr+r~s r-rr!r,ct||||}|ttfi|St|}t |fi|dt diS)zGWrite the data model to a .json file and return a url based data model.r|rtr]) _to_text_kwdsrr_data_to_json_string_to_textrvrUrrrrkwdsdata_strs r-rrsM Hg >D |w'$'''-JDJ&1IJJr,cyrSr+r~s r-to_csvrrr,cyrSr+r~s r-rrrr,ct||||}|ttfi|St|}t |fi|dt diS)zFWrite the data model to a .csv file and return a url based data model.r|rsr)rrr_data_to_csv_stringrrvrs r-rrsM Hg >D |v&&&&t,IDI%1HIIr,ct|}|j|||}t|j|dt t||}t ||dS)N)rhashrzutf-8)encoding)r{r|)_compute_data_hashr|r write_textrzry)rUrrrrr| data_hashr{s r-rr,sZ#4(If9 RHNdW5 d7H% &C !#"@ AAr,c||||dS)Nrrrrr+rs r-rr;s9(W^ __r,ct|tj|d}t|tr dt |iSt |rt|}d|jdiSt|trd|vr d}t||St|tjrt|}d|jdiSdt|}t|) z0Replace a DataFrame by a data model with values.T)r6r`records)orient.values expected in data dict, but not present.namedzUnrecognized data type: )rar9rqr7r$_from_geo_interfacerrto_dictr8KeyErrorr;rrowsr]rm)rU data_nativer[s r- to_valuesr?sD,,t$7K+34-k:;; [ )/ < +--Y-?@@ K & ; &BC3-  D",, '*40$))$)/00)d 5or,cLt|sdt|}t|y)Nz@Expected dict, DataFrame or a __geo_interface__ attribute, got: )r=r] TypeError)rUr[s r-raraWs-  PQUVZQ[P\]n r,cjtj|jjddS)N )hashlibsha256encode hexdigest)rs r-rr`s( >>(//+ , 6 6 8" ==r,cXt|r t|}t|jS)z Santize a ``__geo_interface__`` w/ pre-santize step for ``pandas`` if needed. Notes ----- Split out to resolve typing issues related to: - Intersection types - ``typing.TypeGuard`` - ``pd.DataFrame.__getattr__`` )rrrr%)rUs r-rrds'4 (. !$"8"8 99r,c t|t|trtjt |St |rt|}|jddSt|tr+d|vr d}t|tj|ddS tj|d}t|}tj|j!d S#t$r}d }t||d }~wwxYw) z6Return a JSON string representation of the input data.r)rdouble_precisionr`rT) sort_keysrkzBto_json only works with data expressed as a DataFrame or as a dictNr)rar7r$rtdumpsrrrrr8rr9r:rNotImplementedErrorrr)rUr[data_nwexcs r-rrtsD$,-zz-d344 T "(.||9r|BB D$  4 BC3- zz$x.D990..$7*'2G ::glll. // 0R!#&C/0sC&& D/C==DcLt|t|tr(dttjd}t |t |rt|}|jdSt|trDd|vr d}t| ddl }|jj|djdS tj |d }|j%S#t$r}d }t||d}~wwxYw#t"$r}d }t ||d}~wwxYw) z5Return a CSV string representation of the input data.z3to_csv does not yet work with data that is of type z0. See https://github.com/vega/altair/issues/3441F)indexr`z-values expected in data dict, but not presentrNz4pandas is required to convert a dict to a CSV stringTrkzAto_csv only works with data expressed as a DataFrame or as a dict)rar7r$r]r'rrrrr8rpandas ImportErrorr; from_dictr9r:r write_csv)rUr[pdrrs r-rrs+D$,-34==@A= > "#&& T "(.{{{'' D$  4 AC3-  , ||%%d8n5<<5<II0..$7     ,HCc" + , 0Q!#&C/0s0C(D( D1C??D D#DD#cddl}dD]<}t||d}t|s|}t||js:|cSt }|j |S)zMConvert a DataFrame Interchange Protocol compatible object to an Arrow Table.rN)arrowto_arrowto_arrow_table to_pyarrow)pyarrowgetattrcallabler7Tablerfrom_dataframe)dfi_dfpaconvert_method_nameconvert_methodresultpis r-arrow_table_from_dfi_dataframerse U )r?DataTransformerTyperA ExceptionrPrWrgrwrvryrrrrrrarrrrrr+r,r-rs" 4    ?32,w22=w--8 7"!, 7"$/&8&&cNM#7F) Kx 0 )?"sDcNDc3h$88 99)!%S%S#XT#s(^@T0T*U%U VIVS(](;TAB&cN C!3'{8Q;7:; >n-@!-CD >696 L L K K9=, ,,6,,^ @C #1=   DG '5A  >A !/; !% %% % %Dm$ )Y             ! ! !! !  !  ! !!1 K  K  K K K  K & K             ! -! !! !  !  ! !8<1 J 4 J  J J J  J & J B  B  B B B  B  B B`0> : 0,>%r,