Ë
    ¢�DjÅ)  ã                   ó  — d dl mZ d dlmZ d dlmZmZ d dlmZm	Z	m
Z
mZmZ d dlZd dlmZmZ d dlmZ d dlmZ d dlZd d	lmZmZmZ dd
„Z G d„ de«      Zdd„Zd„ Zd„ Z d„ Z!d„ Z"d„ Z#dd„Z$d„ Z%dd„Z&dd„Z'dd„Z(dd„Z)d„ Z*d d„Z+y)!é    )Úlrange)ÚStringIO)ÚenvironÚmakedirs)ÚabspathÚdirnameÚexistsÚ
expanduserÚjoinN)Ú	HTTPErrorÚURLError)Úurljoin)Úurlopen)ÚIndexÚread_csvÚ
read_statac                 ó6   — t        || dz   «      }t        |«      S )a  
    Download and return an example dataset from Stata.

    Parameters
    ----------
    data : str
        Name of dataset to fetch.
    baseurl : str
        The base URL to the stata datasets.
    as_df : bool
        Deprecated. Always returns a DataFrame

    Returns
    -------
    dta : DataFrame
        A DataFrame containing the Stata dataset.

    Examples
    --------
    >>> dta = webuse('auto')

    Notes
    -----
    Make sure baseurl has trailing forward slash. Does not do any
    error checking in response URLs.
    z.dta)r   r   )ÚdataÚbaseurlÚas_dfÚurls       ú^C:\Crop_Prediction\Backend\crop-ai-system\venv\Lib\site-packages\statsmodels/datasets/utils.pyÚwebuser      s   € ô6 �'˜4 ™;Ó
'€CÜ�c‹?Ðó    c                   ó   — e Zd Zd„ Zd„ Zy)ÚDatasetc                 óÎ   — d | _         d | _        d | _        d | _        t        j                  | |«       | | _        	 | j                  j                  t        «      | _	        y #  Y y xY w©N)
ÚendogÚexogr   ÚnamesÚdictÚ__init__Ú__dict__ÚastypeÚfloatÚraw_data)ÚselfÚkws     r   r#   zDataset.__init__/   sX   € àˆŒ
ØˆŒ	ØˆŒ	ØˆŒ
ä�‰�d˜BÔØˆŒð	Ø ŸI™I×,Ñ,¬UÓ3ˆD�Møð	Ùús   »$A  Á A$c                 ó,   — t        | j                  «      S r   )ÚstrÚ	__class__)r(   s    r   Ú__repr__zDataset.__repr__?   s   € Ü�4—>‘>Ó"Ð"r   N)Ú__name__Ú
__module__Ú__qualname__r#   r-   © r   r   r   r   .   s   „ òó #r   r   c                 óÖ  — | j                   }t        |t        «      rF||   }| |   j                  «       }|€| j	                  |gd¬«      }n | ||      j                  «       }n‰| j
                  d d …|f   j                  «       }t        |j                   «      }|€| j	                  |d¬«      }n=t        |t        «      r| ||      j                  «       }n| ||      j                  «       }|�Lt        | j                  d d …|f   «      }||_	        |j                  «       |_	        | j                  ||   «      } t        |j                   «      }	t        | t        |«      ||||	¬«      }
