B @`w@s^ UddlZddlmZddlmZddlmZddlmZddlZddl Z ddl Z ddl m Z ddl Z ddlZddlmZddlZddlZddlmZmZmZmZmZmZmZmZmZddlZddlZddlZ dd l!m"Z"m#Z#dd l$m%Z%m&Z&m'Z'dd l(m)Z)ddl*m+m,Z-dd l.m/Z/m0Z0m1Z1dd l2m3Z3m4Z4ddl5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;mZ>m?Z?m@Z@mAZAddlBmCZCddlDZEddlDmFZFmGZGmHZHmIZImJZJmKZKmLZLmMZMmNZNmOZOddlPmQZQmRZRddlSmTZTmUZUmVZVmWZWmXZXmYZYddlZm[Z[ddl\m]Z]ddl^m_Z_e4Z`dZadZbdZcddddgZdee/eed<ddd d!gZfee/eed"<egd#d$d%d&gZhee/eed'<d(d)d*d+gZiee/eed,<edehZjefeiZkeld-d.gZmee/eed/<d0d1gZnee/eed2<eod3d4gZpee/eed5<eqd6d7gZree/eed8<d9d:gZsee/eed;<d<eud?gZvewd@gZxeydAgZzemejZ{e{eperesetevezexZ|de j}eEj~eldBeEjgZeefZdCdDZdEdFZedGdHZdUeee0e1dIdJdKZdVeeqdLdMdNZdWeeqdLdOdPZedQdRZdXdTdUZeeuegfeldVdWdXZdYe)dZd[feeueqfeeuegfeleld\d]d^Zd_d`ZdYeudbdcddZdZeldgdhdiZe jeejeje jdjfdkZe jedleeedmdneje jdjfdkZd[dpdqZd\drdsZdtduZd]dvdwZed^euedxdydzZed{d|Zed}d~ZeudddZdYdae)dadadadZd[df eJeJeeueqfeueeuegfeueueueleleqdd ddZd_eeueqfdddZd`eqeqdddZddZddZdaddZdbddZdcddZddddZdeddZdfddZdgddZdade)ddZd[feleldddZdadYdae)dadddadadadadZd[dfddZdadYdYdae)daddddaddadadZd[dfddZddZdhddZddZddZddZddZeudddZddZdiddZdjddÄZdkddƄZdlddȄZdmddʄZdndd̄Zdodd΄ZdpddЄZÐdqdd҄ZĐdrddՄZŐdsdd؄ZƐdtddڄZǐdudd܄ZddddddddddddddddddddddddddgZɐdvddZddZːdwddZ̐dxddZ͐dyddZΐdzddZϐddZАd{dd Zѐd|d d ZҐd}d d ZӐd~ddZԐdddZՐddZ֐ddZאddZؐdddZِdddZڐdddZېddd Zܐdd"d#Zݐd$d%Zސd&Zߐd'Zd(d)Zdd*d+Zeސd,ecddeefd-d.ZeZeed/dadadfeeeeeufeueueeqd0d1d2Zejdd3d4d5ZGd6d7d7Zed8d9Zedd:d;Zdd=d>ZGd?d@d@eNZGdAdBdBeHZGdCdDdDeFZeeqdEdFdGZddHdIZeeqdJdKdLZeeedMdNdOZeEjjjjZdPdQZeqedRdSdTZdS(N)Counter)contextmanager)datetime)wraps)Path)rmtree) IOAnyCallableContextManagerListOptionalTypeUnioncast)randrandn)can_set_locale get_locales set_locale) no_default)DtypeFilePathOrBuffer FrameOrSeries) get_lzma_file import_lzma) is_boolis_categorical_dtypeis_datetime64_dtypeis_datetime64tz_dtypeis_extension_array_dtypeis_interval_dtype is_numberis_numeric_dtypeis_period_dtype is_sequenceis_timedelta64_dtypeneeds_i8_conversion)array_equivalent) CategoricalCategoricalIndex DataFrame DatetimeIndexIndex IntervalIndex MultiIndex RangeIndexSeries bdate_range) safe_sorttake_1d) DatetimeArrayExtensionArray IntervalArray PeriodArrayTimedeltaArray period_array)DatetimeLikeArrayMixin)urlopen) pprint_thingFZuint8Zuint16Zuint32Zuint64UNSIGNED_INT_DTYPESZUInt8ZUInt16ZUInt32ZUInt64UNSIGNED_EA_INT_DTYPESZint8Zint16Zint32Zint64SIGNED_INT_DTYPESZInt8ZInt16ZInt32ZInt64SIGNED_EA_INT_DTYPESZfloat32Zfloat64 FLOAT_DTYPESZFloat32ZFloat64FLOAT_EA_DTYPESZ complex64Z complex128COMPLEX_DTYPESstrU STRING_DTYPESzdatetime64[ns]zM8[ns]DATETIME64_DTYPESztimedelta64[ns]zm8[ns]TIMEDELTA64_DTYPESboolbytesobjectnancCs&tjdd}d|kr"tdtdS)NPANDAS_TESTING_MODENone deprecatealways)osenvirongetwarnings simplefilter_testing_mode_warnings) testing_moder[3/tmp/pip-unpacked-wheel-q9tj5l6a/pandas/_testing.pyset_testing_modexsr]cCs&tjdd}d|kr"tdtdS)NrPrQrRignore)rTrUrVrWrXrY)rZr[r[r\reset_testing_modesr_cCstjddddS)zJ Reset the display options for printing and representing objects. z ^display.T)ZsilentN)pdZ reset_optionr[r[r[r\reset_display_optionssra)objpathreturnc CsF|}|dkrdtdd}t|}t||t|SQRXdS)a Pickle an object and then read it again. Parameters ---------- obj : any object The object to pickle and then re-read. path : str, path object or file-like object, default None The path where the pickled object is written and then read. Returns ------- pandas object The original object that was pickled and then re-read. N__ z __.pickle)rands ensure_cleanr`Z to_pickleZ read_pickle)rbrc_pathZ temp_pathr[r[r\round_trip_pickles   rj)rcc CsPddl}|dj}|dkr d}t|}||||||}WdQRX|S)a Write an object to file specified by a pathlib.Path and read it back Parameters ---------- writer : callable bound to pandas object IO writing function (e.g. DataFrame.to_csv ) reader : callable IO reading function (e.g. pd.read_csv ) path : str, default None The path where the object is written and then read. Returns ------- pandas object The original object that was serialized and then re-read. rNpathlibZ ___pathlib___)pytest importorskiprrh)writerreaderrcrlrrbr[r[r\round_trip_pathlibs   rpc CsPddl}|dj}|dkr d}t|}||||||}WdQRX|S)a Write an object to file specified by a py.path LocalPath and read it back. Parameters ---------- writer : callable bound to pandas object IO writing function (e.g. DataFrame.to_csv ) reader : callable IO reading function (e.g. pd.read_csv ) path : str, default None The path where the object is written and then read. Returns ------- pandas object The original object that was serialized and then re-read. rNzpy.pathZ___localpath___)rlrmlocalrh)rnrorcrlZ LocalPathrbr[r[r\round_trip_localpaths   rrccs|dkrt|d}n|dkr*t|d}n|dkr@t|d}nn|dkrXtt|d}nV|dkrt|}|}t |dkr|| }qt d|d nt d |z |VWd| |dkr| XdS) a Open a compressed file and return a file object. Parameters ---------- path : str The path where the file is read from. compression : {'gzip', 'bz2', 'zip', 'xz', None} Name of the decompression to use Returns ------- file object Nrbgzipbz2xzzipz ZIP file z error. Only one file per ZIP.zUnrecognized compression type: ) openrtruBZ2FilerlzmazipfileZipFilenamelistlenpop ValueErrorclose)rc compressionfzip_fileZ zip_namesr[r[r\decompress_files(    rtestc Cs|dkrtj}n@|dkr tj}n0|dkr0tj}n |dkrBtt}ntd||dkrjd}||f}d}nd}|f}d }|||d }t |||Wd QRXd S) a Write data to a compressed file. Parameters ---------- compression : {'gzip', 'bz2', 'zip', 'xz'} The compression type to use. path : str The file path to write the data. data : str The data to write. dest : str, default "test" The destination file (for ZIP only) Raises ------ ValueError : An invalid compression value was passed in. rwrtrurvzUnrecognized compression type: wwritestrwbwrite)modeN) r|r}rtGzipFilerurzrr{rgetattr) rrcdatadestZcompress_methodrargsmethodrr[r[r\write_to_compresseds$ r)check_less_preciserdcCs*t|tr|rdSdSndd| SdS)a| Return the tolerance equivalent to the deprecated `check_less_precise` parameter. Parameters ---------- check_less_precise : bool or int Returns ------- float Tolerance to be used as relative/absolute tolerance. Examples -------- >>> # Using check_less_precise as a bool: >>> _get_tol_from_less_precise(False) 0.5e-5 >>> _get_tol_from_less_precise(True) 0.5e-3 >>> # Using check_less_precise as an int representing the decimal >>> # tolerance intended: >>> _get_tol_from_less_precise(2) 0.5e-2 >>> _get_tol_from_less_precise(8) 0.5e-8 gMb@?gh㈵>g?rfN) isinstancerL)rr[r[r\_get_tol_from_less_preciseSs  requivgh㈵>g:0yE>) check_dtyperrtolatolcKs|tk r$tjdtddt|}}t|tjrNt||fd|||d|nt|tj rxt ||fd|||d|nt|tj rt ||fd|||d|nt|rt |rt |rnBt|rt|rn0t|tjst|tjrd}nd}t|||d tj||f|||d |d S) a Check that the left and right objects are approximately equal. By approximately equal, we refer to objects that are numbers or that contain numbers which may be equivalent to specific levels of precision. Parameters ---------- left : object right : object check_dtype : bool or {'equiv'}, default 'equiv' Check dtype if both a and b are the same type. If 'equiv' is passed in, then `RangeIndex` and `Int64Index` are also considered equivalent when doing type checking. check_less_precise : bool or int, default False Specify comparison precision. 5 digits (False) or 3 digits (True) after decimal points are compared. If int, then specify the number of digits to compare. When comparing two numbers, if the first number has magnitude less than 1e-5, we compare the two numbers directly and check whether they are equivalent within the specified precision. Otherwise, we compare the **ratio** of the second number to the first number and check whether it is equivalent to 1 within the specified precision. .. deprecated:: 1.1.0 Use `rtol` and `atol` instead to define relative/absolute tolerance, respectively. Similar to :func:`math.isclose`. rtol : float, default 1e-5 Relative tolerance. .. versionadded:: 1.1.0 atol : float, default 1e-8 Absolute tolerance. .. versionadded:: 1.1.0 zThe 'check_less_precise' keyword in testing.assert_*_equal is deprecated and will be removed in a future version. You can stop passing 'check_less_precise' to silence this warning.) stacklevelF) check_exactexactrr)rrrrz numpy arrayInput)rb)rrrN)rrWwarn FutureWarningrrr`r-assert_index_equalr1assert_series_equalr+assert_frame_equalr"rnpndarrayassert_class_equal_testingassert_almost_equal)leftrightrrrrkwargsrbr[r[r\r|sX.       rcCsZ|j}t||s.t|d|dt|dt||sVt|d|dt|ddS)a Helper method for our assert_* methods that ensures that the two objects being compared have the right type before proceeding with the comparison. Parameters ---------- left : The first object being compared. right : The second object being compared. cls : The class type to check against. Raises ------ AssertionError : Either `left` or `right` is not an instance of `cls`. z Expected type z, found z insteadN)__name__rAssertionErrortype)rrclsZcls_namer[r[r\_check_isinstances  rT) compare_keyscCs t||ttj|||ddS)N)r)rdictrassert_dict_equal)rrrr[r[r\rs rr[?)pcCs t||kS)N)r)sizerr[r[r\randbool srrx)dtypeiiOcCs6tjjt|t|dtj|f|}||S)z, Generate an array of byte strings. )r) rrandomchoice RANDS_CHARSprodviewstr_reshapeastype)ncharsrrretvalr[r[r\ rands_arrays rcCs6tjjt|t|dtj|f|}||S)z/ Generate an array of unicode strings. )r) rrr RANDU_CHARSrrunicode_rr)rrrrr[r[r\ randu_array"s rcCsdtjt|S)zt Generate one random byte string. See `rands_array` if you want to create an array of random strings. r)joinrrrr)rr[r[r\rg.srgcCs>ddlm}m}|dkr2x |D] }||q Wn||dS)Nr)r get_fignums)matplotlib.pyplotrr)Zfignum_closerr[r[r\r8s  r)return_filelikerc kstt}|dkrd}dtjtjtjdd|}||}| t |}|rl| ddt |f|}z |VWdt |t s||r|XdS)az Gets a temporary path and agrees to remove on close. This implementation does not use tempfile.mkstemp to avoid having a file handle. If the code using the returned path wants to delete the file itself, windows requires that no program has a file handle to it. Parameters ---------- filename : str (optional) suffix of the created file. return_filelike : bool (default False) if True, returns a file-like which is *always* cleaned. Necessary for savefig and other functions which want to append extensions. **kwargs Additional keywords are passed to open(). Nrr>)krzw+b)rtempfile gettempdirrrchoicesstring ascii_lettersdigitstouchrG setdefaultryrris_fileunlink)filenamerrfolderrcZ handle_or_strr[r[r\rhFs       rhccs@tjdd}z |VWdy t|Wntk r8YnXXdS)z{ Get a temporary directory path and agrees to remove on close. Yields ------ Temporary directory path r)suffixN)rmkdtemprOSError)Zdirectory_namer[r[r\ensure_clean_dirss   rc cs2ttj}z dVWdtjtj|XdS)z Get a context manager to safely set environment variables All changes will be undone on close, hence environment variables set within this contextmanager will neither persist nor change global state. N)rrTrUclearupdate)Z saved_environr[r[r\!ensure_safe_environment_variabless    r)rdcCst|t|kS)zO Checks if the set of unique elements of arr1 and arr2 are equivalent. ) frozenset)Zarr1Zarr2r[r[r\ equalContentssrr-) rrr check_namesrrcheck_categorical check_orderrrrbrdc  sd} dfdd } dd} |tk r@tjdtdd t|}} t||t| ||| d |j|jkr| d }|jd |}|jd |}t| |||t |t |kr| d }t |d |}t |d |}t| ||||stt |}tt |}|jdkrt t |}t t |}xft |jD]X}| ||}| ||}d|d}t|||||| |d| |j||j|| d q4W|rr||st|j|jktdt |}| dt|dd}t| |||ntj|j|j|| | ||d|r(td||| d t|tjsDt|tjrTtd||| d t|tjspt|tjr~t|j|jrt |j!st |j!rt"|j|j| dd dS)at Check that left and right Index are equal. Parameters ---------- left : Index right : Index exact : bool or {'equiv'}, default 'equiv' Whether to check the Index class, dtype and inferred_type are identical. If 'equiv', then RangeIndex can be substituted for Int64Index as well. check_names : bool, default True Whether to check the names attribute. check_less_precise : bool or int, default False Specify comparison precision. Only used when check_exact is False. 5 digits (False) or 3 digits (True) after decimal points are compared. If int, then specify the digits to compare. .. deprecated:: 1.1.0 Use `rtol` and `atol` instead to define relative/absolute tolerance, respectively. Similar to :func:`math.isclose`. check_exact : bool, default True Whether to compare number exactly. check_categorical : bool, default True Whether to compare internal Categorical exactly. check_order : bool, default True Whether to compare the order of index entries as well as their values. If True, both indexes must contain the same elements, in the same order. If False, both indexes must contain the same elements, but in any order. .. versionadded:: 1.2.0 rtol : float, default 1e-5 Relative tolerance. Only used when check_exact is False. .. versionadded:: 1.1.0 atol : float, default 1e-8 Absolute tolerance. Only used when check_exact is False. .. versionadded:: 1.1.0 obj : str, default 'Index' Specify object name being compared, internally used to show appropriate assertion message. Examples -------- >>> from pandas.testing import assert_index_equal >>> a = pd.Index([1, 2, 3]) >>> b = pd.Index([1, 2, 3]) >>> assert_index_equal(a, b) Tr-csVrRt|||dr(td|||d|jdkrB|jdksRtntd|||ddS)N)rrbr)rbr inferred_type)rassert_attr_equalrr)rrrb)rrr[r\ _check_typess z(assert_index_equal.._check_typescSs:|j|}|j|}t|j||jd}|j||j|dS)N) fill_value)name)levelscodesr4_valuesZ _na_valueZ _shallow_copynames)indexleveluniqueZ level_codesZfilledr[r[r\_get_ilevel_valuess  z.assert_index_equal.._get_ilevel_valueszThe 'check_less_precise' keyword in testing.assert_*_equal is deprecated and will be removed in a future version. You can stop passing 'check_less_precise' to silence this warning.r)r)rbz levels are differentz, z length are differentrxzMultiIndex level [])rrrrrrbgY@z values are different (z %))rrrrblobjZrobjrfreqz categoryN)r-)#rrWrrrrr-nlevelsraise_assert_detailrr3rr/rangerrequalsrsumvaluesrintroundrrrrr`Z PeriodIndexr.assert_interval_array_equalrrrassert_categorical_equal)rrrrrrrrrrrb__tracebackhide__rrmsg1msg2Zmsg3rZllevelZrlevelrdiffmsgr[)rrr\rs?                $rr)rcCsd}dd}|dkrjt|t|krt|jt|jh}t|ddhr|d}t||||||n4|rt|t|kr|d}t||||||d S) z# Checks classes are equal. TcSst|tr|St|jS)N)rr-rr)xr[r[r\ repr_classWs z&assert_class_equal..repr_classrZ Int64Indexr0z classes are not equivalentz classes are differentN)rrrr)rrrrbrr typesrr[r[r\rQs  r Attributes)attrrbc Csd}t||}t||}||kr$dSt|rLt|rLt|rLt|rLdSy ||k}Wntk rpd}YnXt|ts|}|rdSd|d}t||||dS)aO Check attributes are equal. Both objects must have attribute. Parameters ---------- attr : str Attribute name being compared. left : object right : object obj : str, default 'Attributes' Specify object name being compared, internally used to show appropriate assertion message TFz Attribute "z" are differentN) rr"risnan TypeErrorrrLallr) r rrrbrZ left_attrZ right_attrresultrr[r[r\rks(        rcCsddlm}t|tjtjfr^xl|D]0}dtt |j }t||j t fs(t |q(Wn.dtt |j }t||jtt fst |dS)NrzBone of 'objs' is not a matplotlib Axes instance, type encountered zoobjs is neither an ndarray of Artist instances nor a single ArtistArtist instance, tuple, or dict, 'objs' is a )rZpyplotrr`r1rrZravelreprrrZAxesrrZArtisttuple)objsZpltelrr[r[r\"assert_is_valid_plot_return_objects rcCs.t|ttfr|j}t|tt|dS)z#Assert that the sequence is sorted.N)rr-r1rassert_numpy_array_equalrsortarray)seqr[r[r\assert_is_sortedsrr)cCst||t|rDt|j|j|ddt|j|j||ddnxy|j}|j}Wn"tk r~|j|j}}YnXt|||ddt|j|j|j|j|ddt d|||ddS)a{ Test that Categoricals are equivalent. Parameters ---------- left : Categorical right : Categorical check_dtype : bool, default True Check that integer dtype of the codes are the same check_category_order : bool, default True Whether the order of the categories should be compared, which implies identical integer codes. If False, only the resulting values are compared. The ordered attribute is checked regardless. obj : str, default 'Categorical' Specify object name being compared, internally used to show appropriate assertion message z .categories)rbz.codes)rrbz.valuesZorderedN) rr)r categoriesrrZ sort_valuesrZtaker)rrrcheck_category_orderrblcrcr[r[r\rs     rr7cCszt||ti}|jjjdkr&d|d<t|j|jfd|di|t|j|jfd|di|td|||ddS) a Test that two IntervalArrays are equivalent. Parameters ---------- left, right : IntervalArray The IntervalArrays to compare. exact : bool or {'equiv'}, default 'equiv' Whether to check the Index class, dtype and inferred_type are identical. If 'equiv', then RangeIndex can be substituted for Int64Index as well. obj : str, default 'IntervalArray' Specify object name being compared, internally used to show appropriate assertion message )mMF check_freqrbz.leftclosed)rbN)rr7_leftrkind assert_equalZ_rightr)rrrrbrr[r[r\rs   rr8cCs8t||tt|j|j|ddtd|||ddS)Nz._data)rbr)rr8r_datar)rrrbr[r[r\assert_period_array_equals r'r5cCsPd}t||tt|j|j|dd|r._get_baseZsamez is not copyz is cs|dkr|j|jkr,td|j|jd}x,t||D]\}}t||ds<|d7}q._raise)r1rN) rrrrrrr(rr) rrr1rr3Z check_samerbr,rr/Z left_baseZ right_baser4r[)r,rbr1r\r4s$ r)rrc Cs.|tk r$tjdtddt|}}t|ts6tdt|tsHtd|r\td||ddt|t rt|t rt |t |krt t |jt |j|d d St |}t |} t || d |d t ||t} t || t} |rt | | d|d ntj| | |||d|d d S)a- Check that left and right ExtensionArrays are equal. Parameters ---------- left, right : ExtensionArray The two arrays to compare. check_dtype : bool, default True Whether to check if the ExtensionArray dtypes are identical. index_values : numpy.ndarray, default None Optional index (shared by both left and right), used in output. check_less_precise : bool or int, default False Specify comparison precision. Only used when check_exact is False. 5 digits (False) or 3 digits (True) after decimal points are compared. If int, then specify the digits to compare. .. deprecated:: 1.1.0 Use `rtol` and `atol` instead to define relative/absolute tolerance, respectively. Similar to :func:`math.isclose`. check_exact : bool, default False Whether to compare number exactly. rtol : float, default 1e-5 Relative tolerance. Only used when check_exact is False. .. versionadded:: 1.1.0 atol : float, default 1e-8 Absolute tolerance. Only used when check_exact is False. .. versionadded:: 1.1.0 Notes ----- Missing values are checked separately from valid values. A mask of missing values is computed for each and checked to match. The remaining all-valid values are cast to object dtype and checked. Examples -------- >>> from pandas.testing import assert_extension_array_equal >>> a = pd.Series([1, 2, 3, 4]) >>> b, c = a.array, a.array >>> assert_extension_array_equal(b, c) zThe 'check_less_precise' keyword in testing.assert_*_equal is deprecated and will be removed in a future version. You can stop passing 'check_less_precise' to silence this warning.r)rzleft is not an ExtensionArrayzright is not an ExtensionArrayrr6)rb)r,NzExtensionArray NA mask)rbr,)rrrrbr,)rrWrrrrr6rrr;rrrasarrayZasi8ZisnarrNrr) rrrr,rrrrZleft_naZright_naZ left_validZ right_validr[r[r\assert_extension_array_equalsB5   r6r1c Csd}|tk r(tjdtddt|} }t||t|rFt|||dt|t|krt|d|j }t|d|j }t |d||| r|j |j kst t |j dt |j t|j |j |||| | ||d d | r$t|j tjtjfr$|j }|j }|j|jks$t |j|jf|r`t|jrJt|jrJ| sJntd ||d |d|rt|jrt|jrt|j|j|t|t|j d n|rt|jst|jrt|jt|js:d|jd|jd}t |n>t |jr&t |jr&t!|j"|j"nt|js>t|jrht#j$|j|j| ||t|t|j dnt%|jrt%|jrt&|j|j|t|j dnt'|j|jst'|j|jrt&|j|j|t|j dn^t|jrt|jrt&|j|j|t|j dn(t#j$|j|j| ||t|t|j d|rPtd|||d| rt|jsnt|jrt(|j|j|d| ddS)ai Check that left and right Series are equal. Parameters ---------- left : Series right : Series check_dtype : bool, default True Whether to check the Series dtype is identical. check_index_type : bool or {'equiv'}, default 'equiv' Whether to check the Index class, dtype and inferred_type are identical. check_series_type : bool, default True Whether to check the Series class is identical. check_less_precise : bool or int, default False Specify comparison precision. Only used when check_exact is False. 5 digits (False) or 3 digits (True) after decimal points are compared. If int, then specify the digits to compare. When comparing two numbers, if the first number has magnitude less than 1e-5, we compare the two numbers directly and check whether they are equivalent within the specified precision. Otherwise, we compare the **ratio** of the second number to the first number and check whether it is equivalent to 1 within the specified precision. .. deprecated:: 1.1.0 Use `rtol` and `atol` instead to define relative/absolute tolerance, respectively. Similar to :func:`math.isclose`. check_names : bool, default True Whether to check the Series and Index names attribute. check_exact : bool, default False Whether to compare number exactly. check_datetimelike_compat : bool, default False Compare datetime-like which is comparable ignoring dtype. check_categorical : bool, default True Whether to compare internal Categorical exactly. check_category_order : bool, default True Whether to compare category order of internal Categoricals. .. versionadded:: 1.0.2 check_freq : bool, default True Whether to check the `freq` attribute on a DatetimeIndex or TimedeltaIndex. .. versionadded:: 1.1.0 check_flags : bool, default True Whether to check the `flags` attribute. .. versionadded:: 1.2.0 rtol : float, default 1e-5 Relative tolerance. Only used when check_exact is False. .. versionadded:: 1.1.0 atol : float, default 1e-8 Absolute tolerance. Only used when check_exact is False. .. versionadded:: 1.1.0 obj : str, default 'Series' Specify object name being compared, internally used to show appropriate assertion message. Examples -------- >>> from pandas.testing import assert_series_equal >>> a = pd.Series([1, 2, 3, 4]) >>> b = pd.Series([1, 2, 3, 4]) >>> assert_series_equal(a, b) TzThe 'check_less_precise' keyword in testing.assert_*_equal is deprecated and will be removed in a future version. You can stop passing 'check_less_precise' to silence this warning.r)r)rbz, zSeries length are differentz != z.index)rrrrrrrbrzAttributes of )rrbr,zatetimelike_compat=True] z is not equal to .)rrrrbr,)rr,rz category)rbrN))rrWrrrrr1rrrrflagsrrrrr`r,TimedeltaIndexrrrrr#rrrGrr5r'r-rr!rrrrr r60is_extension_array_dtype_and_needs_i8_conversionr)rrrcheck_index_typeZcheck_series_typerrrcheck_datetimelike_compatrrr! check_flagsrrrbrrrZlidxZridxrr[r[r\rsV  *   rr+cCsd}|tk r(tjdtddt|}}t||t|rJt|t|sJt |j |j krzt ||dt |j t |j |r|j |j kst t |j dt |j t|j|j||| | | |||dd t|j|j||| | | |||d d | r|||}}|r|}|}xttt|t|D]:}||ksXt ||ksft t||||||d qFWn~x|t|jD]n\}}||kst |jd d |f}|jd d |f}t||||| || | | |d |d |d||d qWd S)a Check that left and right DataFrame are equal. This function is intended to compare two DataFrames and output any differences. Is is mostly intended for use in unit tests. Additional parameters allow varying the strictness of the equality checks performed. Parameters ---------- left : DataFrame First DataFrame to compare. right : DataFrame Second DataFrame to compare. check_dtype : bool, default True Whether to check the DataFrame dtype is identical. check_index_type : bool or {'equiv'}, default 'equiv' Whether to check the Index class, dtype and inferred_type are identical. check_column_type : bool or {'equiv'}, default 'equiv' Whether to check the columns class, dtype and inferred_type are identical. Is passed as the ``exact`` argument of :func:`assert_index_equal`. check_frame_type : bool, default True Whether to check the DataFrame class is identical. check_less_precise : bool or int, default False Specify comparison precision. Only used when check_exact is False. 5 digits (False) or 3 digits (True) after decimal points are compared. If int, then specify the digits to compare. When comparing two numbers, if the first number has magnitude less than 1e-5, we compare the two numbers directly and check whether they are equivalent within the specified precision. Otherwise, we compare the **ratio** of the second number to the first number and check whether it is equivalent to 1 within the specified precision. .. deprecated:: 1.1.0 Use `rtol` and `atol` instead to define relative/absolute tolerance, respectively. Similar to :func:`math.isclose`. check_names : bool, default True Whether to check that the `names` attribute for both the `index` and `column` attributes of the DataFrame is identical. by_blocks : bool, default False Specify how to compare internal data. If False, compare by columns. If True, compare by blocks. check_exact : bool, default False Whether to compare number exactly. check_datetimelike_compat : bool, default False Compare datetime-like which is comparable ignoring dtype. check_categorical : bool, default True Whether to compare internal Categorical exactly. check_like : bool, default False If True, ignore the order of index & columns. Note: index labels must match their respective rows (same as in columns) - same labels must be with the same data. check_freq : bool, default True Whether to check the `freq` attribute on a DatetimeIndex or TimedeltaIndex. .. versionadded:: 1.1.0 check_flags : bool, default True Whether to check the `flags` attribute. rtol : float, default 1e-5 Relative tolerance. Only used when check_exact is False. .. versionadded:: 1.1.0 atol : float, default 1e-8 Absolute tolerance. Only used when check_exact is False. .. versionadded:: 1.1.0 obj : str, default 'DataFrame' Specify object name being compared, internally used to show appropriate assertion message. See Also -------- assert_series_equal : Equivalent method for asserting Series equality. DataFrame.equals : Check DataFrame equality. Examples -------- This example shows comparing two DataFrames that are equal but with columns of differing dtypes. >>> from pandas._testing import assert_frame_equal >>> df1 = pd.DataFrame({'a': [1, 2], 'b': [3, 4]}) >>> df2 = pd.DataFrame({'a': [1, 2], 'b': [3.0, 4.0]}) df1 equals itself. >>> assert_frame_equal(df1, df1) df1 differs from df2 as column 'b' is of a different type. >>> assert_frame_equal(df1, df2) Traceback (most recent call last): ... AssertionError: Attributes of DataFrame.iloc[:, 1] (column name="b") are different Attribute "dtype" are different [left]: int64 [right]: float64 Ignore differing dtypes in columns with check_dtype. >>> assert_frame_equal(df1, df2, check_dtype=False) TzThe 'check_less_precise' keyword in testing.assert_*_equal is deprecated and will be removed in a future version. You can stop passing 'check_less_precise' to silence this warning.r)rz shape mismatchz != z.index)rrrrrrrrbz.columns)rrbNz .iloc[:, z] (column name="z")) rr;rrr<rr!rbrr)rrWrrrrr+rrrr2rrr8rrcolumnsZ reindex_likeZ_to_dict_of_blockslistsetkeysr enumerateilocr)rrrr;Zcheck_column_typeZcheck_frame_typerrZ by_blocksrr<rZ check_liker!r=rrrbrZrblocksZlblocksricolZlcolZrcolr[r[r\rs~   "*&rcKsdd}t|tjrPt||f|t|tjtjfrL|j|jksLt|j|jfnt|tjrlt ||f|nt|tj rt ||f|nt|t rt ||f|nt|trt||f|nt|trt||f|nt|trt||f|npt|tr t||f|nTt|tjr*t||f|n6t|trT|iksDt||ks`tn tt|dS)aC Wrapper for tm.assert_*_equal to dispatch to the appropriate test function. Parameters ---------- left, right : Index, Series, DataFrame, ExtensionArray, or np.ndarray The two items to be compared. **kwargs All keyword arguments are passed through to the underlying assert method. TN)rr`r-rr,r9rrr1rr+rr7rr8r'r5r)r9r*r6r6rrrrGNotImplementedErrorr)rrrrr[r[r\r%s2           r%cCs|tjkrt|}n|tjkr,t|}n|tjkrBt|}n|tjkrft|}|r|j}nf|tkrxt|}nT|t krt |}nB|t krt |}n0|t j krt |}n|t krt |}nt||S)z Helper function to wrap the expected output of a test in a given box_class. Parameters ---------- expected : np.ndarray, Index, Series box_cls : {Index, Series, DataFrame} Returns ------- subclass of box_cls )r`rr-r1r+Zto_frameTr8r:r5r9rrto_arrayrF)expectedZbox_clsZ transposer[r[r\ box_expecteds,             rJcCsVt|dd}t|rt|St|s,t|r6t|St|rHt|St |SdS)Nr) rr$r:rrr5Z_from_sequencer&r9rr)rbrr[r[r\rHs   rHcCst||tjjt|j|jt|jtjj j s4t t|jtjj j sJt |j}|j}| |spt dd||ntd||td||t||dS)z Check that the left and right SparseArray are equal. Parameters ---------- left : SparseArray right : SparseArray zSparseArray.indexzindex are not equalrrN)rr`ZarraysZ SparseArrayrZ sp_valuesrZsp_index_libssparseZ SparseIndexrrrrZto_dense)rrZ left_indexZ right_indexr[r[r\assert_sp_array_equals    rMcCs,x&|D]}||kstdt|qWdS)NzDid not contain item: )rr)iterableZdicrr[r[r\assert_contains_all5s rOcKs\xVt||D]H\}}t||f|dtt|dtt|d}||k s t|q WdS)z iter1, iter2: iterables that produce elements comparable with assert_almost_equal Checks that the elements are equal, but not the same object. (Does not check that items in sequences are also not the same object) zExpected object z and object z8 to be different objects, but they were the same object.N)rwrrrr)Ziter1Ziter2Z eql_kwargsZelem1Zelem2rr[r[r\ assert_copy:s "rPcCst|ot|S)z Checks that we have the combination of an ExtensionArraydtype and a dtype that should be converted to int64 Returns ------- bool Related to issue #37609 )r r')Z left_dtypeZ right_dtyper[r[r\r:Ls r:cCstjd|S)N)rascii_uppercase)rr[r[r\getColsZsrRrfcCsttd|d|dS)Nrf)rr)r)r-r)rrr[r[r\makeStringIndex_srScCsttd|d|dS)Nrf)rr)r)r-r)rrr[r[r\makeUnicodeIndexcsrTcKs4td|d}ttjt|||dfd|i|S)z' make a length k index or n categories r?)rr)rr)rr*r)Z from_codesrZarange)rnrrrr[r[r\makeCategoricalIndexgs rWcKs*tjdd|dd}tj|fd|i|S)z make a length k IntervalIndex rdrx)numr)rZlinspacer.Z from_breaks)rrrrr[r[r\makeIntervalIndexosrZcCsL|dkrtdg|dS|dkr.tddg|dStddgdg|d|dS)NrxT)rrF)r-)rrr[r[r\ makeBoolIndexus r[cCsttt||dS)N)r)r-r?r)rrr[r[r\ makeIntIndex}sr\cCstddt|D|dS)NcSsg|] }d|qS)lr[).0rDr[r[r\ sz!makeUIntIndex..)r)r-r)rrr[r[r\ makeUIntIndexsr_cKstd|dfd|i|S)Nrrxr)r0)rrrr[r[r\makeRangeIndexsr`cCs:ttj|tjd}t|dtjdd|dS)Nrxrfr )r)sortedrrZ random_sampler-randint)rrrr[r[r\makeFloatIndexsrdBcKs0tddd}t||||d}t|fd|i|S)Nirx)periodsrrr)rr2r,)rrrrdtZdrr[r[r\ makeDateIndexs rhDcKstjfd|||d|S)Nz1 day)startrfrr)r`Ztimedelta_range)rrrrr[r[r\makeTimedeltaIndexsrkcKs&tddd}tjf||d|d|S)Nirxre)rjrfrr)rr`Z period_range)rrrrgr[r[r\makePeriodIndexs rlcKstjdd|i|S)N)Zfoobar)rxrr)rm)r/Z from_product)rrrr[r[r\makeMultiIndexsroZAliceZBobZCharlieZDanZEdithZFrankZGeorgeZHannahZIngridZJerryZKevinZLauraZMichaelZNorbertZOliverZPatriciaZQuinnZRayZSarahZTimZUrsulaZVictorZWendyZXavierZYvonneZZelda 2000-01-01 2000-12-311Dc Cstj|||dd}t|}tj|}|jt|d|jd|d| |dd| |ddd}tj ||t |d}|j d |kr|j d d }|S) a Make a DataFrame with a DatetimeIndex Parameters ---------- start : str or Timestamp, default "2000-01-01" The start of the index. Passed to date_range with `freq`. end : str or Timestamp, default "2000-12-31" The end of the index. Passed to date_range with `freq`. freq : str or Freq The frequency to use for the DatetimeIndex seed : int, optional The random state seed. * name : object dtype with string names * id : int dtype with * x, y : float dtype Examples -------- >>> _make_timeseries() id name x y timestamp 2000-01-01 982 Frank 0.031261 0.986727 2000-01-02 1025 Edith -0.086358 -0.032920 2000-01-03 982 Edith 0.473177 0.298654 2000-01-04 1009 Sarah 0.534344 -0.750377 2000-01-05 963 Zelda -0.271573 0.054424 ... ... ... ... ... 2000-12-27 980 Ingrid -0.132333 -0.422195 2000-12-28 972 Frank -0.376007 -0.298687 2000-12-29 1009 Ursula -0.865047 -0.503133 2000-12-30 1000 Hannah -0.063757 -0.507336 2000-12-31 972 Tim -0.869120 0.531685 timestamp)rjendrr)rirrx)ridry)rr>N)r`Z date_rangerrr RandomStater_namesZpoissonrr+rbrrC) rjrtrseedrrVstater>dfr[r[r\_make_timeseriess$   r}ccs tttttttg}|EdHdS)N)rhrlrkr`rZrWro)make_index_funcsr[r[r\index_subclass_makers_generatorsrccs(tttg}x|D]}||dVqWdS)z Generator which can be iterated over to get instances of all the classes which represent time-series. Parameters ---------- k: length of each of the index instances )rN)rhrlrk)rr~Zmake_index_funcr[r[r\all_timeseries_index_generators  rcCstt}ttt||dS)N)rr)rS_Nr1r)rrr[r[r\makeFloatSeriessrcCstt}ttt||dS)N)rr)rSrr1r)rrr[r[r\makeStringSeriessrcCs*tt}t|td}tt}t|||dS)N)r)rr)rSrr-rNr1)rrrr[r[r\makeObjectSeriess rcsttfddttDS)Ncsi|]}tttd|qS))r)r1rr)r]c)rr[r\ "sz!getSeriesData..)rSrrR_Kr[r[)rr\ getSeriesData srcCs&|dkr t}tt|t||d|dS)N)r)rr)rr1rrh)nperrrr[r[r\makeTimeSeries%srcCs"|dkr t}tt|t||dS)N)rr)rr1rrl)rrr[r[r\makePeriodSeries+srcsfddttDS)Ncsi|]}t|qSr[)r)r]r)rrr[r\r2sz%getTimeSeriesData..)rRr)rrr[)rrr\getTimeSeriesData1srcsfddttDS)Ncsi|]}t|qSr[)r)r]r)rr[r\r6sz!getPeriodData..)rRr)rr[)rr\ getPeriodData5srcCst||}t|S)N)rr+)rrrr[r[r\makeTimeDataFrame:s rcCst}t|S)N)rr+)rr[r[r\ makeDataFrame?srcCsNtdddddg}dddd d gdddddgd d d ddgtdddd}||fS)Nabrdegg?g@g@g@Zfoo1Zfoo2Zfoo3Zfoo4Zfoo5z1/1/2009r)rf)AreCri)r-r2)rrr[r[r\getMixedTypeDictDs    rcCsttdS)Nrx)r+rr[r[r[r\makeMixedDataFrameQsrcCst|}t|S)N)rr+)rrr[r[r\makePeriodFrameUsr#csV|dkrdg|}t|r&t||ks*t|dksR|dksR|dksRt||ksRt|dksn|dkrj|dksnt|dkrfddt|D}|dkrd}t|tr|dkr|g}tttt t t t d |}|r||}|r|d |_|S|dk rtt|d t||kr*|dg|t|t||ks 1 produces multindex) prefix - a string prefix for labels names - (Optional), bool or list of strings. if True will use default names, if false will use no names, if a list is given, the name of each level in the index will be taken from the list. ndupe_l - (Optional), list of ints, the number of rows for which the label will repeated at the corresponding level, you can specify just the first few, the rest will use the default ndupe_l of 1. len(ndupe_l) <= nlevels. idx_type - "i"/"f"/"s"/"u"/"dt"/"p"/"td". If idx_type is not None, `idx_nlevels` must be 1. "i"/"f" creates an integer/float index, "s"/"u" creates a string/unicode index "dt" create a datetime index. "td" create a datetime index. if unspecified, string labels will be generated. NrxFT)rDrsurgrtdcsg|]}t|qSr[)rG)r]rD)prefixr[r\r^}sz#makeCustomIndex..)rDrrrrgrrrzI is not a legal value for `idx_type`, use 'i'/'f'/'s'/'u'/'dt'/'p'/'td'.css|]}|dkVqdS)rNr[)r]rr[r[r\ sz"makeCustomIndex..cSs*ddl}|dd|d}dd|DS)Nrz[^\d_]_?r_cSsg|] }t|qSr[)r)r]rYr[r[r\r^sz4makeCustomIndex..keyfunc..)resubsplit)rrZ numeric_tupler[r[r\keyfuncsz makeCustomIndex..keyfuncZ_lZ_g)key)rcss|]}|dVqdS)rNr[)r]rr[r[r\rs)r)r%rrrrrGr\rdrSrTrhrkrlrVrrrextendrrrbelementsappendr?rwr-r/ from_tuples)Znentriesrrrndupe_lidx_typeZidx_funcidxZtuplesrDrZ div_factorZcntjlabelrrrr[)rr\makeCustomIndexZsd (     rc s|dks t|dkst| dks4| dkr0|dks4t| dksP| dkrL|dksPtt|d||| d} t||d||| d} dkrdd fd d t|D}t|| | | d S) a Create a DataFrame using supplied parameters. Parameters ---------- nrows, ncols - number of data rows/cols c_idx_names, idx_names - False/True/list of strings, yields No names , default names or uses the provided names for the levels of the corresponding index. You can provide a single string when c_idx_nlevels ==1. c_idx_nlevels - number of levels in columns index. > 1 will yield MultiIndex r_idx_nlevels - number of levels in rows index. > 1 will yield MultiIndex data_gen_f - a function f(row,col) which return the data value at that position, the default generator used yields values of the form "RxCy" based on position. c_ndupe_l, r_ndupe_l - list of integers, determines the number of duplicates for each label at a given level of the corresponding index. The default `None` value produces a multiplicity of 1 across all levels, i.e. a unique index. Will accept a partial list of length N < idx_nlevels, for just the first N levels. If ndupe doesn't divide nrows/ncol, the last label might have lower multiplicity. dtype - passed to the DataFrame constructor as is, in case you wish to have more control in conjunction with a custom `data_gen_f` r_idx_type, c_idx_type - "i"/"f"/"s"/"u"/"dt"/"td". If idx_type is not None, `idx_nlevels` must be 1. "i"/"f" creates an integer/float index, "s"/"u" creates a string/unicode index "dt" create a datetime index. "td" create a timedelta index. if unspecified, string labels will be generated. Examples -------- # 5 row, 3 columns, default names on both, single index on both axis >> makeCustomDataframe(5,3) # make the data a random int between 1 and 100 >> mkdf(5,3,data_gen_f=lambda r,c:randint(1,100)) # 2-level multiindex on rows with each label duplicated # twice on first level, default names on both axis, single # index on both axis >> a=makeCustomDataframe(5,3,r_idx_nlevels=2,r_ndupe_l=[2]) # DatetimeIndex on row, index with unicode labels on columns # no names on either axis >> a=makeCustomDataframe(5,3,c_idx_names=False,r_idx_names=False, r_idx_type="dt",c_idx_type="u") # 4-level multindex on rows with names provided, 2-level multindex # on columns with default labels and default names. >> a=makeCustomDataframe(5,3,r_idx_nlevels=4, r_idx_names=["FEE","FI","FO","FAM"], c_idx_nlevels=2) >> a=mkdf(5,3,r_idx_nlevels=2,c_idx_nlevels=4) rN)rDrrrrgrrrxr)rrrrrRcSsd|d|S)Nrrr[)rrr[r[r\) z%makeCustomDataframe..cs$g|]fddtDqS)csg|]}|qSr[r[)r]r) data_gen_frr[r\r^+ sz2makeCustomDataframe...)r)r])rncols)rr\r^+ sz'makeCustomDataframe..)r)rrrr+)nrowsrZ c_idx_namesZ r_idx_namesZ c_idx_nlevelsZ r_idx_nlevelsrZ c_ndupe_lZ r_ndupe_lrZ c_idx_typeZ r_idx_typer>rrr[)rrr\makeCustomDataframes0H  rc s|dkrtj}n tj|}ttd|d}d}t||}fdd}|||}x |jkr|d9}|||}qlWt|dt} || t} | | fS)NrxrgRQ?cs.|t|}tt|dS)N)rrrrfloor)rngZ _extra_sizeind)rrrr[r\_gen_unique_rand= sz-_create_missing_idx.._gen_unique_randg?g?) rrrxrrminrrrtolist) rrdensity random_stateZmin_rowsZfacZ extra_sizerrrrDr[)rrrr\_create_missing_idx0 s   r?cCs0t}t|j||d\}}tj|j||f<|S)N)rr)rrr2rrOr)rrr|rDrr[r[r\makeMissingDataframeK s rcstfdd}|S)aB allows a decorator to take optional positional and keyword arguments. Assumes that taking a single, callable, positional argument means that it is decorating a function, i.e. something like this:: @my_decorator def function(): pass Calls decorator with decorator(f, *args, **kwargs) csNfdd} o,tdko,td}|rFd}g||S|SdS)Ncs|fS)Nr[)r)r decoratorrr[r\decg sz+optional_args..wrapper..decrxr)rcallable)rrrZ is_decoratingr)r)rrr\wrappere szoptional_args..wrapper)r)rrr[)rr\ optional_argsY s r) z timed outz Server Hangupz#HTTP Error 503: Service Unavailablez502: Proxy ErrorzHTTP Error 502: internal errorzHTTP Error 502zHTTP Error 503zHTTP Error 403zHTTP Error 400z$Temporary failure in name resolutionzName or service not knownzConnection refusedzcertificate verify)eonh6<cCsddl}t|jjtfS)Nr) http.clientIOErrorclient HTTPException TimeoutError)httpr[r[r\_get_default_network_errors src CsD|dkrt}yt|WdQRXWn|k r:dSXdSdS)a@ Try to connect to the given url. True if succeeds, False if IOError raised Parameters ---------- url : basestring The URL to try to connect to Returns ------- connectable : bool Return True if no IOError (unable to connect) or URLError (bad url) was raised NFT)rr<)url error_classesr[r[r\ can_connect s rzhttps://www.google.comc sFddlmdkrtd_tfdd}|S)a\ Label a test as requiring network connection and, if an error is encountered, only raise if it does not find a network connection. In comparison to ``network``, this assumes an added contract to your test: you must assert that, under normal conditions, your test will ONLY fail if it does not have network connectivity. You can call this in 3 ways: as a standard decorator, with keyword arguments, or with a positional argument that is the url to check. Parameters ---------- t : callable The test requiring network connectivity. url : path The url to test via ``pandas.io.common.urlopen`` to check for connectivity. Defaults to 'https://www.google.com'. raise_on_error : bool If True, never catches errors. check_before_test : bool If True, checks connectivity before running the test case. error_classes : tuple or Exception error classes to ignore. If not in ``error_classes``, raises the error. defaults to IOError. Be careful about changing the error classes here. skip_errnos : iterable of int Any exception that has .errno or .reason.erno set to one of these values will be skipped with an appropriate message. _skip_on_messages: iterable of string any exception e for which one of the strings is a substring of str(e) will be skipped with an appropriate message. Intended to suppress errors where an errno isn't available. Notes ----- * ``raise_on_error`` supersedes ``check_before_test`` Returns ------- t : callable The decorated test ``t``, with checks for connectivity errors. Example ------- Tests decorated with @network will fail if it's possible to make a network connection to another URL (defaults to google.com):: >>> from pandas._testing import network >>> from pandas.io.common import urlopen >>> @network ... def test_network(): ... with urlopen("rabbit://bonanza.com"): ... pass Traceback ... URLError: You can specify alternative URLs:: >>> @network("https://www.yahoo.com") ... def test_something_with_yahoo(): ... raise IOError("Failure Message") >>> test_something_with_yahoo() Traceback (most recent call last): ... IOError: Failure Message If you set check_before_test, it will check the url first and not run the test on failure:: >>> @network("failing://url.blaher", check_before_test=True) ... def test_something(): ... print("I ran!") ... raise ValueError("Failure") >>> test_something() Traceback (most recent call last): ... Errors not related to networking will always be raised. r)skipNTc srstsy ||Stk r}zt|dd}|s\t|dr\t|jdd}|krrd|t|tfddDrd|t|sstrnd|Wdd}~XYnXdS)Nerrnoreasonz+Skipping test due to known errno and error c3s|]}|kVqdS)N)lower)r]r)e_strr[r\r: sz+network..wrapper..z;Skipping test because exception message is known and error z4Skipping test due to lack of connectivity and error )r ExceptionrhasattrrrGanyr)rrerrr)_skip_on_messagescheck_before_testrraise_on_errorr skip_errnostr)rr\r$ s*     znetwork..wrapper)rlrrnetworkr)rrrrrrrrr[)rrrrrrrrr\r s \ "#rrS)expected_warningcheck_stacklevelraise_on_extra_warningsmatchc cs8d}tjdd}d}d}t||Vg} x|D]} |s@q6ttt|}t| j|rd}|rzt| jtt frzt | |dk rt |t | jrd}q6| | jj| j| j| jfq6W|r ttt|}|stdt|j|r |s tdt|jd||r*| r*tdt| WdQRXdS) a Context manager for running code expected to either raise a specific warning, or not raise any warnings. Verifies that the code raises the expected warning, and that it does not raise any other unexpected warnings. It is basically a wrapper around ``warnings.catch_warnings``. Parameters ---------- expected_warning : {Warning, False, None}, default Warning The type of Exception raised. ``exception.Warning`` is the base class for all warnings. To check that no warning is returned, specify ``False`` or ``None``. filter_level : str or None, default "always" Specifies whether warnings are ignored, displayed, or turned into errors. Valid values are: * "error" - turns matching warnings into exceptions * "ignore" - discard the warning * "always" - always emit a warning * "default" - print the warning the first time it is generated from each location * "module" - print the warning the first time it is generated from each module * "once" - print the warning the first time it is generated check_stacklevel : bool, default True If True, displays the line that called the function containing the warning to show were the function is called. Otherwise, the line that implements the function is displayed. raise_on_extra_warnings : bool, default True Whether extra warnings not of the type `expected_warning` should cause the test to fail. match : str, optional Match warning message. Examples -------- >>> import warnings >>> with assert_produces_warning(): ... warnings.warn(UserWarning()) ... >>> with assert_produces_warning(False): ... warnings.warn(RuntimeWarning()) ... Traceback (most recent call last): ... AssertionError: Caused unexpected warning(s): ['RuntimeWarning']. >>> with assert_produces_warning(UserWarning): ... warnings.warn(RuntimeWarning()) Traceback (most recent call last): ... AssertionError: Did not see expected warning of class 'UserWarning'. ..warn:: This is *not* thread-safe. T)recordFNz&Did not see expected warning of class zDid not see warning z matching zCaused unexpected warning(s): )rWcatch_warningsrXrrWarning issubclasscategoryrDeprecationWarning&_assert_raised_with_correct_stacklevelrsearchrGr+rrrlinenorr) rZ filter_levelrrrrrZ saw_warningZmatched_messageZextra_warningsactual_warningr[r[r\assert_produces_warningM sD@     r)rrdcCsVddlm}m}||dd}d|jd|jd|j}|j|jksRt|dS)Nr) getframeinfostackrUzGWarning not set with correct stacklevel. File where warning is raised: z != z. Warning message: )inspectrrrr+r)rrrZcallerrr[r[r\r src@s(eZdZdZddZddZddZdS) RNGContexta, Context manager to set the numpy random number generator speed. Returns to the original value upon exiting the context manager. Parameters ---------- seed : int Seed for numpy.random.seed Examples -------- with RNGContext(42): np.random.randn() cCs ||_dS)N)rz)selfrzr[r[r\__init__ szRNGContext.__init__cCstj|_tj|jdS)N)rrZ get_state start_staterz)rr[r[r\ __enter__ s zRNGContext.__enter__cCstj|jdS)N)rrZ set_stater)rexc_type exc_value tracebackr[r[r\__exit__ szRNGContext.__exit__N)r __module__ __qualname____doc__rrrr[r[r[r\r srcksDddl}dddh}||kr"td|j|f|dV||dS)au Context manager to temporarily register a CSV dialect for parsing CSV. Parameters ---------- name : str The name of the dialect. kwargs : mapping The parameters for the dialect. Raises ------ ValueError : the name of the dialect conflicts with a builtin one. See Also -------- csv : Python's CSV library. rNexcelz excel-tabunixz Cannot override builtin dialect.)csvrregister_dialectunregister_dialect)rrrZ_BUILTIN_DIALECTSr[r[r\with_csv_dialect s r ccsPddlm}|dkr|j}|j}|j}||||_dV||_||dS)Nr) expressions)Zpandas.core.computationr Z _MIN_ELEMENTSZ USE_NUMEXPRZset_use_numexpr)ZuseZ min_elementsexprZolduseZoldminr[r[r\ use_numexpr s  r rcsFdks tdk r(tks(tddlfdd}|S)a  Decorator to run the same function multiple times in parallel. Parameters ---------- num_threads : int, optional The number of times the function is run in parallel. kwargs_list : list of dicts, optional The list of kwargs to update original function kwargs on different threads. Notes ----- This decorator does not pass the return value of the decorated function. Original from scikit-image: https://github.com/scikit-image/scikit-image/pull/1519 rNcs tfdd}|S)Ncsrfdd}n fdd}g}x2tD]&}||}j||d}||q.Wx|D] }|q^Wx|D] }|qtWdS)Ncstf|S)N)r)rD)r kwargs_listr[r\r@ rz?test_parallel..wrapper..inner..csS)Nr[)rD)rr[r\rB r)targetrr)rThreadrrjr)rrZ update_kwargsthreadsrDZupdated_kwargsthread)funchas_kwargs_listr  num_threads threading)rr\inner= s    z-test_parallel..wrapper..inner)r)rr)rr rr)rr\r< sztest_parallel..wrapper)rrr)rr rr[)rr rrr\ test_parallel! s rc@s,eZdZddgZeddZeddZdS)SubclassedSeriestestattrrcCstS)N)r)rr[r[r\ _constructorU szSubclassedSeries._constructorcCstS)N)SubclassedDataFrame)rr[r[r\_constructor_expanddimY sz'SubclassedSeries._constructor_expanddimN)rrr _metadatapropertyrrr[r[r[r\rR s rc@s*eZdZdgZeddZeddZdS)rrcCstS)N)r)rr[r[r\ra sz SubclassedDataFrame._constructorcCstS)N)r)rr[r[r\_constructor_slicede sz'SubclassedDataFrame._constructor_slicedN)rrrrrrrr[r[r[r\r^ s rc@seZdZeddZdS)SubclassedCategoricalcCstS)N)r )rr[r[r\rk sz"SubclassedCategorical._constructorN)rrrrrr[r[r[r\r j sr )r(c#sLddlddlfdd}jd}||z dVWd||XdS)a Context manager for temporarily setting a timezone. Parameters ---------- tz : str A string representing a valid timezone. Examples -------- >>> from datetime import datetime >>> from dateutil.tz import tzlocal >>> tzlocal().tzname(datetime.now()) 'IST' >>> with set_timezone('US/Eastern'): ... tzlocal().tzname(datetime.now()) ... 'EDT' rNcsB|dkr,y jd=Wq>tk r(Yq>Xn|jd<dS)NTZ)rUKeyErrortzset)r()rTtimer[r\setTZ s  zset_timezone..setTZr!)rTr$rUrV)r(r%Zorig_tzr[)rTr$r\ set_timezonep s  r&cs"rfdd}n fdd}|S)a` Create a function for calling on an array. Parameters ---------- alternative : function The function to be called on the array with no NaNs. Only used when 'skipna_alternative' is None. skipna_alternative : function The function to be called on the original array Returns ------- function cs |jS)N)r)r)skipna_alternativer[r\skipna_wrapper sz,_make_skipna_wrapper..skipna_wrappercs"|}t|dkrtjS|S)Nr)ZdropnarrrO)rZnona) alternativer[r\r( s r[)r)r'r(r[)r)r'r\_make_skipna_wrapper s r*) rows_listcCstj}|||S)aW Convert list of CSV rows to single CSV-formatted string for current OS. This method is used for creating expected value of to_csv() method. Parameters ---------- rows_list : List[str] Each element represents the row of csv. Returns ------- str Expected output of to_csv() in current OS. )rTlinesepr)r+sepr[r[r\convert_rows_list_to_csv_str sr.)expected_exceptionrdcCsddl}|j|ddS)a$ Helper function to mark pytest.raises that have an external error message. Parameters ---------- expected_exception : Exception Expected error to raise. Returns ------- Callable Regular `pytest.raises` function with `match` equal to `None`. rN)r)rlZraises)r/rlr[r[r\external_error_raised sr0csDg}x:|D]2\|f|fddtD7}q W|S)a Combine frame, functions from SelectionMixin._cython_table keys and expected result. Parameters ---------- ndframe : DataFrame or Series func_names_and_expected : Sequence of two items The first item is a name of a NDFrame method ('sum', 'prod') etc. The second item is the expected return value. Returns ------- list List of three items (DataFrame, function, expected result) cs"g|]\}}|kr|fqSr[r[)r]rr)rI func_namendframer[r\r^ sz+get_cython_table_params..)r cython_table)r2Zfunc_names_and_expectedresultsr[)rIr1r2r\get_cython_table_params sr5)op_namerdcsP|d}ytt|}Wn2tk rJtt|ddfdd}YnX|S)z The operator function for a given op name. Parameters ---------- op_name : string The op name, in form of "add" or "__add__". Returns ------- function A function performing the operation. rrxNcs ||S)Nr[)rrv)ropr[r\r rz"get_op_from_name..)striproperatorAttributeError)r6Z short_opnameopr[)r7r\get_op_from_name s r<)N)N)N)r)T)r[r)r)r)N)NF)Tr)r )TTr))rr7)r8)r5T)r9T)NN)FTNNr-N)T)rfN)rfN)rfrUN)rfN)rfN)rfN)rfN)rfN)rfN)rfreN)rfriN)rfN)rfN)rprqrrN)rf)N)N)N)NreN)NN)Nre)N)Nre)N)rFNN) TTrxrxNNNNNN)N)rN)N)N)rN)N)ru collectionsr contextlibrr functoolsrrtr9rTrkrrrshutilrrrtypingrr r r r r rrrrWr|ZnumpyrZ numpy.randomrrZpandas._config.localizationrrrZpandas._libs.librZpandas._libs.testingrKZtestingrZpandas._typingrrrZ pandas.compatrrZpandas.core.dtypes.commonrrrrr r!r"r#r$r%r&r'Zpandas.core.dtypes.missingr(Zpandasr`r)r*r+r,r-r.r/r0r1r2Zpandas.core.algorithmsr3r4Zpandas.core.arraysr5r6r7r8r9r:Zpandas.core.arrays.datetimeliker;Zpandas.io.commonr<Zpandas.io.formats.printingr=r{rrZ_RAISE_NETWORK_ERROR_DEFAULTr@__annotations__rArrBrCZALL_INT_DTYPESZALL_EA_INT_DTYPESfloatrDrEcomplexrFrGrIrJrKrLZ BOOL_DTYPESrMZ BYTES_DTYPESrNZ OBJECT_DTYPESZALL_REAL_DTYPESZALL_NUMPY_DTYPESrOZNaTZNAZ NULL_OBJECTSrResourceWarningrYr]r_rarjrprrrrrrrrrrr?rrrrrmapchrrrrrrrgrrhrrrrrrrrrrr'r)r*rrr6rrr%rJrHrMrOrPr:rRrSrTrWrZr[r\r_r`rdrhrkrlroryr}rrrrrrrrrrrrrrrrrrrrZ_network_error_messagesZ_network_errno_valsrrrZwith_connectivity_checkrrWarningMessagerrr r rrrr r&r*r.rr0corer.ZSelectionMixinZ _cython_tableitemsr3r5r<r[r[r[r\s      , 8 0          1 4,(g   ,   *%- ,    ! Kd Y I( *%              3    m_ $ 4t  1  *