<s> from __future__ import division <EOL> import re <EOL> import string <EOL> import sys <EOL> import tempfile <EOL> import warnings <EOL> import inspect <EOL> import os <EOL> import subprocess <EOL> import locale <EOL> import unittest <EOL> import traceback <EOL> from datetime import datetime <EOL> from functools import wraps , partial <EOL> from contextlib import contextmanager <EOL> from distutils . version import LooseVersion <EOL> from numpy . random import randn , rand <EOL> import numpy as np <EOL> import pandas as pd <EOL> from pandas . core . common import ( is_sequence , array_equivalent , <EOL> is_list_like , is_datetimelike_v_numeric , <EOL> is_datetimelike_v_object , is_number , <EOL> needs_i8_conversion ) <EOL> from pandas . formats . printing import pprint_thing <EOL> from pandas . core . algorithms import take_1d <EOL> import pandas . compat as compat <EOL> import pandas . lib as lib <EOL> from pandas . compat import ( <EOL> filter , map , zip , range , unichr , lrange , lmap , lzip , u , callable , Counter , <EOL> raise_with_traceback , httplib , is_platform_windows , is_platform_32bit , <EOL> PY3 <EOL> ) <EOL> from pandas . computation import expressions as expr <EOL> from pandas import ( bdate_range , CategoricalIndex , DatetimeIndex , <EOL> TimedeltaIndex , PeriodIndex , RangeIndex , Index , MultiIndex , <EOL> Series , DataFrame , Panel , Panel4D ) <EOL> from pandas . util . decorators import deprecate <EOL> from pandas import _testing <EOL> from pandas . io . common import urlopen <EOL> N = <NUM_LIT:30> <EOL> K = <NUM_LIT:4> <EOL> _RAISE_NETWORK_ERROR_DEFAULT = False <EOL> _testing_mode_warnings = ( DeprecationWarning , compat . ResourceWarning ) <EOL> def set_testing_mode ( ) : <EOL> testing_mode = os . environ . get ( '<STR_LIT>' , '<STR_LIT:None>' ) <EOL> if '<STR_LIT>' in testing_mode : <EOL> warnings . simplefilter ( '<STR_LIT>' , _testing_mode_warnings ) <EOL> def reset_testing_mode ( ) : <EOL> testing_mode = os . environ . get ( '<STR_LIT>' , '<STR_LIT:None>' ) <EOL> if '<STR_LIT>' in testing_mode : <EOL> warnings . simplefilter ( '<STR_LIT:ignore>' , _testing_mode_warnings ) <EOL> set_testing_mode ( ) <EOL> class TestCase ( unittest . TestCase ) : <EOL> @ classmethod <EOL> def setUpClass ( cls ) : <EOL> pd . set_option ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> @ classmethod <EOL> def tearDownClass ( cls ) : <EOL> pass <EOL> def reset_display_options ( self ) : <EOL> pd . reset_option ( '<STR_LIT>' , silent = True ) <EOL> def round_trip_pickle ( self , obj , path = None ) : <EOL> if path is None : <EOL> path = u ( '<STR_LIT>' % rands ( <NUM_LIT:10> ) ) <EOL> with ensure_clean ( path ) as path : <EOL> pd . to_pickle ( obj , path ) <EOL> return pd . read_pickle ( path ) <EOL> def assertEquals ( self , * args , ** kwargs ) : <EOL> return deprecate ( '<STR_LIT>' , <EOL> self . assertEqual ) ( * args , ** kwargs ) <EOL> def assertNotEquals ( self , * args , ** kwargs ) : <EOL> return deprecate ( '<STR_LIT>' , <EOL> self . assertNotEqual ) ( * args , ** kwargs ) <EOL> def assert_ ( self , * args , ** kwargs ) : <EOL> return deprecate ( '<STR_LIT>' , <EOL> self . assertTrue ) ( * args , ** kwargs ) <EOL> def assertAlmostEquals ( self , * args , ** kwargs ) : <EOL> return deprecate ( '<STR_LIT>' , <EOL> self . assertAlmostEqual ) ( * args , ** kwargs ) <EOL> def assertNotAlmostEquals ( self , * args , ** kwargs ) : <EOL> return deprecate ( '<STR_LIT>' , <EOL> self . assertNotAlmostEqual ) ( * args , ** kwargs ) <EOL> def assert_almost_equal ( left , right , check_exact = False , ** kwargs ) : <EOL> if isinstance ( left , pd . Index ) : <EOL> return assert_index_equal ( left , right , check_exact = check_exact , <EOL> ** kwargs ) <EOL> elif isinstance ( left , pd . Series ) : <EOL> return assert_series_equal ( left , right , check_exact = check_exact , <EOL> ** kwargs ) <EOL> elif isinstance ( left , pd . DataFrame ) : <EOL> return assert_frame_equal ( left , right , check_exact = check_exact , <EOL> ** kwargs ) <EOL> return _testing . assert_almost_equal ( left , right , ** kwargs ) <EOL> assert_dict_equal = _testing . assert_dict_equal <EOL> def randbool ( size = ( ) , p = <NUM_LIT:0.5> ) : <EOL> return rand ( * size ) <= p <EOL> RANDS_CHARS = np . array ( list ( string . ascii_letters + string . digits ) , <EOL> dtype = ( np . str_ , <NUM_LIT:1> ) ) <EOL> RANDU_CHARS = np . array ( list ( u ( "<STR_LIT>" ) . join ( map ( unichr , lrange ( <NUM_LIT> , <NUM_LIT> + <NUM_LIT> ) ) ) + <EOL> string . digits ) , dtype = ( np . unicode_ , <NUM_LIT:1> ) ) <EOL> def rands_array ( nchars , size , dtype = '<STR_LIT:O>' ) : <EOL> """<STR_LIT>""" <EOL> retval = ( np . random . choice ( RANDS_CHARS , size = nchars * np . prod ( size ) ) <EOL> . view ( ( np . str_ , nchars ) ) . reshape ( size ) ) <EOL> if dtype is None : <EOL> return retval <EOL> else : <EOL> return retval . astype ( dtype ) <EOL> def randu_array ( nchars , size , dtype = '<STR_LIT:O>' ) : <EOL> """<STR_LIT>""" <EOL> retval = ( np . random . choice ( RANDU_CHARS , size = nchars * np . prod ( size ) ) <EOL> . view ( ( np . unicode_ , nchars ) ) . reshape ( size ) ) <EOL> if dtype is None : <EOL> return retval <EOL> else : <EOL> return retval . astype ( dtype ) <EOL> def rands ( nchars ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' . join ( np . random . choice ( RANDS_CHARS , nchars ) ) <EOL> def randu ( nchars ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' . join ( np . random . choice ( RANDU_CHARS , nchars ) ) <EOL> def close ( fignum = None ) : <EOL> from matplotlib . pyplot import get_fignums , close as _close <EOL> if fignum is None : <EOL> for fignum in get_fignums ( ) : <EOL> _close ( fignum ) <EOL> else : <EOL> _close ( fignum ) <EOL> def _skip_if_32bit ( ) : <EOL> import nose <EOL> if is_platform_32bit ( ) : <EOL> raise nose . SkipTest ( "<STR_LIT>" ) <EOL> def mplskip ( cls ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def setUpClass ( cls ) : <EOL> try : <EOL> import matplotlib as mpl <EOL> mpl . use ( "<STR_LIT>" , warn = False ) <EOL> except ImportError : <EOL> import nose <EOL> raise nose . SkipTest ( "<STR_LIT>" ) <EOL> cls . setUpClass = setUpClass <EOL> return cls <EOL> def _skip_if_no_mpl ( ) : <EOL> try : <EOL> import matplotlib <EOL> except ImportError : <EOL> import nose <EOL> raise nose . SkipTest ( "<STR_LIT>" ) <EOL> def _skip_if_mpl_1_5 ( ) : <EOL> import matplotlib <EOL> v = matplotlib . __version__ <EOL> if v > LooseVersion ( '<STR_LIT>' ) or v [ <NUM_LIT:0> ] == '<STR_LIT:0>' : <EOL> import nose <EOL> raise nose . SkipTest ( "<STR_LIT>" ) <EOL> def _skip_if_no_scipy ( ) : <EOL> try : <EOL> import scipy . stats <EOL> except ImportError : <EOL> import nose <EOL> raise nose . SkipTest ( "<STR_LIT>" ) <EOL> try : <EOL> import scipy . interpolate <EOL> except ImportError : <EOL> import nose <EOL> raise nose . SkipTest ( '<STR_LIT>' ) <EOL> def _skip_if_scipy_0_17 ( ) : <EOL> import scipy <EOL> v = scipy . __version__ <EOL> if v >= LooseVersion ( "<STR_LIT>" ) : <EOL> import nose <EOL> raise nose . SkipTest ( "<STR_LIT>" ) <EOL> def _skip_if_no_lzma ( ) : <EOL> try : <EOL> return compat . import_lzma ( ) <EOL> except ImportError : <EOL> import nose <EOL> raise nose . SkipTest ( '<STR_LIT>' ) <EOL> def _skip_if_no_xarray ( ) : <EOL> try : <EOL> import xarray <EOL> except ImportError : <EOL> import nose <EOL> raise nose . SkipTest ( "<STR_LIT>" ) <EOL> v = xarray . __version__ <EOL> if v < LooseVersion ( '<STR_LIT>' ) : <EOL> import nose <EOL> raise nose . SkipTest ( "<STR_LIT>" . format ( v ) ) <EOL> def _skip_if_no_pytz ( ) : <EOL> try : <EOL> import pytz <EOL> except ImportError : <EOL> import nose <EOL> raise nose . SkipTest ( "<STR_LIT>" ) <EOL> def _skip_if_no_dateutil ( ) : <EOL> try : <EOL> import dateutil <EOL> except ImportError : <EOL> import nose <EOL> raise nose . SkipTest ( "<STR_LIT>" ) <EOL> def _skip_if_windows_python_3 ( ) : <EOL> if PY3 and is_platform_windows ( ) : <EOL> import nose <EOL> raise nose . SkipTest ( "<STR_LIT>" ) <EOL> def _skip_if_windows ( ) : <EOL> if is_platform_windows ( ) : <EOL> import nose <EOL> raise nose . SkipTest ( "<STR_LIT>" ) <EOL> def _skip_if_no_pathlib ( ) : <EOL> try : <EOL> from pathlib import Path <EOL> except ImportError : <EOL> import nose <EOL> raise nose . SkipTest ( "<STR_LIT>" ) <EOL> def _skip_if_no_localpath ( ) : <EOL> try : <EOL> from py . path import local as LocalPath <EOL> except ImportError : <EOL> import nose <EOL> raise nose . SkipTest ( "<STR_LIT>" ) <EOL> def _incompat_bottleneck_version ( method ) : <EOL> """<STR_LIT>""" <EOL> if method not in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> return False <EOL> try : <EOL> import bottleneck as bn <EOL> return bn . __version__ >= LooseVersion ( '<STR_LIT:1.0>' ) <EOL> except ImportError : <EOL> return False <EOL> def skip_if_no_ne ( engine = '<STR_LIT>' ) : <EOL> from pandas . computation . expressions import ( _USE_NUMEXPR , <EOL> _NUMEXPR_INSTALLED ) <EOL> if engine == '<STR_LIT>' : <EOL> if not _USE_NUMEXPR : <EOL> import nose <EOL> raise nose . SkipTest ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( <EOL> enabled = _USE_NUMEXPR , <EOL> installed = _NUMEXPR_INSTALLED ) ) <EOL> def _skip_if_has_locale ( ) : <EOL> import locale <EOL> lang , _ = locale . getlocale ( ) <EOL> if lang is not None : <EOL> import nose <EOL> raise nose . SkipTest ( "<STR_LIT>" . format ( lang ) ) <EOL> def check_output ( * popenargs , ** kwargs ) : <EOL> r"""<STR_LIT>""" <EOL> if '<STR_LIT>' in kwargs : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> process = subprocess . Popen ( stdout = subprocess . PIPE , stderr = subprocess . PIPE , <EOL> * popenargs , ** kwargs ) <EOL> output , unused_err = process . communicate ( ) <EOL> retcode = process . poll ( ) <EOL> if retcode : <EOL> cmd = kwargs . get ( "<STR_LIT:args>" ) <EOL> if cmd is None : <EOL> cmd = popenargs [ <NUM_LIT:0> ] <EOL> raise subprocess . CalledProcessError ( retcode , cmd , output = output ) <EOL> return output <EOL> def _default_locale_getter ( ) : <EOL> try : <EOL> raw_locales = check_output ( [ '<STR_LIT>' ] , shell = True ) <EOL> except subprocess . CalledProcessError as e : <EOL> raise type ( e ) ( "<STR_LIT>" <EOL> "<STR_LIT>" % e ) <EOL> return raw_locales <EOL> def get_locales ( prefix = None , normalize = True , <EOL> locale_getter = _default_locale_getter ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> raw_locales = locale_getter ( ) <EOL> except : <EOL> return None <EOL> try : <EOL> raw_locales = raw_locales . split ( b'<STR_LIT:\n>' ) <EOL> out_locales = [ ] <EOL> for x in raw_locales : <EOL> if PY3 : <EOL> out_locales . append ( str ( <EOL> x , encoding = pd . options . display . encoding ) ) <EOL> else : <EOL> out_locales . append ( str ( x ) ) <EOL> except TypeError : <EOL> pass <EOL> if prefix is None : <EOL> return _valid_locales ( out_locales , normalize ) <EOL> found = re . compile ( '<STR_LIT>' % prefix ) . findall ( '<STR_LIT:\n>' . join ( out_locales ) ) <EOL> return _valid_locales ( found , normalize ) <EOL> @ contextmanager <EOL> def set_locale ( new_locale , lc_var = locale . LC_ALL ) : <EOL> """<STR_LIT>""" <EOL> current_locale = locale . getlocale ( ) <EOL> try : <EOL> locale . setlocale ( lc_var , new_locale ) <EOL> try : <EOL> normalized_locale = locale . getlocale ( ) <EOL> except ValueError : <EOL> yield new_locale <EOL> else : <EOL> if all ( lc is not None for lc in normalized_locale ) : <EOL> yield '<STR_LIT:.>' . join ( normalized_locale ) <EOL> else : <EOL> yield new_locale <EOL> finally : <EOL> locale . setlocale ( lc_var , current_locale ) <EOL> def _can_set_locale ( lc ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> with set_locale ( lc ) : <EOL> pass <EOL> except locale . Error : <EOL> return False <EOL> else : <EOL> return True <EOL> def _valid_locales ( locales , normalize ) : <EOL> """<STR_LIT>""" <EOL> if normalize : <EOL> normalizer = lambda x : locale . normalize ( x . strip ( ) ) <EOL> else : <EOL> normalizer = lambda x : x . strip ( ) <EOL> return list ( filter ( _can_set_locale , map ( normalizer , locales ) ) ) <EOL> def debug ( f , * args , ** kwargs ) : <EOL> from pdb import Pdb as OldPdb <EOL> try : <EOL> from IPython . core . debugger import Pdb <EOL> kw = dict ( color_scheme = '<STR_LIT>' ) <EOL> except ImportError : <EOL> Pdb = OldPdb <EOL> kw = { } <EOL> pdb = Pdb ( ** kw ) <EOL> return pdb . runcall ( f , * args , ** kwargs ) <EOL> def pudebug ( f , * args , ** kwargs ) : <EOL> import pudb <EOL> return pudb . runcall ( f , * args , ** kwargs ) <EOL> def set_trace ( ) : <EOL> from IPython . core . debugger import Pdb <EOL> try : <EOL> Pdb ( color_scheme = '<STR_LIT>' ) . set_trace ( sys . _getframe ( ) . f_back ) <EOL> except : <EOL> from pdb import Pdb as OldPdb <EOL> OldPdb ( ) . set_trace ( sys . _getframe ( ) . f_back ) <EOL> @ contextmanager <EOL> def ensure_clean ( filename = None , return_filelike = False ) : <EOL> """<STR_LIT>""" <EOL> filename = filename or '<STR_LIT>' <EOL> fd = None <EOL> if return_filelike : <EOL> f = tempfile . TemporaryFile ( suffix = filename ) <EOL> try : <EOL> yield f <EOL> finally : <EOL> f . close ( ) <EOL> else : <EOL> if len ( os . path . dirname ( filename ) ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> try : <EOL> fd , filename = tempfile . mkstemp ( suffix = filename ) <EOL> except UnicodeEncodeError : <EOL> import nose <EOL> raise nose . SkipTest ( '<STR_LIT>' ) <EOL> try : <EOL> yield filename <EOL> finally : <EOL> try : <EOL> os . close ( fd ) <EOL> except Exception as e : <EOL> print ( "<STR_LIT>" % <EOL> ( fd , filename ) ) <EOL> try : <EOL> if os . path . exists ( filename ) : <EOL> os . remove ( filename ) <EOL> except Exception as e : <EOL> print ( "<STR_LIT>" % e ) <EOL> def get_data_path ( f = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> _ , filename , _ , _ , _ , _ = inspect . getouterframes ( inspect . currentframe ( ) ) [ <NUM_LIT:1> ] <EOL> base_dir = os . path . abspath ( os . path . dirname ( filename ) ) <EOL> return os . path . join ( base_dir , '<STR_LIT:data>' , f ) <EOL> def equalContents ( arr1 , arr2 ) : <EOL> """<STR_LIT>""" <EOL> return frozenset ( arr1 ) == frozenset ( arr2 ) <EOL> def assert_equal ( a , b , msg = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> assert a == b , "<STR_LIT>" % ( msg . format ( a , b ) , a , b ) <EOL> def assert_index_equal ( left , right , exact = '<STR_LIT>' , check_names = True , <EOL> check_less_precise = False , check_exact = True , <EOL> obj = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> def _check_types ( l , r , obj = '<STR_LIT>' ) : <EOL> if exact : <EOL> if exact == '<STR_LIT>' : <EOL> if type ( l ) != type ( r ) : <EOL> types = set ( [ type ( l ) . __name__ , type ( r ) . __name__ ] ) <EOL> if len ( types - set ( [ '<STR_LIT>' , '<STR_LIT>' ] ) ) : <EOL> msg = '<STR_LIT>' . format ( obj ) <EOL> raise_assert_detail ( obj , msg , l , r ) <EOL> else : <EOL> if type ( l ) != type ( r ) : <EOL> msg = '<STR_LIT>' . format ( obj ) <EOL> raise_assert_detail ( obj , msg , l , r ) <EOL> assert_attr_equal ( '<STR_LIT>' , l , r , obj = obj ) <EOL> if l . inferred_type in ( '<STR_LIT:string>' , '<STR_LIT>' ) : <EOL> assertIn ( r . inferred_type , ( '<STR_LIT:string>' , '<STR_LIT>' ) ) <EOL> else : <EOL> assert_attr_equal ( '<STR_LIT>' , l , r , obj = obj ) <EOL> def _get_ilevel_values ( index , level ) : <EOL> unique = index . levels [ level ] <EOL> labels = index . labels [ level ] <EOL> filled = take_1d ( unique . values , labels , fill_value = unique . _na_value ) <EOL> values = unique . _simple_new ( filled , <EOL> name = index . names [ level ] , <EOL> freq = getattr ( unique , '<STR_LIT>' , None ) , <EOL> tz = getattr ( unique , '<STR_LIT>' , None ) ) <EOL> return values <EOL> assertIsInstance ( left , Index , '<STR_LIT>' ) <EOL> assertIsInstance ( right , Index , '<STR_LIT>' ) <EOL> _check_types ( left , right , obj = obj ) <EOL> if left . nlevels != right . nlevels : <EOL> raise_assert_detail ( obj , '<STR_LIT>' . format ( obj ) , <EOL> '<STR_LIT>' . format ( left . nlevels , left ) , <EOL> '<STR_LIT>' . format ( right . nlevels , right ) ) <EOL> if len ( left ) != len ( right ) : <EOL> raise_assert_detail ( obj , '<STR_LIT>' . format ( obj ) , <EOL> '<STR_LIT>' . format ( len ( left ) , left ) , <EOL> '<STR_LIT>' . format ( len ( right ) , right ) ) <EOL> if left . nlevels > <NUM_LIT:1> : <EOL> for level in range ( left . nlevels ) : <EOL> llevel = _get_ilevel_values ( left , level ) <EOL> rlevel = _get_ilevel_values ( right , level ) <EOL> lobj = '<STR_LIT>' . format ( level ) <EOL> assert_index_equal ( llevel , rlevel , <EOL> exact = exact , check_names = check_names , <EOL> check_less_precise = check_less_precise , <EOL> check_exact = check_exact , obj = lobj ) <EOL> _check_types ( left . levels [ level ] , right . levels [ level ] , obj = obj ) <EOL> if check_exact : <EOL> if not left . equals ( right ) : <EOL> diff = np . sum ( ( left . values != right . values ) <EOL> . astype ( int ) ) * <NUM_LIT> / len ( left ) <EOL> msg = '<STR_LIT>' . format ( obj , np . round ( diff , <NUM_LIT:5> ) ) <EOL> raise_assert_detail ( obj , msg , left , right ) <EOL> else : <EOL> _testing . assert_almost_equal ( left . values , right . values , <EOL> check_less_precise = check_less_precise , <EOL> obj = obj , lobj = left , robj = right ) <EOL> if check_names : <EOL> assert_attr_equal ( '<STR_LIT>' , left , right , obj = obj ) <EOL> def assert_attr_equal ( attr , left , right , obj = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> left_attr = getattr ( left , attr ) <EOL> right_attr = getattr ( right , attr ) <EOL> if left_attr is right_attr : <EOL> return True <EOL> elif ( is_number ( left_attr ) and np . isnan ( left_attr ) and <EOL> is_number ( right_attr ) and np . isnan ( right_attr ) ) : <EOL> return True <EOL> try : <EOL> result = left_attr == right_attr <EOL> except TypeError : <EOL> result = False <EOL> if not isinstance ( result , bool ) : <EOL> result = result . all ( ) <EOL> if result : <EOL> return True <EOL> else : <EOL> raise_assert_detail ( obj , '<STR_LIT>' . format ( attr ) , <EOL> left_attr , right_attr ) <EOL> def assert_is_valid_plot_return_object ( objs ) : <EOL> import matplotlib . pyplot as plt <EOL> if isinstance ( objs , np . ndarray ) : <EOL> for el in objs . flat : <EOL> assert isinstance ( el , plt . Axes ) , ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( el . __class__ . __name__ ) ) <EOL> else : <EOL> assert isinstance ( objs , ( plt . Artist , tuple , dict ) ) , ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( objs . __class__ . __name__ ) ) <EOL> def isiterable ( obj ) : <EOL> return hasattr ( obj , '<STR_LIT>' ) <EOL> def is_sorted ( seq ) : <EOL> if isinstance ( seq , ( Index , Series ) ) : <EOL> seq = seq . values <EOL> return assert_numpy_array_equal ( seq , np . sort ( np . array ( seq ) ) ) <EOL> def assertIs ( first , second , msg = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> a , b = first , second <EOL> assert a is b , "<STR_LIT>" % ( msg . format ( a , b ) , a , b ) <EOL> def assertIsNot ( first , second , msg = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> a , b = first , second <EOL> assert a is not b , "<STR_LIT>" % ( msg . format ( a , b ) , a , b ) <EOL> def assertIn ( first , second , msg = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> a , b = first , second <EOL> assert a in b , "<STR_LIT>" % ( msg . format ( a , b ) , a , b ) <EOL> def assertNotIn ( first , second , msg = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> a , b = first , second <EOL> assert a not in b , "<STR_LIT>" % ( msg . format ( a , b ) , a , b ) <EOL> def assertIsNone ( expr , msg = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> return assertIs ( expr , None , msg ) <EOL> def assertIsNotNone ( expr , msg = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> return assertIsNot ( expr , None , msg ) <EOL> def assertIsInstance ( obj , cls , msg = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> assert isinstance ( obj , cls ) , ( <EOL> "<STR_LIT>" % ( <EOL> msg , cls , type ( obj ) ) ) <EOL> def assert_isinstance ( obj , class_type_or_tuple , msg = '<STR_LIT>' ) : <EOL> return deprecate ( '<STR_LIT>' , assertIsInstance ) ( <EOL> obj , class_type_or_tuple , msg = msg ) <EOL> def assertNotIsInstance ( obj , cls , msg = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> assert not isinstance ( obj , cls ) , ( <EOL> "<STR_LIT>" % ( <EOL> msg , cls , type ( obj ) ) ) <EOL> def assert_categorical_equal ( res , exp ) : <EOL> assertIsInstance ( res , pd . Categorical , '<STR_LIT>' ) <EOL> assertIsInstance ( exp , pd . Categorical , '<STR_LIT>' ) <EOL> assert_index_equal ( res . categories , exp . categories ) <EOL> if not array_equivalent ( res . codes , exp . codes ) : <EOL> raise AssertionError ( <EOL> '<STR_LIT>' . format ( res . codes , exp . codes ) ) <EOL> if res . ordered != exp . ordered : <EOL> raise AssertionError ( "<STR_LIT>" ) <EOL> def raise_assert_detail ( obj , message , left , right ) : <EOL> if isinstance ( left , np . ndarray ) : <EOL> left = pprint_thing ( left ) <EOL> if isinstance ( right , np . ndarray ) : <EOL> right = pprint_thing ( right ) <EOL> msg = """<STR_LIT>""" . format ( obj , message , left , right ) <EOL> raise AssertionError ( msg ) <EOL> def assert_numpy_array_equal ( left , right , <EOL> strict_nan = False , err_msg = None , <EOL> obj = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if array_equivalent ( left , right , strict_nan = strict_nan ) : <EOL> return True <EOL> if err_msg is None : <EOL> if lib . isscalar ( left ) and lib . isscalar ( right ) : <EOL> assert_equal ( left , right ) <EOL> elif is_list_like ( left ) and is_list_like ( right ) : <EOL> left = np . asarray ( left ) <EOL> right = np . array ( right ) <EOL> if left . shape != right . shape : <EOL> raise_assert_detail ( obj , '<STR_LIT>' <EOL> . format ( obj ) , left . shape , right . shape ) <EOL> diff = <NUM_LIT:0> <EOL> for l , r in zip ( left , right ) : <EOL> if not array_equivalent ( l , r , strict_nan = strict_nan ) : <EOL> diff += <NUM_LIT:1> <EOL> diff = diff * <NUM_LIT> / left . size <EOL> msg = '<STR_LIT>' . format ( obj , np . round ( diff , <NUM_LIT:5> ) ) <EOL> raise_assert_detail ( obj , msg , left , right ) <EOL> elif is_list_like ( left ) : <EOL> msg = "<STR_LIT>" <EOL> raise_assert_detail ( obj , msg , left , right ) <EOL> else : <EOL> msg = "<STR_LIT>" <EOL> raise_assert_detail ( obj , msg , left , right ) <EOL> raise AssertionError ( err_msg ) <EOL> def assert_series_equal ( left , right , check_dtype = True , <EOL> check_index_type = '<STR_LIT>' , <EOL> check_series_type = True , <EOL> check_less_precise = False , <EOL> check_names = True , <EOL> check_exact = False , <EOL> check_datetimelike_compat = False , <EOL> obj = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> assertIsInstance ( left , Series , '<STR_LIT>' ) <EOL> assertIsInstance ( right , Series , '<STR_LIT>' ) <EOL> if check_series_type : <EOL> assertIsInstance ( left , type ( right ) ) <EOL> if len ( left ) != len ( right ) : <EOL> raise_assert_detail ( obj , '<STR_LIT>' , <EOL> '<STR_LIT>' . format ( len ( left ) , left . index ) , <EOL> '<STR_LIT>' . format ( len ( right ) , right . index ) ) <EOL> assert_index_equal ( left . index , right . index , exact = check_index_type , <EOL> check_names = check_names , <EOL> check_less_precise = check_less_precise , <EOL> check_exact = check_exact , <EOL> obj = '<STR_LIT>' . format ( obj ) ) <EOL> if check_dtype : <EOL> assert_attr_equal ( '<STR_LIT>' , left , right ) <EOL> if check_exact : <EOL> assert_numpy_array_equal ( left . get_values ( ) , right . get_values ( ) , <EOL> obj = '<STR_LIT>' . format ( obj ) ) <EOL> elif check_datetimelike_compat : <EOL> if ( is_datetimelike_v_numeric ( left , right ) or <EOL> is_datetimelike_v_object ( left , right ) or <EOL> needs_i8_conversion ( left ) or <EOL> needs_i8_conversion ( right ) ) : <EOL> if not Index ( left . values ) . equals ( Index ( right . values ) ) : <EOL> msg = '<STR_LIT>' <EOL> raise AssertionError ( msg . format ( left . values , right . values ) ) <EOL> else : <EOL> assert_numpy_array_equal ( left . values , right . values ) <EOL> else : <EOL> _testing . assert_almost_equal ( left . get_values ( ) , right . get_values ( ) , <EOL> check_less_precise , obj = '<STR_LIT>' . format ( obj ) ) <EOL> if check_names : <EOL> assert_attr_equal ( '<STR_LIT:name>' , left , right , obj = obj ) <EOL> def assert_frame_equal ( left , right , check_dtype = True , <EOL> check_index_type = '<STR_LIT>' , <EOL> check_column_type = '<STR_LIT>' , <EOL> check_frame_type = True , <EOL> check_less_precise = False , <EOL> check_names = True , <EOL> by_blocks = False , <EOL> check_exact = False , <EOL> check_datetimelike_compat = False , <EOL> check_like = False , <EOL> obj = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> assertIsInstance ( left , DataFrame , '<STR_LIT>' ) <EOL> assertIsInstance ( right , DataFrame , '<STR_LIT>' ) <EOL> if check_frame_type : <EOL> assertIsInstance ( left , type ( right ) ) <EOL> if check_like : <EOL> left , right = left . reindex_like ( right ) , right <EOL> if left . shape [ <NUM_LIT:0> ] != right . shape [ <NUM_LIT:0> ] : <EOL> raise_assert_detail ( obj , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' . format ( left . shape [ <NUM_LIT:0> ] , left . index ) , <EOL> '<STR_LIT>' . format ( right . shape [ <NUM_LIT:0> ] , right . index ) ) <EOL> if left . shape [ <NUM_LIT:1> ] != right . shape [ <NUM_LIT:1> ] : <EOL> raise_assert_detail ( obj , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' . format ( left . shape [ <NUM_LIT:1> ] , <EOL> left . columns ) , <EOL> '<STR_LIT>' . format ( right . shape [ <NUM_LIT:1> ] , <EOL> right . columns ) ) <EOL> assert_index_equal ( left . index , right . index , exact = check_index_type , <EOL> check_names = check_names , <EOL> check_less_precise = check_less_precise , <EOL> check_exact = check_exact , <EOL> obj = '<STR_LIT>' . format ( obj ) ) <EOL> assert_index_equal ( left . columns , right . columns , exact = check_column_type , <EOL> check_names = check_names , <EOL> check_less_precise = check_less_precise , <EOL> check_exact = check_exact , <EOL> obj = '<STR_LIT>' . format ( obj ) ) <EOL> if by_blocks : <EOL> rblocks = right . blocks <EOL> lblocks = left . blocks <EOL> for dtype in list ( set ( list ( lblocks . keys ( ) ) + list ( rblocks . keys ( ) ) ) ) : <EOL> assert dtype in lblocks <EOL> assert dtype in rblocks <EOL> assert_frame_equal ( lblocks [ dtype ] , rblocks [ dtype ] , <EOL> check_dtype = check_dtype , obj = '<STR_LIT>' ) <EOL> else : <EOL> for i , col in enumerate ( left . columns ) : <EOL> assert col in right <EOL> lcol = left . iloc [ : , i ] <EOL> rcol = right . iloc [ : , i ] <EOL> assert_series_equal ( <EOL> lcol , rcol , check_dtype = check_dtype , <EOL> check_index_type = check_index_type , <EOL> check_less_precise = check_less_precise , <EOL> check_exact = check_exact , check_names = check_names , <EOL> check_datetimelike_compat = check_datetimelike_compat , <EOL> obj = '<STR_LIT>' . format ( i ) ) <EOL> def assert_panelnd_equal ( left , right , <EOL> check_panel_type = False , <EOL> check_less_precise = False , <EOL> assert_func = assert_frame_equal , <EOL> check_names = False ) : <EOL> if check_panel_type : <EOL> assertIsInstance ( left , type ( right ) ) <EOL> for axis in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> left_ind = getattr ( left , axis ) <EOL> right_ind = getattr ( right , axis ) <EOL> assert_index_equal ( left_ind , right_ind , check_names = check_names ) <EOL> for i , item in enumerate ( left . _get_axis ( <NUM_LIT:0> ) ) : <EOL> assert item in right , "<STR_LIT>" % item <EOL> litem = left . iloc [ i ] <EOL> ritem = right . iloc [ i ] <EOL> assert_func ( litem , ritem , check_less_precise = check_less_precise ) <EOL> for i , item in enumerate ( right . _get_axis ( <NUM_LIT:0> ) ) : <EOL> assert item in left , "<STR_LIT>" % item <EOL> _panel_frame_equal = partial ( assert_frame_equal , check_names = False ) <EOL> assert_panel_equal = partial ( assert_panelnd_equal , <EOL> assert_func = _panel_frame_equal ) <EOL> assert_panel4d_equal = partial ( assert_panelnd_equal , <EOL> assert_func = assert_panel_equal ) <EOL> def assert_sp_array_equal ( left , right ) : <EOL> assertIsInstance ( left , pd . SparseArray , '<STR_LIT>' ) <EOL> assertIsInstance ( right , pd . SparseArray , '<STR_LIT>' ) <EOL> assert_almost_equal ( left . sp_values , right . sp_values ) <EOL> assertIsInstance ( left . sp_index , pd . _sparse . SparseIndex , '<STR_LIT>' ) <EOL> assertIsInstance ( right . sp_index , pd . _sparse . SparseIndex , '<STR_LIT>' ) <EOL> if not left . sp_index . equals ( right . sp_index ) : <EOL> raise_assert_detail ( '<STR_LIT>' , '<STR_LIT>' , <EOL> left . sp_index , right . sp_index ) <EOL> if np . isnan ( left . fill_value ) : <EOL> assert ( np . isnan ( right . fill_value ) ) <EOL> else : <EOL> assert ( left . fill_value == right . fill_value ) <EOL> assert_attr_equal ( '<STR_LIT>' , left , right ) <EOL> assert_numpy_array_equal ( left . values , right . values ) <EOL> def assert_sp_series_equal ( left , right , exact_indices = True , <EOL> check_names = True , obj = '<STR_LIT>' ) : <EOL> assertIsInstance ( left , pd . SparseSeries , '<STR_LIT>' ) <EOL> assertIsInstance ( right , pd . SparseSeries , '<STR_LIT>' ) <EOL> assert_index_equal ( left . index , right . index , <EOL> obj = '<STR_LIT>' . format ( obj ) ) <EOL> assert_sp_array_equal ( left . block . values , right . block . values ) <EOL> if check_names : <EOL> assert_attr_equal ( '<STR_LIT:name>' , left , right ) <EOL> assert_attr_equal ( '<STR_LIT>' , left , right ) <EOL> assert_numpy_array_equal ( left . values , right . values ) <EOL> def assert_sp_frame_equal ( left , right , exact_indices = True , <EOL> obj = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> assertIsInstance ( left , pd . SparseDataFrame , '<STR_LIT>' ) <EOL> assertIsInstance ( right , pd . SparseDataFrame , '<STR_LIT>' ) <EOL> assert_index_equal ( left . index , right . index , <EOL> obj = '<STR_LIT>' . format ( obj ) ) <EOL> assert_index_equal ( left . columns , right . columns , <EOL> obj = '<STR_LIT>' . format ( obj ) ) <EOL> for col , series in compat . iteritems ( left ) : <EOL> assert ( col in right ) <EOL> if exact_indices : <EOL> assert_sp_series_equal ( series , right [ col ] ) <EOL> else : <EOL> assert_series_equal ( series . to_dense ( ) , right [ col ] . to_dense ( ) ) <EOL> assert_attr_equal ( '<STR_LIT>' , left , right , obj = obj ) <EOL> for col in right : <EOL> assert ( col in left ) <EOL> def assert_sp_panel_equal ( left , right , exact_indices = True ) : <EOL> assertIsInstance ( left , pd . SparsePanel , '<STR_LIT>' ) <EOL> assertIsInstance ( right , pd . SparsePanel , '<STR_LIT>' ) <EOL> for item , frame in left . iteritems ( ) : <EOL> assert ( item in right ) <EOL> assert_sp_frame_equal ( frame , right [ item ] , exact_indices = exact_indices ) <EOL> assert_almost_equal ( left . default_fill_value , right . default_fill_value ) <EOL> assert ( left . default_kind == right . default_kind ) <EOL> for item in right : <EOL> assert ( item in left ) <EOL> def assert_sp_list_equal ( left , right ) : <EOL> assertIsInstance ( left , pd . SparseList , '<STR_LIT>' ) <EOL> assertIsInstance ( right , pd . SparseList , '<STR_LIT>' ) <EOL> assert_sp_array_equal ( left . to_array ( ) , right . to_array ( ) ) <EOL> def assert_contains_all ( iterable , dic ) : <EOL> for k in iterable : <EOL> assert k in dic , "<STR_LIT>" % k <EOL> def assert_copy ( iter1 , iter2 , ** eql_kwargs ) : <EOL> """<STR_LIT>""" <EOL> for elem1 , elem2 in zip ( iter1 , iter2 ) : <EOL> assert_almost_equal ( elem1 , elem2 , ** eql_kwargs ) <EOL> assert elem1 is not elem2 , ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( type ( elem1 ) , type ( elem2 ) ) ) <EOL> def getCols ( k ) : <EOL> return string . ascii_uppercase [ : k ] <EOL> def getArangeMat ( ) : <EOL> return np . arange ( N * K ) . reshape ( ( N , K ) ) <EOL> def makeStringIndex ( k = <NUM_LIT:10> , name = None ) : <EOL> return Index ( rands_array ( nchars = <NUM_LIT:10> , size = k ) , name = name ) <EOL> def makeUnicodeIndex ( k = <NUM_LIT:10> , name = None ) : <EOL> return Index ( randu_array ( nchars = <NUM_LIT:10> , size = k ) ) <EOL> def makeCategoricalIndex ( k = <NUM_LIT:10> , n = <NUM_LIT:3> , name = None ) : <EOL> """<STR_LIT>""" <EOL> x = rands_array ( nchars = <NUM_LIT:4> , size = n ) <EOL> return CategoricalIndex ( np . random . choice ( x , k ) , name = name ) <EOL> def makeBoolIndex ( k = <NUM_LIT:10> , name = None ) : <EOL> if k == <NUM_LIT:1> : <EOL> return Index ( [ True ] , name = name ) <EOL> elif k == <NUM_LIT:2> : <EOL> return Index ( [ False , True ] , name = name ) <EOL> return Index ( [ False , True ] + [ False ] * ( k - <NUM_LIT:2> ) , name = name ) <EOL> def makeIntIndex ( k = <NUM_LIT:10> , name = None ) : <EOL> return Index ( lrange ( k ) , name = name ) <EOL> def makeRangeIndex ( k = <NUM_LIT:10> , name = None ) : <EOL> return RangeIndex ( <NUM_LIT:0> , k , <NUM_LIT:1> , name = name ) <EOL> def makeFloatIndex ( k = <NUM_LIT:10> , name = None ) : <EOL> values = sorted ( np . random . random_sample ( k ) ) - np . random . random_sample ( <NUM_LIT:1> ) <EOL> return Index ( values * ( <NUM_LIT:10> ** np . random . randint ( <NUM_LIT:0> , <NUM_LIT:9> ) ) , name = name ) <EOL> def makeDateIndex ( k = <NUM_LIT:10> , freq = '<STR_LIT:B>' , name = None ) : <EOL> dt = datetime ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> dr = bdate_range ( dt , periods = k , freq = freq , name = name ) <EOL> return DatetimeIndex ( dr , name = name ) <EOL> def makeTimedeltaIndex ( k = <NUM_LIT:10> , freq = '<STR_LIT:D>' , name = None ) : <EOL> return TimedeltaIndex ( start = '<STR_LIT>' , periods = k , freq = freq , name = name ) <EOL> def makePeriodIndex ( k = <NUM_LIT:10> , name = None ) : <EOL> dt = datetime ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> dr = PeriodIndex ( start = dt , periods = k , freq = '<STR_LIT:B>' , name = name ) <EOL> return dr <EOL> def all_index_generator ( k = <NUM_LIT:10> ) : <EOL> """<STR_LIT>""" <EOL> all_make_index_funcs = [ makeIntIndex , makeFloatIndex , makeStringIndex , <EOL> makeUnicodeIndex , makeDateIndex , makePeriodIndex , <EOL> makeTimedeltaIndex , makeBoolIndex , <EOL> makeCategoricalIndex ] <EOL> for make_index_func in all_make_index_funcs : <EOL> yield make_index_func ( k = k ) <EOL> def all_timeseries_index_generator ( k = <NUM_LIT:10> ) : <EOL> """<STR_LIT>""" <EOL> make_index_funcs = [ makeDateIndex , makePeriodIndex , makeTimedeltaIndex ] <EOL> for make_index_func in make_index_funcs : <EOL> yield make_index_func ( k = k ) <EOL> def makeFloatSeries ( name = None ) : <EOL> index = makeStringIndex ( N ) <EOL> return Series ( randn ( N ) , index = index , name = name ) <EOL> def makeStringSeries ( name = None ) : <EOL> index = makeStringIndex ( N ) <EOL> return Series ( randn ( N ) , index = index , name = name ) <EOL> def makeObjectSeries ( name = None ) : <EOL> dateIndex = makeDateIndex ( N ) <EOL> dateIndex = Index ( dateIndex , dtype = object ) <EOL> index = makeStringIndex ( N ) <EOL> return Series ( dateIndex , index = index , name = name ) <EOL> def getSeriesData ( ) : <EOL> index = makeStringIndex ( N ) <EOL> return dict ( ( c , Series ( randn ( N ) , index = index ) ) for c in getCols ( K ) ) <EOL> def makeTimeSeries ( nper = None , freq = '<STR_LIT:B>' , name = None ) : <EOL> if nper is None : <EOL> nper = N <EOL> return Series ( randn ( nper ) , index = makeDateIndex ( nper , freq = freq ) , name = name ) <EOL> def makePeriodSeries ( nper = None , name = None ) : <EOL> if nper is None : <EOL> nper = N <EOL> return Series ( randn ( nper ) , index = makePeriodIndex ( nper ) , name = name ) <EOL> def getTimeSeriesData ( nper = None , freq = '<STR_LIT:B>' ) : <EOL> return dict ( ( c , makeTimeSeries ( nper , freq ) ) for c in getCols ( K ) ) <EOL> def getPeriodData ( nper = None ) : <EOL> return dict ( ( c , makePeriodSeries ( nper ) ) for c in getCols ( K ) ) <EOL> def makeTimeDataFrame ( nper = None , freq = '<STR_LIT:B>' ) : <EOL> data = getTimeSeriesData ( nper , freq ) <EOL> return DataFrame ( data ) <EOL> def makeDataFrame ( ) : <EOL> data = getSeriesData ( ) <EOL> return DataFrame ( data ) <EOL> def getMixedTypeDict ( ) : <EOL> index = Index ( [ '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:c>' , '<STR_LIT:d>' , '<STR_LIT:e>' ] ) <EOL> data = { <EOL> '<STR_LIT:A>' : [ <NUM_LIT:0.> , <NUM_LIT:1.> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> '<STR_LIT:B>' : [ <NUM_LIT:0.> , <NUM_LIT:1.> , <NUM_LIT:0.> , <NUM_LIT:1.> , <NUM_LIT:0.> ] , <EOL> '<STR_LIT:C>' : [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT:D>' : bdate_range ( '<STR_LIT>' , periods = <NUM_LIT:5> ) <EOL> } <EOL> return index , data <EOL> def makeMixedDataFrame ( ) : <EOL> return DataFrame ( getMixedTypeDict ( ) [ <NUM_LIT:1> ] ) <EOL> def makePeriodFrame ( nper = None ) : <EOL> data = getPeriodData ( nper ) <EOL> return DataFrame ( data ) <EOL> def makePanel ( nper = None ) : <EOL> cols = [ '<STR_LIT>' + c for c in string . ascii_uppercase [ : K - <NUM_LIT:1> ] ] <EOL> data = dict ( ( c , makeTimeDataFrame ( nper ) ) for c in cols ) <EOL> return Panel . fromDict ( data ) <EOL> def makePeriodPanel ( nper = None ) : <EOL> cols = [ '<STR_LIT>' + c for c in string . ascii_uppercase [ : K - <NUM_LIT:1> ] ] <EOL> data = dict ( ( c , makePeriodFrame ( nper ) ) for c in cols ) <EOL> return Panel . fromDict ( data ) <EOL> def makePanel4D ( nper = None ) : <EOL> return Panel4D ( dict ( l1 = makePanel ( nper ) , l2 = makePanel ( nper ) , <EOL> l3 = makePanel ( nper ) ) ) <EOL> def makeCustomIndex ( nentries , nlevels , prefix = '<STR_LIT:#>' , names = False , ndupe_l = None , <EOL> idx_type = None ) : <EOL> """<STR_LIT>""" <EOL> if ndupe_l is None : <EOL> ndupe_l = [ <NUM_LIT:1> ] * nlevels <EOL> assert ( is_sequence ( ndupe_l ) and len ( ndupe_l ) <= nlevels ) <EOL> assert ( names is None or names is False or <EOL> names is True or len ( names ) is nlevels ) <EOL> assert idx_type is None or ( idx_type in ( '<STR_LIT:i>' , '<STR_LIT:f>' , '<STR_LIT:s>' , '<STR_LIT:u>' , '<STR_LIT>' , '<STR_LIT:p>' , '<STR_LIT>' ) and nlevels == <NUM_LIT:1> ) <EOL> if names is True : <EOL> names = [ prefix + str ( i ) for i in range ( nlevels ) ] <EOL> if names is False : <EOL> names = None <EOL> if isinstance ( names , compat . string_types ) and nlevels == <NUM_LIT:1> : <EOL> names = [ names ] <EOL> idx_func = dict ( i = makeIntIndex , f = makeFloatIndex , <EOL> s = makeStringIndex , u = makeUnicodeIndex , <EOL> dt = makeDateIndex , td = makeTimedeltaIndex , <EOL> p = makePeriodIndex ) . get ( idx_type ) <EOL> if idx_func : <EOL> idx = idx_func ( nentries ) <EOL> if names : <EOL> idx . name = names [ <NUM_LIT:0> ] <EOL> return idx <EOL> elif idx_type is not None : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' % idx_type ) <EOL> if len ( ndupe_l ) < nlevels : <EOL> ndupe_l . extend ( [ <NUM_LIT:1> ] * ( nlevels - len ( ndupe_l ) ) ) <EOL> assert len ( ndupe_l ) == nlevels <EOL> assert all ( [ x > <NUM_LIT:0> for x in ndupe_l ] ) <EOL> tuples = [ ] <EOL> for i in range ( nlevels ) : <EOL> def keyfunc ( x ) : <EOL> import re <EOL> numeric_tuple = re . sub ( "<STR_LIT>" , "<STR_LIT>" , x ) . split ( "<STR_LIT:_>" ) <EOL> return lmap ( int , numeric_tuple ) <EOL> div_factor = nentries // ndupe_l [ i ] + <NUM_LIT:1> <EOL> cnt = Counter ( ) <EOL> for j in range ( div_factor ) : <EOL> label = prefix + '<STR_LIT>' % i + str ( j ) <EOL> cnt [ label ] = ndupe_l [ i ] <EOL> result = list ( sorted ( cnt . elements ( ) , key = keyfunc ) ) [ : nentries ] <EOL> tuples . append ( result ) <EOL> tuples = lzip ( * tuples ) <EOL> if nentries == <NUM_LIT:1> : <EOL> index = Index ( tuples [ <NUM_LIT:0> ] , name = names [ <NUM_LIT:0> ] ) <EOL> else : <EOL> index = MultiIndex . from_tuples ( tuples , names = names ) <EOL> return index <EOL> def makeCustomDataframe ( nrows , ncols , c_idx_names = True , r_idx_names = True , <EOL> c_idx_nlevels = <NUM_LIT:1> , r_idx_nlevels = <NUM_LIT:1> , data_gen_f = None , <EOL> c_ndupe_l = None , r_ndupe_l = None , dtype = None , <EOL> c_idx_type = None , r_idx_type = None ) : <EOL> """<STR_LIT>""" <EOL> assert c_idx_nlevels > <NUM_LIT:0> <EOL> assert r_idx_nlevels > <NUM_LIT:0> <EOL> assert r_idx_type is None or ( r_idx_type in ( '<STR_LIT:i>' , '<STR_LIT:f>' , '<STR_LIT:s>' , <EOL> '<STR_LIT:u>' , '<STR_LIT>' , '<STR_LIT:p>' , '<STR_LIT>' ) and r_idx_nlevels == <NUM_LIT:1> ) <EOL> assert c_idx_type is None or ( c_idx_type in ( '<STR_LIT:i>' , '<STR_LIT:f>' , '<STR_LIT:s>' , <EOL> '<STR_LIT:u>' , '<STR_LIT>' , '<STR_LIT:p>' , '<STR_LIT>' ) and c_idx_nlevels == <NUM_LIT:1> ) <EOL> columns = makeCustomIndex ( ncols , nlevels = c_idx_nlevels , prefix = '<STR_LIT:C>' , <EOL> names = c_idx_names , ndupe_l = c_ndupe_l , <EOL> idx_type = c_idx_type ) <EOL> index = makeCustomIndex ( nrows , nlevels = r_idx_nlevels , prefix = '<STR_LIT:R>' , <EOL> names = r_idx_names , ndupe_l = r_ndupe_l , <EOL> idx_type = r_idx_type ) <EOL> if data_gen_f is None : <EOL> data_gen_f = lambda r , c : "<STR_LIT>" % ( r , c ) <EOL> data = [ [ data_gen_f ( r , c ) for c in range ( ncols ) ] for r in range ( nrows ) ] <EOL> return DataFrame ( data , index , columns , dtype = dtype ) <EOL> def _create_missing_idx ( nrows , ncols , density , random_state = None ) : <EOL> if random_state is None : <EOL> random_state = np . random <EOL> else : <EOL> random_state = np . random . RandomState ( random_state ) <EOL> size = int ( np . round ( ( <NUM_LIT:1> - density ) * nrows * ncols ) ) <EOL> min_rows = <NUM_LIT:5> <EOL> fac = <NUM_LIT> <EOL> extra_size = min ( size + min_rows , fac * size ) <EOL> def _gen_unique_rand ( rng , _extra_size ) : <EOL> ind = rng . rand ( int ( _extra_size ) ) <EOL> return np . unique ( np . floor ( ind * nrows * ncols ) ) [ : size ] <EOL> ind = _gen_unique_rand ( random_state , extra_size ) <EOL> while ind . size < size : <EOL> extra_size *= <NUM_LIT> <EOL> ind = _gen_unique_rand ( random_state , extra_size ) <EOL> j = np . floor ( ind * <NUM_LIT:1.> / nrows ) . astype ( int ) <EOL> i = ( ind - j * nrows ) . astype ( int ) <EOL> return i . tolist ( ) , j . tolist ( ) <EOL> def makeMissingCustomDataframe ( nrows , ncols , density = <NUM_LIT> , random_state = None , <EOL> c_idx_names = True , r_idx_names = True , <EOL> c_idx_nlevels = <NUM_LIT:1> , r_idx_nlevels = <NUM_LIT:1> , <EOL> data_gen_f = None , <EOL> c_ndupe_l = None , r_ndupe_l = None , dtype = None , <EOL> c_idx_type = None , r_idx_type = None ) : <EOL> """<STR_LIT>""" <EOL> df = makeCustomDataframe ( nrows , ncols , c_idx_names = c_idx_names , <EOL> r_idx_names = r_idx_names , <EOL> c_idx_nlevels = c_idx_nlevels , <EOL> r_idx_nlevels = r_idx_nlevels , <EOL> data_gen_f = data_gen_f , <EOL> c_ndupe_l = c_ndupe_l , r_ndupe_l = r_ndupe_l , <EOL> dtype = dtype , c_idx_type = c_idx_type , <EOL> r_idx_type = r_idx_type ) <EOL> i , j = _create_missing_idx ( nrows , ncols , density , random_state ) <EOL> df . values [ i , j ] = np . nan <EOL> return df <EOL> def makeMissingDataframe ( density = <NUM_LIT> , random_state = None ) : <EOL> df = makeDataFrame ( ) <EOL> i , j = _create_missing_idx ( * df . shape , density = density , <EOL> random_state = random_state ) <EOL> df . values [ i , j ] = np . nan <EOL> return df <EOL> def add_nans ( panel ) : <EOL> I , J , N = panel . shape <EOL> for i , item in enumerate ( panel . items ) : <EOL> dm = panel [ item ] <EOL> for j , col in enumerate ( dm . columns ) : <EOL> dm [ col ] [ : i + j ] = np . NaN <EOL> return panel <EOL> def add_nans_panel4d ( panel4d ) : <EOL> for l , label in enumerate ( panel4d . labels ) : <EOL> panel = panel4d [ label ] <EOL> add_nans ( panel ) <EOL> return panel4d <EOL> class TestSubDict ( dict ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> dict . __init__ ( self , * args , ** kwargs ) <EOL> def package_check ( pkg_name , version = None , app = '<STR_LIT>' , checker = LooseVersion , <EOL> exc_failed_import = ImportError , <EOL> exc_failed_check = RuntimeError ) : <EOL> """<STR_LIT>""" <EOL> if app : <EOL> msg = '<STR_LIT>' % ( app , pkg_name ) <EOL> else : <EOL> msg = '<STR_LIT>' % pkg_name <EOL> if version : <EOL> msg += '<STR_LIT>' % ( version , ) <EOL> try : <EOL> mod = __import__ ( pkg_name ) <EOL> except ImportError : <EOL> raise exc_failed_import ( msg ) <EOL> if not version : <EOL> return <EOL> try : <EOL> have_version = mod . __version__ <EOL> except AttributeError : <EOL> raise exc_failed_check ( '<STR_LIT>' % pkg_name ) <EOL> if checker ( have_version ) < checker ( version ) : <EOL> raise exc_failed_check ( msg ) <EOL> def skip_if_no_package ( * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> from nose import SkipTest <EOL> package_check ( exc_failed_import = SkipTest , <EOL> exc_failed_check = SkipTest , <EOL> * args , ** kwargs ) <EOL> def skip_if_no_package_deco ( pkg_name , version = None , app = '<STR_LIT>' ) : <EOL> from nose import SkipTest <EOL> def deco ( func ) : <EOL> @ wraps ( func ) <EOL> def wrapper ( * args , ** kwargs ) : <EOL> package_check ( pkg_name , version = version , app = app , <EOL> exc_failed_import = SkipTest , <EOL> exc_failed_check = SkipTest ) <EOL> return func ( * args , ** kwargs ) <EOL> return wrapper <EOL> return deco <EOL> def optional_args ( decorator ) : <EOL> """<STR_LIT>""" <EOL> @ wraps ( decorator ) <EOL> def wrapper ( * args , ** kwargs ) : <EOL> def dec ( f ) : <EOL> return decorator ( f , * args , ** kwargs ) <EOL> is_decorating = not kwargs and len ( args ) == <NUM_LIT:1> and callable ( args [ <NUM_LIT:0> ] ) <EOL> if is_decorating : <EOL> f = args [ <NUM_LIT:0> ] <EOL> args = [ ] <EOL> return dec ( f ) <EOL> else : <EOL> return dec <EOL> return wrapper <EOL> _network_error_messages = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> _network_errno_vals = ( <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> ) <EOL> _network_error_classes = ( IOError , httplib . HTTPException ) <EOL> if sys . version_info >= ( <NUM_LIT:3> , <NUM_LIT:3> ) : <EOL> _network_error_classes += ( TimeoutError , ) <EOL> def can_connect ( url , error_classes = _network_error_classes ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> with urlopen ( url ) : <EOL> pass <EOL> except error_classes : <EOL> return False <EOL> else : <EOL> return True <EOL> @ optional_args <EOL> def network ( t , url = "<STR_LIT>" , <EOL> raise_on_error = _RAISE_NETWORK_ERROR_DEFAULT , <EOL> check_before_test = False , <EOL> error_classes = _network_error_classes , <EOL> skip_errnos = _network_errno_vals , <EOL> _skip_on_messages = _network_error_messages , <EOL> ) : <EOL> """<STR_LIT>""" <EOL> from nose import SkipTest <EOL> t . network = True <EOL> @ wraps ( t ) <EOL> def wrapper ( * args , ** kwargs ) : <EOL> if check_before_test and not raise_on_error : <EOL> if not can_connect ( url , error_classes ) : <EOL> raise SkipTest <EOL> try : <EOL> return t ( * args , ** kwargs ) <EOL> except Exception as e : <EOL> errno = getattr ( e , '<STR_LIT>' , None ) <EOL> if not errno and hasattr ( errno , "<STR_LIT>" ) : <EOL> errno = getattr ( e . reason , '<STR_LIT>' , None ) <EOL> if errno in skip_errnos : <EOL> raise SkipTest ( "<STR_LIT>" <EOL> "<STR_LIT>" % e ) <EOL> try : <EOL> e_str = traceback . format_exc ( e ) <EOL> except : <EOL> e_str = str ( e ) <EOL> if any ( [ m . lower ( ) in e_str . lower ( ) for m in _skip_on_messages ] ) : <EOL> raise SkipTest ( "<STR_LIT>" <EOL> "<STR_LIT>" % e ) <EOL> if not isinstance ( e , error_classes ) : <EOL> raise <EOL> if raise_on_error or can_connect ( url , error_classes ) : <EOL> raise <EOL> else : <EOL> raise SkipTest ( "<STR_LIT>" <EOL> "<STR_LIT>" % e ) <EOL> return wrapper <EOL> with_connectivity_check = network <EOL> class SimpleMock ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , obj , * args , ** kwds ) : <EOL> assert ( len ( args ) % <NUM_LIT:2> == <NUM_LIT:0> ) <EOL> attrs = kwds . get ( "<STR_LIT>" , { } ) <EOL> for k , v in zip ( args [ : : <NUM_LIT:2> ] , args [ <NUM_LIT:1> : : <NUM_LIT:2> ] ) : <EOL> attrs [ k ] = v <EOL> self . attrs = attrs <EOL> self . obj = obj <EOL> def __getattribute__ ( self , name ) : <EOL> attrs = object . __getattribute__ ( self , "<STR_LIT>" ) <EOL> obj = object . __getattribute__ ( self , "<STR_LIT>" ) <EOL> return attrs . get ( name , type ( obj ) . __getattribute__ ( obj , name ) ) <EOL> @ contextmanager <EOL> def stdin_encoding ( encoding = None ) : <EOL> """<STR_LIT>""" <EOL> import sys <EOL> _stdin = sys . stdin <EOL> sys . stdin = SimpleMock ( sys . stdin , "<STR_LIT>" , encoding ) <EOL> yield <EOL> sys . stdin = _stdin <EOL> def assertRaises ( _exception , _callable = None , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> manager = _AssertRaisesContextmanager ( exception = _exception ) <EOL> if _callable is not None : <EOL> with manager : <EOL> _callable ( * args , ** kwargs ) <EOL> else : <EOL> return manager <EOL> def assertRaisesRegexp ( _exception , _regexp , _callable = None , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> manager = _AssertRaisesContextmanager ( exception = _exception , regexp = _regexp ) <EOL> if _callable is not None : <EOL> with manager : <EOL> _callable ( * args , ** kwargs ) <EOL> else : <EOL> return manager <EOL> class _AssertRaisesContextmanager ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , exception , regexp = None , * args , ** kwargs ) : <EOL> self . exception = exception <EOL> if regexp is not None and not hasattr ( regexp , "<STR_LIT>" ) : <EOL> regexp = re . compile ( regexp , re . DOTALL ) <EOL> self . regexp = regexp <EOL> def __enter__ ( self ) : <EOL> return self <EOL> def __exit__ ( self , exc_type , exc_value , traceback ) : <EOL> expected = self . exception <EOL> if not exc_type : <EOL> name = getattr ( expected , "<STR_LIT>" , str ( expected ) ) <EOL> raise AssertionError ( "<STR_LIT>" . format ( name ) ) <EOL> if issubclass ( exc_type , expected ) : <EOL> return self . handle_success ( exc_type , exc_value , traceback ) <EOL> return self . handle_failure ( exc_type , exc_value , traceback ) <EOL> def handle_failure ( * args , ** kwargs ) : <EOL> return False <EOL> def handle_success ( self , exc_type , exc_value , traceback ) : <EOL> if self . regexp is not None : <EOL> val = str ( exc_value ) <EOL> if not self . regexp . search ( val ) : <EOL> e = AssertionError ( '<STR_LIT>' % <EOL> ( self . regexp . pattern , str ( val ) ) ) <EOL> raise_with_traceback ( e , traceback ) <EOL> return True <EOL> @ contextmanager <EOL> def assert_produces_warning ( expected_warning = Warning , filter_level = "<STR_LIT>" , <EOL> clear = None , check_stacklevel = True ) : <EOL> """<STR_LIT>""" <EOL> with warnings . catch_warnings ( record = True ) as w : <EOL> if clear is not None : <EOL> if not is_list_like ( clear ) : <EOL> clear = [ clear ] <EOL> for m in clear : <EOL> try : <EOL> m . __warningregistry__ . clear ( ) <EOL> except : <EOL> pass <EOL> saw_warning = False <EOL> warnings . simplefilter ( filter_level ) <EOL> yield w <EOL> extra_warnings = [ ] <EOL> for actual_warning in w : <EOL> if ( expected_warning and issubclass ( actual_warning . category , <EOL> expected_warning ) ) : <EOL> saw_warning = True <EOL> if check_stacklevel and issubclass ( actual_warning . category , <EOL> ( FutureWarning , <EOL> DeprecationWarning ) ) : <EOL> from inspect import getframeinfo , stack <EOL> caller = getframeinfo ( stack ( ) [ <NUM_LIT:2> ] [ <NUM_LIT:0> ] ) <EOL> msg = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" . format ( <EOL> actual_warning . filename , caller . filename , <EOL> actual_warning . message ) ) <EOL> assert actual_warning . filename == caller . filename , msg <EOL> else : <EOL> extra_warnings . append ( actual_warning . category . __name__ ) <EOL> if expected_warning : <EOL> assert saw_warning , ( "<STR_LIT>" <EOL> % expected_warning . __name__ ) <EOL> assert not extra_warnings , ( "<STR_LIT>" <EOL> % extra_warnings ) <EOL> def disabled ( t ) : <EOL> t . disabled = True <EOL> return t <EOL> class RNGContext ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , seed ) : <EOL> self . seed = seed <EOL> def __enter__ ( self ) : <EOL> self . start_state = np . random . get_state ( ) <EOL> np . random . seed ( self . seed ) <EOL> def __exit__ ( self , exc_type , exc_value , traceback ) : <EOL> np . random . set_state ( self . start_state ) <EOL> @ contextmanager <EOL> def use_numexpr ( use , min_elements = expr . _MIN_ELEMENTS ) : <EOL> olduse = expr . _USE_NUMEXPR <EOL> oldmin = expr . _MIN_ELEMENTS <EOL> expr . set_use_numexpr ( use ) <EOL> expr . _MIN_ELEMENTS = min_elements <EOL> yield <EOL> expr . _MIN_ELEMENTS = oldmin <EOL> expr . set_use_numexpr ( olduse ) <EOL> for name , obj in inspect . getmembers ( sys . modules [ __name__ ] ) : <EOL> if inspect . isfunction ( obj ) and name . startswith ( '<STR_LIT>' ) : <EOL> setattr ( TestCase , name , staticmethod ( obj ) ) <EOL> def test_parallel ( num_threads = <NUM_LIT:2> , kwargs_list = None ) : <EOL> """<STR_LIT>""" <EOL> assert num_threads > <NUM_LIT:0> <EOL> has_kwargs_list = kwargs_list is not None <EOL> if has_kwargs_list : <EOL> assert len ( kwargs_list ) == num_threads <EOL> import threading <EOL> def wrapper ( func ) : <EOL> @ wraps ( func ) <EOL> def inner ( * args , ** kwargs ) : <EOL> if has_kwargs_list : <EOL> update_kwargs = lambda i : dict ( kwargs , ** kwargs_list [ i ] ) <EOL> else : <EOL> update_kwargs = lambda i : kwargs <EOL> threads = [ ] <EOL> for i in range ( num_threads ) : <EOL> updated_kwargs = update_kwargs ( i ) <EOL> thread = threading . Thread ( target = func , args = args , <EOL> kwargs = updated_kwargs ) <EOL> threads . append ( thread ) <EOL> for thread in threads : <EOL> thread . start ( ) <EOL> for thread in threads : <EOL> thread . join ( ) <EOL> return inner <EOL> return wrapper <EOL> class SubclassedSeries ( Series ) : <EOL> _metadata = [ '<STR_LIT>' ] <EOL> @ property <EOL> def _constructor ( self ) : <EOL> return SubclassedSeries <EOL> @ property <EOL> def _constructor_expanddim ( self ) : <EOL> return SubclassedDataFrame <EOL> class SubclassedDataFrame ( DataFrame ) : <EOL> _metadata = [ '<STR_LIT>' ] <EOL> @ property <EOL> def _constructor ( self ) : <EOL> return SubclassedDataFrame <EOL> @ property <EOL> def _constructor_sliced ( self ) : <EOL> return SubclassedSeries <EOL> @ contextmanager <EOL> def patch ( ob , attr , value ) : <EOL> """<STR_LIT>""" <EOL> noattr = object ( ) <EOL> old = getattr ( ob , attr , noattr ) <EOL> setattr ( ob , attr , value ) <EOL> try : <EOL> yield <EOL> finally : <EOL> if old is noattr : <EOL> delattr ( ob , attr ) <EOL> else : <EOL> setattr ( ob , attr , old ) </s>