|
S )Né   )Úaxis)r   r!   r   r    Ú
endog_nameÚ	exog_name)ÚcolumnsÚ
isinstanceÚintÚcopyÚdropÚlocÚlistr   ÚilocÚindexÚ	set_indexr   )r   Ú	endog_idxÚexog_idxÚ	index_idxr!   r5   r   r    r?   r6   Údatasets              r   Úprocess_pandasrE   C   sO  € Ø�L‰L€Eä�)œSÔ!Ø˜9Ñ%ˆ
Ø�ZÑ ×%Ñ%Ó'ˆØÐØ—9‘9˜j˜\°�9Ó2‰Dà˜˜h™Ñ(×-Ñ-Ó/‰Dà—‘š˜I˜Ñ&×+Ñ+Ó-ˆÜ˜%Ÿ-™-Ó(ˆ
ØÐØ—9‘9˜Z¨a�9Ó0‰DÜ˜¤#Ô&Ø˜˜h™Ñ(×-Ñ-Ó/‰Dà˜˜h™Ñ(×-Ñ-Ó/ˆDàÐÜ�d—i‘i¢ 9 Ñ-Ó.ˆØˆŒØ—Z‘Z“\ˆŒ
Ø�~‰~˜e IÑ.Ó/ˆä�T—\‘\Ó"€IÜ˜4¤t¨E£{¸%Ø¨JÀ)ôM€Gà€Nr   c           
      óž   — | j                   j                  t        t        dt	        | «      dz   «      «      «      r| j                  d¬«      } | S )z
    All the Rdatasets have the integer row.labels from R if there is no
    real index. Strip this for a zero-based index
    r3   T)r;   )r?   Úequalsr   r   ÚlenÚreset_index)r   s    r   Ú_maybe_reset_indexrJ   c   sB   € ð
 ‡z�z×Ñœœv a¬¨T«°Q©Ó7Ó8Ô9Ø×Ñ TÐÓ*ˆØ€Kr   c                 óN   — | du rd } | S | du rt        d «      } | S t        | «      } | S )NFT)Úget_data_home)Úcaches    r   Ú
_get_cacherN   m   s@   € Ø��~àˆð
 €Lð	 
�$‰Ü˜dÓ#ˆð €Lô ˜eÓ$ˆØ€Lr   c                 óŽ   — dd l }t        |d«      5 }|j                  |j                  | «      «       d d d «       y # 1 sw Y   y xY w)Nr   Úwb)ÚzlibÚopenÚwriteÚcompress)r   Ú
cache_pathrQ   Úzfs       r   Ú	_cache_itrW   x   s;   € ÛÜ	ˆj˜$Ó	ð & 2Ø
�‰�—‘˜tÓ$Ô%÷&÷ &ñ &ús	   ‘!;»Ac                 óŒ   — dd l }t        | d«      5 }|j                  |j                  «       «      cd d d «       S # 1 sw Y   y xY w)Nr   Úrb)rQ   rR   Ú
decompressÚread)rU   rQ   rV   s      r   Ú_open_cacher\   ~   s8   € Ûä	ˆj˜$Ó	ð * 2Ø�‰˜rŸw™w›yÓ)÷*÷ *ò *ús	   ‘:ºAc                 óŽ  — d}|�Œ| j                  d«      d   j                  dd«      }|j                  d«      }t        |«      dkD  r|dxx   d	z  cc<   n|d
xx   d	z  cc<   dj                  |«      dz   }t        ||«      }	 t	        |«      }d}|s)t        | d¬«      j                  «       }|�t        |«       |fS #  Y Œ3xY w)z·
    Tries to load data from cache location otherwise downloads it. If it
    downloads the data and cache is not None then it will put the downloaded
    data in the cache path.
    Fz://éÿÿÿÿú/ú,ú.r3   éþÿÿÿz-v2r   z.zipTé   )Útimeout)ÚsplitÚreplacerH   r   r\   r   r[   rW   )r   rM   Ú
from_cacheÚ	file_namerU   r   s         r   Ú_urlopen_cachedri   …   s×   € ð €JØÐØ—I‘I˜eÓ$ RÑ(×0Ñ0°°cÓ:ˆ	Ø—O‘O CÓ(ˆ	Üˆy‹>˜AÒØ�b‹M˜UÑ"ŒMà�a‹L˜EÑ!‹LØ—H‘H˜YÓ'¨&Ñ0ˆ	Ü˜% Ó+ˆ
ð	Ü˜zÓ*ˆDØˆJñ
 Ü�s AÔ&×+Ñ+Ó-ˆØÐÜ�d˜JÔ'Ø�ÐÐøð	Ùús   ÂC  Ã Cc                 óÒ   — | |dz   |z  z   }	 t        ||«      \  }}|j	                  dd«      }t        |«      |fS # t        $ r"}dt        |«      v rt        d|z  «      ‚|‚d }~ww xY w)Nz.%sÚ404zDataset %s was not found.úutf-8Ústrict)ri   r   r+   Ú
ValueErrorÚdecoder   )Úbase_urlÚdatanamerM   Ú	extensionr   r   rg   Úerrs           r   Ú	_get_datart   £   s�   € Ø
�h Ñ&¨)Ñ3Ñ
3€CðÜ*¨3°Ó6Ñˆˆjð �;‰;�w Ó)€DÜ�D‹>˜:Ð%Ð%øô ò Ø”C˜“HÑÜÐ8¸8ÑCÓDÐDàˆIûð	ús   �; »	A&ÁA!Á!A&c           	      ó\  — d}t        ||«      \  }}|j                  dd«      }t        t        |«      «      }t	        j
                  |j                  | k(  |j                  |k(  «      }|j                  «       st        d| › d|› d|› d�«      ‚|j                  |   }|d   j                  d	   S )
