gL i *dZddlmZddlmZmZddlZddlmZddl m Z m Z m Z ddl Z ddlZddlZddlmZmZmZmZmZmZddlZddlZddlmZddlZddlZdd lmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'dd l(m)Z)m*Z+m,Z,m-Z-m.Z.ddl/Z/ddl0Z0dd l1m2Z2m3Z3dd l4m5Z5m6Z6dd l7m8Z8ddl9m:Z:ddl;mZ>m?Z?m@Z@mAZAddlBmCZCddlDmEZEeFe.e,ze-zZGeGjdejdZJe%de2ZKerddlLmMZMddl1mNZNmOZOmPZPmQZQmRZRmSZSddlTmUZUe jGddZWe jGdde#e!ZXdSdZYe'dTd ZZe'dUd!ZZdVd"ZZdWd#Z[e'dXdYd%Z\e' dX dZd&Z\ d[ d\d(Z\d)Z]d]d*Z^e:eEd+eEd,dz- d^ d_d.Z_d`d/Z`d0d0d0d0d1d2d3d4d5d6 ZaeFeajZc dad7Zde:eEd,dz8 dbd9Zedcd:Zfe'd$d$d$d$d$d; ddd=Zge'd$d$d$d$d$d$d> ded?Zge'd$d$d$d$d$d$d> dfd@Zge:eEd,d dgdBZgGdCdDeeZhGdEdFehZiGdGdHehZjGdIdJZkGdKdLZl dhdMZmdidNZndjdOZoejdkdPZq dl dmdQZr dndRZsy)ozCommon IO api utilities) annotations)ABCabstractmethodN) defaultdict)HashableMappingSequence)BufferedIOBaseBytesIO RawIOBaseStringIO TextIOBase TextIOWrapper)Path) IO TYPE_CHECKINGAnyAnyStr DefaultDictGenericLiteralTypeVarcastoverload)urljoinurlparse uses_netloc uses_params uses_relative) BaseBuffer ReadCsvBuffer) get_bz2_file get_lzma_file)import_optional_dependency)doc)find_stack_level)is_bool is_file_like is_integer is_list_like) ABCMultiIndex) _shared_docsz^[A-Za-z][A-Za-z0-9+\-+.]*:// BaseBufferT)bound) TracebackType)CompressionDictCompressionOptionsFilePath ReadBufferStorageOptions WriteBuffer) MultiIndexcHeZdZUdZded<ded<ded<ded<d Zd ed <y ) IOArgsz? Return value of io/common.py:_get_filepath_or_buffer. str | BaseBufferfilepath_or_bufferstrencodingmoder1 compressionFbool should_closeN)__name__ __module__ __qualname____doc____annotations__rAV/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/pandas/io/common.pyr9r9as))(M I  L$rHr9ceZdZUdZded<ded<ej eZded<d Z d ed <dd Z dd Z ddZ y) IOHandlesau Return value of io/common.py:get_handle Can be used as a context manager. This is used to easily close created buffers and to handle corner cases when TextIOWrapper is inserted. handle: The file handle to be used. created_handles: All file handles that are created by get_handle is_wrapped: Whether a TextIOWrapper needs to be detached. z IO[AnyStr]handler1r?)default_factoryzlist[IO[bytes] | IO[str]]created_handlesFr@ is_wrappedcd|jrut|jtsJ|jj |jj |j j|j|j D]}|jg|_d|_y)z Close all created buffers. Note: If a TextIOWrapper was inserted, it is flushed and detached to avoid closing the potentially user-created buffer. FN) rO isinstancerLrflushdetachrNremoveclose)selfrLs rIrUzIOHandles.closes ??dkk=9 99 KK    KK    ' ' 4** F LLN !rHc|SNrGrVs rI __enter__zIOHandles.__enter__s rHc$|jyrX)rU)rVexc_type exc_value tracebacks rI__exit__zIOHandles.__exit__s rHNreturnNone)razIOHandles[AnyStr])r\ztype[BaseException] | Noner]zBaseException | Noner^zTracebackType | Nonerarb) rBrCrDrErF dataclassesfieldlistrNrOrUrZr_rGrHrIrKrKnsm    1B1B1BSW1XO.XJ ",((   rHrKcZt|tsyt|jtvS)z Check to see if a URL has a valid protocol. Parameters ---------- url : str or unicode Returns ------- isurl : bool If `url` has a valid protocol return True otherwise False. F)rQr< parse_urlscheme _VALID_URLSurls rIis_urlrls& c3  S> K //rHr;cyrXrGr;s rI _expand_userrorHcyrXrGrns rIrororprHcdt|trtjj |S|S)a] Return the argument with an initial component of ~ or ~user replaced by that user's home directory. Parameters ---------- filepath_or_buffer : object to be converted if possible Returns ------- expanded_filepath_or_buffer : an expanded filepath or the input if not expandable )rQr<ospath expanduserrns rIroros+$c*ww!!"455 rHc`|yt|r!tt|}|dkr tdyt |drRtt |}t tt|s tdtd|Dr tdyt|r tdtd) NrzUPassing negative integer to header is invalid. For no header, use header=None insteadF) allow_setsz*header must be integer or list of integersc3&K|] }|dk yw)rNrG).0is rI z&validate_header_arg..s%q1u%sz8cannot specify multi-index header with negative integerszPassing a bool to header is invalid. Use header=None for no header or header=int or list-like of ints to specify the row(s) making up the column names) r)rint ValueErrorr*r allmapanyr' TypeError)headers rIvalidate_header_argrs ~&c6" A:9  Fu-h'3z6*+IJ J %f% %WX Xv 4  A BBrH.cyrXrGr;convert_file_likes rIstringify_pathrrprHcyrXrGrs rIrrsrHFc|st|rtt|St|tj r|j }t|S)a Attempt to convert a path-like object to a string. Parameters ---------- filepath_or_buffer : object to be converted Returns ------- str_filepath_or_buffer : maybe a string version of the object Notes ----- Objects supporting the fspath protocol are coerced according to its __fspath__ method. Any other object is passed through unchanged, which includes bytes, strings, buffers, or anything else that's not even path-like. )r(rr.rQrsPathLike __fspath__rors rIrrsI. .@!AK!344$bkk2/::< * ++rHcBddl}|jj|i|S)z` Lazy-import wrapper for stdlib urlopen, as that imports a big chunk of the stdlib. rN)urllib.requestrequesturlopen)argskwargsurllibs rIrrs#  !6>> ! !4 26 22rHct|txr2ttj |xr|j d S)zR Returns true if the given URL looks like something fsspec can handle )zhttp://zhttps://)rQr<r@_RFC_3986_PATTERNmatch startswithrjs rI is_fsspec_urlr$sA 3 8 "((- . 867 7rHstorage_optionscompression_options)rrc&t|}t|\}}t||}|r6t|dr*d|vr&t j dt td}t||}d|vr1|dvr-|d vr)t j |d |tt|}d |vr d|vr|dz }t|trt|r|xsi}d dl }|jj|| }t!|5} | j"j%dd} | dk(rddi}t'| j)} dddt+ ||d|St-|rt|tsJ|j/dr|j1dd}|j/dr|j1dd}t3d} g} t3dd dlm}m}||t:g} | j>|fd|i|xsij?}t+|||d|S|r tCdt|ttDtFjFfrt+tI|||d|St|ds%t|dsdtK|}tC|t+|||d|S#1swYkxYw#t<$rYwxYw#tA| $rB|ddi}nt|}d|d<| j>|fd|i|xsij?}YwxYw)a If the filepath_or_buffer is a url, translate and return the buffer. Otherwise passthrough. Parameters ---------- filepath_or_buffer : a url, filepath (str, py.path.local or pathlib.Path), or buffer {compression_options} .. versionchanged:: 1.4.0 Zstandard support. encoding : the encoding to use to decode bytes, default is 'utf-8' mode : str, optional {storage_options} Returns the dataclass IOArgs. writebzDcompression has no effect when passing a non-binary object as input.) stacklevelN)methodw)bz2xz)zutf-16zutf-32z( will not write the byte order mark for tr)headerszContent-EncodinggziprT)r;r=r?rAr>zs3a://zs3://zs3n://fsspecbotocore) ClientErrorNoCredentialsErrorr>anonz?storage_options passed with file object or non-fsspec file pathFreadz)Invalid file path or buffer object type: )&rget_compression_methodinfer_compressionhasattrwarningswarnRuntimeWarningr&dictUnicodeWarningrQr<rlrrRequestrrgetr rr9rrreplacer$botocore.exceptionsrrPermissionError ImportErroropentupler}bytesmmaprotype)r;r=r?r>rcompression_method fsspec_moderreq_inforeqcontent_encodingreaderrerr_types_to_retry_with_anonrrfile_objmsgs rI_get_filepath_or_bufferr0s>((:;'=[&I# *+=?QRg&8'BsRV R ') "{+=>K t - / , , mCH: N ') K +#["8s $c*v6H/I*/R >>))*+,>?#    "F+w7I7/S9$?Q:R9STo -   u ) )T   12 &#)4.#'"7*.'"v{{")49H9NBdf  s1RVW,c2'?&D&D&F # "I{!'')229="" #-- dOf%<= =E )+7'',g / S/rHclt|j}|jstd|dy)z Check if parent directory of a file exists, raise OSError if it does not Parameters ---------- path: Path or str Path to check parent directory of z1Cannot save file into a non-existent directory: ''N)rparentis_dirOSError)rtrs rIcheck_parent_directoryr]s7$Z  F ==?J6(RSTUU rH)r=r? memory_maperrorsr path_or_bufcyrXrGrr>r=r?ris_textrrs rI get_handlerkrH)r=r?rrrrcyrXrGrs rIrrzrrHcyrXrGrs rIrrrrHTc |xsd}|xsd}t||r d|vr|dz }tj|t|trtj |t |||||}|j} t| |\} }} t| t} t|j} | jd}d|vr| rtt | |r8|dk7r"|jjdd |_ n(|dk(r#d|jvr|xjdz c_ |d k(rXt| tr$tj d)| |jd | } ntj d)| |jd | } nh|d k(rt#| fd|ji| } nD|dk(rt%| |jfi| } | j&jdk(r| j)| | j&j+} t-| dk(r+| j&j/| j} n| st1d|t1d| |dk(r| j3d|jt| trt5d)d| i| } n t5d)d| i| } t| t4sJd| j&jvr| j)| | j&j7}t-|dk(r%| j&j9|d}|J|} n|st1d|t1d||dk(rt;| |jfi| } nw|dk(rbt=d}d|jvrd|j>d)i| i}nd|j@d)i| i}|j.| fd|ji|} nd|}t1|t| trJ| j)| nvt| trf|jBr3d|jvr%t/| |j|jB|d } nt/| |j} | j)| d}|s7|jd k(r(t| tDrtG| |jB!} n|r|s|st| |jrtI| d"rtI| d#r tI| d$s tK| } tM| |jB|d } | j)| t|jtxs |jN }d|jvr.tI| d%s"tQd&tS|jd'| jU|jNr7t|jtrJ| j)|jtW| | ||j(S)*a# Get file handle for given path/buffer and mode. Parameters ---------- path_or_buf : str or file handle File path or object. mode : str Mode to open path_or_buf with. encoding : str or None Encoding to use. {compression_options} May be a dict with key 'method' as compression mode and other keys as compression options if compression mode is 'zip'. Passing compression options as keys in dict is supported for compression modes 'gzip', 'bz2', 'zstd' and 'zip'. .. versionchanged:: 1.4.0 Zstandard support. memory_map : bool, default False See parsers._parser_params for more information. Only used by read_csv. is_text : bool, default True Whether the type of the content passed to the file/buffer is string or bytes. This is not the same as `"b" not in mode`. If a string content is passed to a binary file/buffer, a wrapper is inserted. errors : str, default 'strict' Specifies how encoding and decoding errors are to be handled. See the errors argument for :func:`open` for a full list of options. storage_options: StorageOptions = None Passed to _get_filepath_or_buffer Returns the dataclass IOHandles utf-8strictr)r=r?r>rrrrrr-r)filenamer>)fileobjr>rr>rzZero files found in ZIP file z9Multiple files found in ZIP file. Only one file per ZIP: rnamerrz Zero files found in TAR archive zDMultiple files found in TAR archive. Only one file per TAR archive: r zstandarddctxcctxr)r=rnewlineFrb)r=readablewritableseekablerz1Expected file path name or file-like object, got z type)rLrNrOr?rG),_is_binary_modecodecslookuprQr< lookup_errorrr;_maybe_memory_maprr?rrr>rrGzipFiler" _BytesZipFilebufferappendnamelistlenrr} setdefault _BytesTarFilegetnames extractfiler#r$ZstdDecompressorZstdCompressorr=r_BytesIOWrapperr _IOWrapperrrArrreverserK)rr>r=r?rrrrioargsrLhandlesis_pathr zip_namesfilesfiler open_argsrrOs rIrrs d"7H  xF{D)co   MM(&#F#% ' F & &F #4FJ"GFJ%GF../"&&x0K $7s6{+ &  ++--c26FK F "s&++'= KK3 K & &#&#' #  ' E !$\^[[#FE !# '7F}}!!S(v&"MM224 y>Q&#]]// @F"$'D[M%RSS$22;> E !  ' ' <&#&&GFG6FG '"&6fm4 44fmm(((v& ..0u:?!==44U1X>D+++!F$'G }%UVV$::?B D %]_ '7F F "-k:Dfkk!#%:T%:%:%N=M%NO #%8T%8%8%L;K%LM TYY[[F4K=ACS/ !fc***v FC  ??s&++5  F&&++.FvJ v{{d*z&*/M __  z_VV[[%I +6:.6:.'F __   v v00# 6 M&:M:M  fkk'&&"9 11235 :   OO f77===v001  &&  rHcDeZdZdZeZeddZdfd ZxZ S)_BufferedWriterz Some objects do not support multiple .write() calls (TarFile and ZipFile). This wrapper writes to the underlying buffer on close. cyrXrGrYs rIwrite_to_bufferz_BufferedWriter.write_to_buffers rHc*|jry|jjr7|jd|j5|j dddn|jj t|y#1swYxYw)Nr)closed getbuffernbytesseekr r$rUsuper)rV __class__s rIrUz_BufferedWriter.closesp ;;  >>  " " IIaL '$$& ' ' KK       ' 's B  Br`) rBrCrDrEr r rr$rU __classcell__r+s@rIr"r"s- YF    rHr"cTeZdZ d dfd ZddZddZd dZxZS) rc t|||_||_t j j d||j||d||_y)N)rr>rrG) r*__init__ archive_namertarfileTarFiler extend_moder )rVrr>rr1rr+s rIr0z_BytesTarFile.__init__sZ ( (/';';( !!$'(  (  rHc|jdd}|dk7r|S|j-t|jj}|dvr |d|dd}|S)Nrr-r)rrr:r)rrrsuffix)rVr>r7s rIr4z_BytesTarFile.extend_modes_||C$ 3;K 99 $))_++F//q - rHc"|jyt|j}|jdk(r|jdjS|jdvr*|jdjdjS|jS)z If an explicit archive_name is not given, we still want the file inside the zip file not to be named something.tar, because that causes confusion (GH39465). Nrr-)rrr)rrr7 with_suffixrVrs rIinfer_filenamez_BytesTarFile.infer_filenamesz 99  ? ??f $''+00 0 __ B B''+77;@@ @}}rHc|jxs|jxsd}tj|}t |j |_|jj||y)Nr)r) r1r;r2TarInforgetvaluesizer addfile)rVr1tarinfos rIr$z_BytesTarFile.write_to_buffersT((JD,?,?,AJU //|44==?+  GT*rH)NrNN) r str | Noner>zLiteral['r', 'a', 'w', 'x']rz-ReadBuffer[bytes] | WriteBuffer[bytes] | Noner1rBrarb)r>r<rar<rarBr`)rBrCrDr0r4r;r$r,r-s@rIrrsU ,/AE#'   * ?  !    ( +rHrcBeZdZ d dfd ZddZddZxZS)r c t||jdd}||_|j dt j t j||fi||_y)Nrr-r?) r*r0rr1rzipfile ZIP_DEFLATEDZipFiler )rVrr>r1rr+s rIr0z_BytesZipFile.__init__s] ||C$(-)=)=>(/ $(  (  rHct|jjtjt frUt |jj}|jdk(r|jdjS|jSy)z If an explicit archive_name is not given, we still want the file inside the zip file not to be named something.zip, because that causes confusion (GH39465). rr-N) rQr rrsrr<rr7r9rr:s rIr;z_BytesZipFile.infer_filenamese dkk**R[[#,> ?DKK001H&(++B/444== rHc|jxs|jxsd}|jj||j y)Nr)r1r;r writestrr>)rVr1s rIr$z_BytesZipFile.write_to_buffers:((JD,?,?,AJU  \4==?;rHrX)rz1FilePath | ReadBuffer[bytes] | WriteBuffer[bytes]r>r<r1rBrarbrCr`)rBrCrDr0r;r$r,r-s@rIr r s= $(  ?  !    $  "mmj8 9Ac*=&>!>DM& &+BQ/I/3DM rHN)r)r zStringIO | TextIOBaser=r<rarb)r[r<))r^z int | Nonerar)rBrCrDr0rQrrGrHrIrr@s* rHrcg}|t|dxst|tz}|s|||fStt|}t|trt |d}|j | ttj|jdtj}t|D]}|j |||gfS#t|D]}|jwxYw)zTry to memory map file/buffer.filenorr)access) rrQr<rr!rrrrrd ACCESS_READreversedrU)rLrrwrappeds rIr r ]s!#G'&(+Fz&#/FFJ z7**- (F&#fd#v  II 4+;+;   w' F LLN  J )) w' F LLN s )=C "C/cd}t|}t|ts|S tjj |}|S#t tf$rY|SwxYw)zTest whether file exists.F)rrQr<rsrtexistsrr})r;rjs rI file_existsrks` F'(:; (# .   23 M z "  M sAAAcd|vsd|vrd|vStjtjtjf}t t ||ryt |txsdt|d|vS)z+Whether the handle is opened in binary moderrFr>) r StreamWriter StreamReaderStreamReaderWriter issubclassrrQ_get_binary_io_classesrP)rLr> text_classess rIrrs d{cTkd{ !! L $v, - f46 7 3'C<rHcttf}tdd}|9|jj d5}|t |fz }ddd|S|S#1swY|SxYw)z!IO classes that that expect bytesrignore)rNrH)r r r$r stream_readerr)binary_classesrrs rIrqrqsp)7 'BN &k( CD   " " $ 2 23 7 .6 tF|o -N . > . s AA!cttrgtt|xr't|t xrt fd|DS)a Check whether or not the `columns` parameter could be converted into a MultiIndex. Parameters ---------- columns : array-like Object which may or may not be convertible into a MultiIndex index_col : None, bool or list, optional Column or columns to use as the (possibly hierarchical) index Returns ------- bool : Whether or not columns could become a MultiIndex c3ZK|]"}|tvst|t$ywrX)rerQr)ryc index_cols rIr{z+is_potential_multi_index..s#R$y/9Q 1e$Rs++)rQr@rr+r~)columnsrzs `rIis_potential_multi_indexr|sT&Jy$7  G  S7M2 2 S RgR R rHct|}tt}t|D]\\}}||}|dkDr@|dz||<|r$t |t sJ|dd|dd|fz}n|d|}||}|dkDr@|||<|dz||<^|S)a: Rename column names if duplicates exist. Currently the renaming is done by appending a period and an autonumeric, but a custom pattern may be supported in the future. Examples -------- >>> dedup_names(["x", "y", "x", "x"], is_potential_multiindex=False) ['x', 'y', 'x.1', 'x.2'] rrNrb.)rerr| enumeraterQr)namesis_potential_multiindexcountsrzcol cur_counts rI dedup_namesrs KE)4S)9FE"$33K !m#a-F3K&!#u---#2hSWIQyk":!<<Qyk*s I!ma!ms $" LrH)rkobjectrar@)r;r<rar<)r;r.rar.)r;str | BaseBufferTrar)rrrarb).)r;r3rr@rar<)r;r.rr@rar.)F)r;zFilePath | BaseBufferTrr@rar)rkFilePath | BaseBufferrar@)rNrN) r;rr=r<r?CompressionOptions | Noner>r<rStorageOptions | Nonerar9)rtr<rar<)r?r2raz"tuple[str | None, CompressionDict])r;rr?rBrarB)rtz Path | strrarb)rrr>r<r=rBr?r2rr@rzLiteral[False]rrBrr5razIOHandles[bytes])rrr>r<r=rBr?r2rr@rz Literal[True]rrBrr5razIOHandles[str])rrr>r<r=rBr?r2rr@rr@rrBrr5ra!IOHandles[str] | IOHandles[bytes])rrr>r<r=rBr?rrr@rr@rrBrrrar)rLr:rr@raz/tuple[str | BaseBuffer, bool, list[BaseBuffer]])r;rrar@)rLrr>r<rar@)raztuple[type, ...]rX)r{zSequence[Hashable] | MultiIndexrzzbool | Sequence[int] | Nonerar@)rSequence[Hashable]rr@rar)trE __future__rabcrrr collectionsrcollections.abcrrr rc functoolsrior r r r rrrrspathlibrrer2typingrrrrrrrrrr urllib.parserrrgrrrrrFpandas._typingr r! pandas.compatr"r#pandas.compat._optionalr$pandas.util._decoratorsr%pandas.util._exceptionsr&pandas.core.dtypes.commonr'r(r)r*pandas.core.dtypes.genericr+pandas.core.shared_docsr,setridiscardcompilerr.typesr0r1r2r3r4r5r6pandasr7 dataclassr9rKrlrorrrrrrrvaluesrrrrrr"rr rrr rkr lru_cacherqr|rrGrHrIrs"#       ?'4 50-+- ;< BBJJ?@m:6 #"     ...b0$    &C:   ?B#8< $,.,,,D3  !23$%:;>RR -1-1 l-ll+l  l + l  l l^0&       6==?@ 0# 0' 0F&;