NzRhttps://raw.githubusercontent.com/vincentarelbundock/Rdatasets/master/datasets.csvrl   rm   zItem z from Package z& was not found. Check the CSV file at z  to verify the Item and Package.ÚTitler   )ri   ro   r   r   ÚnpÚlogical_andÚItemÚPackageÚanyrn   r<   r>   )	rq   ÚpackagerM   Ú	index_urlr   Ú_r?   ÚidxÚdataset_metas	            r   Ú_get_dataset_metar�   ±   s´   € ð1€Iä˜i¨Ó/�G€Dˆ!Ø�;‰;�w Ó)€DÜ”X˜d“^Ó$€EÜ
�.‰.˜Ÿ™ xÑ/°·±À'Ñ1IÓ
J€CØ�7‰7Œ9ÜØ�H�:˜^¨G¨9ð 5Ø(˜kÐ)IðKó
ð 	
ð —9‘9˜S‘>€LØ˜Ñ ×%Ñ% aÑ(Ð(r   c                 ó   — d|z   dz   }d|z   dz   }t        |«      }t        || |«      \  }}t        |d¬«      }t        |«      }t	        | ||«      }t        || |d«      \  }}	t        ||j                  «       |||¬«      S )	a7  download and return R dataset

    Parameters
    ----------
    dataname : str
        The name of the dataset you want to download
    package : str
        The package in which the dataset is found. The default is the core
        'datasets' package.
    cache : bool or str
        If True, will download this data into the STATSMODELS_DATA folder.
        The default location is a folder called statsmodels_data in the
        user home folder. Otherwise, you can specify a path to a folder to
        use for caching the data. If False, the data will not be cached.

    Returns
    -------
    dataset : Dataset
        A `statsmodels.data.utils.Dataset` instance. This objects has
        attributes:

        * data - A pandas DataFrame containing the data
        * title - The dataset title
        * package - The package from which the data came
        * from_cache - Whether not cached data was retrieved
        * __doc__ - The verbatim R documentation.

    Notes
    -----
    If the R dataset has an integer index. This is reset to be zero-based.
    Otherwise the index is preserved. The caching facilities are dumb. That
    is, no download dates, e-tags, or otherwise identifying information
    is checked to see if the data should be downloaded again or not. If the
    dataset is in the cache, it's used.
    zJhttps://raw.githubusercontent.com/vincentarelbundock/Rdatasets/master/csv/r_   zJhttps://raw.githubusercontent.com/vincentarelbundock/Rdatasets/master/doc/z/rst/r   )Ú	index_colÚrst)r   Ú__doc__r|   Útitlerg   )rN   rt   r   rJ   r�   r   r[   )
rq   r|   rM   Údata_base_urlÚdocs_base_urlr   rg   r†   Údocr~   s
             r   Úget_rdatasetrŠ   Ã   s¤   € ðJ#Ø#*ñ+Ø+.ñ/€Mð#Ø#*ñ+Ø+2ñ3€Mä�uÓ€EÜ  °¸%Ó@Ñ€Dˆ*Ü�D AÔ&€DÜ˜dÓ#€Dä˜h¨°Ó7€EÜ�} h°°uÓ=�F€Cˆä˜ c§h¡h£j¸'ÈØ(ô*ð *r   c                 óŒ   — | € t        j                  dt        dd«      «      } t        | «      } t	        | «      st        | «       | S )a
  Return the path of the statsmodels data dir.

    This folder is used by some large dataset loaders to avoid
    downloading the data several times.

    By default the data dir is set to a folder named 'statsmodels_data'
    in the user home folder.

    Alternatively, it can be set by the 'STATSMODELS_DATA' environment
    variable or programatically by giving an explicit folder path. The
    '~' symbol is expanded to the user home folder.

    If the folder does not already exist, it is automatically created.
    ÚSTATSMODELS_DATAú~Ústatsmodels_data)r   Úgetr   r
   r	   r   ©Ú	data_homes    r   rL   rL   ú   sF   € ð ÐÜ—K‘KÐ 2Ü $ SÐ*<Ó =ó?ˆ	ä˜9Ó%€IÜ�)ÔÜ�ÔØÐr   c                 óD   — t        | «      } t        j                  | «       y)z.Delete all the content of the data home cache.N)rL   ÚshutilÚrmtreer�   s    r   Úclear_data_homer•     s   € ä˜iÓ(€IÜ
‡M�M�)Õr   c                 óT   — | €dn| } 	 t        | «       y# t        $ r
}Y d}~yd}~ww xY w)zCheck if internet is availableNzhttps://github.comFT)r   r   )r   rs   s     r   Úcheck_internetr—     s4   € à"% +Ñ
°3€CðÜ�Œð øô ò Üûðús   ˆ ”	'¢'c                 óò   — g }| D ]h  }|j                  d«      r|j                  d«      r|dd }n-|j                  d«      r|dd }n|j                  d«      r|dd }|j                  |«       Œj || _        | S )a
  
    Remove leading and trailing single quotes

    Parameters
    ----------
    df : DataFrame
        DataFrame to process

    Returns
    -------
    df : DataFrame
        DataFrame with stripped column names

    Notes
    -----
    In-place modification
    ú'r3   r^   N)Ú
startswithÚendswithÚappendr7   )Údfr7   Úcs      r   Ústrip_column_namesrŸ   "  s‚   € ð$ €GØò ˆØ�<‰<˜Ô !§*¡*¨TÔ"2Ø�!�B�‰AØ�\‰\˜$ÔØ�!�"�‰AØ�Z‰Z˜ÔØ�#�2�ˆAØ�‰�qÕðð €B„JØ€Ir   c                 óº   — t        t        | «      «      }t        ||«      }|dk7  rdnd}i }|dk(  rddi}t        |f||dœ|¤Ž}|r|j	                  t
        «      }|S )zStandard simple csv loaderr`   Úpythonrž   Úfloat_precisionÚhigh)ÚsepÚengine)r   r   r   r   r%   r&   )	Ú	base_fileÚcsv_namer¤   Úconvert_floatÚfilepathÚfilenamer¥   r¢   r   s	            r   Úload_csvr«   A  sl   € ä”w˜yÓ)Ó*€HÜ�H˜XÓ&€HØ š‰X¨€FØ€OØ�‚}Ø,¨fÐ5ˆÜ�HÐH #¨fÑH¸ÑH€DÙØ�{‰{œ5Ó!ˆØ€Kr   )z%https://www.stata-press.com/data/r11/T)r   NN)Úcsv)ÚdatasetsFr   )r`   F),Ústatsmodels.compat.pythonr   Úior   Úosr   r   Úos.pathr   r   r	   r
   r   r“   Úurllib.errorr   r   Úurllib.parser   Úurllib.requestr   Únumpyrw   Úpandasr   r   r   r   r"   r   rE   rJ   rN   rW   r\   ri   rt   r�   rŠ   rL   r•   r—   rŸ   r«   r1   r   r   ú<module>r·      s‚   ðÝ ,å ß  ß >Õ >Û ß ,Ý  Ý "ã ß .Ñ .óô>#ˆdô #ó*ò@òò&ò*òó<&ò)ó$2*ónó0óòô>r   