<s> """<STR_LIT>""" <EOL> import contextlib <EOL> import datetime <EOL> import functools <EOL> import itertools <EOL> import re <EOL> import warnings <EOL> from collections import Mapping , MutableMapping <EOL> import numpy as np <EOL> import pandas as pd <EOL> from . import ops <EOL> from . pycompat import iteritems , OrderedDict <EOL> def alias_warning ( old_name , new_name , stacklevel = <NUM_LIT:3> ) : <EOL> warnings . warn ( '<STR_LIT>' <EOL> % ( old_name , new_name ) , <EOL> FutureWarning , stacklevel = stacklevel ) <EOL> def function_alias ( obj , old_name ) : <EOL> @ functools . wraps ( obj ) <EOL> def wrapper ( * args , ** kwargs ) : <EOL> alias_warning ( old_name , obj . __name__ ) <EOL> return obj ( * args , ** kwargs ) <EOL> return wrapper <EOL> def class_alias ( obj , old_name ) : <EOL> class Wrapper ( obj ) : <EOL> def __new__ ( cls , * args , ** kwargs ) : <EOL> alias_warning ( old_name , obj . __name__ ) <EOL> return super ( Wrapper , cls ) . __new__ ( cls , * args , ** kwargs ) <EOL> Wrapper . __name__ = obj . __name__ <EOL> return Wrapper <EOL> def safe_cast_to_index ( array ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( array , pd . Index ) : <EOL> index = array <EOL> elif hasattr ( array , '<STR_LIT>' ) : <EOL> index = array . to_index ( ) <EOL> else : <EOL> kwargs = { } <EOL> if hasattr ( array , '<STR_LIT>' ) and array . dtype . kind == '<STR_LIT:O>' : <EOL> kwargs [ '<STR_LIT>' ] = object <EOL> index = pd . Index ( np . asarray ( array ) , ** kwargs ) <EOL> return index <EOL> def maybe_wrap_array ( original , new_array ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( new_array , np . ndarray ) and new_array . shape == original . shape : <EOL> return original . __array_wrap__ ( new_array ) <EOL> else : <EOL> return new_array <EOL> def equivalent ( first , second ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( first , np . ndarray ) or isinstance ( second , np . ndarray ) : <EOL> return ops . array_equiv ( first , second ) <EOL> else : <EOL> return first is second or first == second <EOL> def peek_at ( iterable ) : <EOL> """<STR_LIT>""" <EOL> gen = iter ( iterable ) <EOL> peek = next ( gen ) <EOL> return peek , itertools . chain ( [ peek ] , gen ) <EOL> def update_safety_check ( first_dict , second_dict , compat = equivalent ) : <EOL> """<STR_LIT>""" <EOL> for k , v in iteritems ( second_dict ) : <EOL> if k in first_dict and not compat ( v , first_dict [ k ] ) : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' % k ) <EOL> def remove_incompatible_items ( first_dict , second_dict , compat = equivalent ) : <EOL> """<STR_LIT>""" <EOL> for k in list ( first_dict ) : <EOL> if ( k not in second_dict or <EOL> ( k in second_dict and <EOL> not compat ( first_dict [ k ] , second_dict [ k ] ) ) ) : <EOL> del first_dict [ k ] <EOL> def is_dict_like ( value ) : <EOL> return hasattr ( value , '<STR_LIT>' ) and hasattr ( value , '<STR_LIT>' ) <EOL> def is_full_slice ( value ) : <EOL> return isinstance ( value , slice ) and value == slice ( None ) <EOL> def combine_pos_and_kw_args ( pos_kwargs , kw_kwargs , func_name ) : <EOL> if pos_kwargs is not None : <EOL> if not is_dict_like ( pos_kwargs ) : <EOL> raise ValueError ( '<STR_LIT>' <EOL> % func_name ) <EOL> if kw_kwargs : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' % func_name ) <EOL> return pos_kwargs <EOL> else : <EOL> return kw_kwargs <EOL> _SCALAR_TYPES = ( datetime . datetime , datetime . date , datetime . timedelta ) <EOL> def is_scalar ( value ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( value , '<STR_LIT>' ) : <EOL> return value . ndim == <NUM_LIT:0> <EOL> return ( np . isscalar ( value ) or <EOL> isinstance ( value , _SCALAR_TYPES ) or <EOL> value is None ) <EOL> def is_valid_numpy_dtype ( dtype ) : <EOL> try : <EOL> np . dtype ( dtype ) <EOL> except ( TypeError , ValueError ) : <EOL> return False <EOL> else : <EOL> return True <EOL> def tuple_to_0darray ( value ) : <EOL> result = np . empty ( ( <NUM_LIT:1> , ) , dtype = object ) <EOL> result [ : ] = [ value ] <EOL> result . shape = ( ) <EOL> return result <EOL> def dict_equiv ( first , second , compat = equivalent ) : <EOL> """<STR_LIT>""" <EOL> for k in first : <EOL> if k not in second or not compat ( first [ k ] , second [ k ] ) : <EOL> return False <EOL> for k in second : <EOL> if k not in first : <EOL> return False <EOL> return True <EOL> def ordered_dict_intersection ( first_dict , second_dict , compat = equivalent ) : <EOL> """<STR_LIT>""" <EOL> new_dict = OrderedDict ( first_dict ) <EOL> remove_incompatible_items ( new_dict , second_dict , compat ) <EOL> return new_dict <EOL> class SingleSlotPickleMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> def __getstate__ ( self ) : <EOL> return getattr ( self , self . __slots__ [ <NUM_LIT:0> ] ) <EOL> def __setstate__ ( self , state ) : <EOL> setattr ( self , self . __slots__ [ <NUM_LIT:0> ] , state ) <EOL> class Frozen ( Mapping , SingleSlotPickleMixin ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = [ '<STR_LIT>' ] <EOL> def __init__ ( self , mapping ) : <EOL> self . mapping = mapping <EOL> def __getitem__ ( self , key ) : <EOL> return self . mapping [ key ] <EOL> def __iter__ ( self ) : <EOL> return iter ( self . mapping ) <EOL> def __len__ ( self ) : <EOL> return len ( self . mapping ) <EOL> def __contains__ ( self , key ) : <EOL> return key in self . mapping <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( type ( self ) . __name__ , self . mapping ) <EOL> def FrozenOrderedDict ( * args , ** kwargs ) : <EOL> return Frozen ( OrderedDict ( * args , ** kwargs ) ) <EOL> class SortedKeysDict ( MutableMapping , SingleSlotPickleMixin ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = [ '<STR_LIT>' ] <EOL> def __init__ ( self , mapping = None ) : <EOL> self . mapping = { } if mapping is None else mapping <EOL> def __getitem__ ( self , key ) : <EOL> return self . mapping [ key ] <EOL> def __setitem__ ( self , key , value ) : <EOL> self . mapping [ key ] = value <EOL> def __delitem__ ( self , key ) : <EOL> del self . mapping [ key ] <EOL> def __iter__ ( self ) : <EOL> return iter ( sorted ( self . mapping ) ) <EOL> def __len__ ( self ) : <EOL> return len ( self . mapping ) <EOL> def __contains__ ( self , key ) : <EOL> return key in self . mapping <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( type ( self ) . __name__ , self . mapping ) <EOL> def copy ( self ) : <EOL> return type ( self ) ( self . mapping . copy ( ) ) <EOL> class ChainMap ( MutableMapping , SingleSlotPickleMixin ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = [ '<STR_LIT>' ] <EOL> def __init__ ( self , * maps ) : <EOL> self . maps = maps <EOL> def __getitem__ ( self , key ) : <EOL> for mapping in self . maps : <EOL> try : <EOL> return mapping [ key ] <EOL> except KeyError : <EOL> pass <EOL> raise KeyError ( key ) <EOL> def __setitem__ ( self , key , value ) : <EOL> self . maps [ <NUM_LIT:0> ] [ key ] = value <EOL> def __delitem__ ( self , value ) : <EOL> raise NotImplementedError <EOL> def __iter__ ( self ) : <EOL> seen = set ( ) <EOL> for mapping in self . maps : <EOL> for item in mapping : <EOL> if item not in seen : <EOL> yield item <EOL> seen . add ( item ) <EOL> def __len__ ( self ) : <EOL> raise len ( iter ( self ) ) <EOL> class NdimSizeLenMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> @ property <EOL> def ndim ( self ) : <EOL> return len ( self . shape ) <EOL> @ property <EOL> def size ( self ) : <EOL> return int ( np . prod ( self . shape ) ) <EOL> def __len__ ( self ) : <EOL> try : <EOL> return self . shape [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> class NDArrayMixin ( NdimSizeLenMixin ) : <EOL> """<STR_LIT>""" <EOL> @ property <EOL> def dtype ( self ) : <EOL> return self . array . dtype <EOL> @ property <EOL> def shape ( self ) : <EOL> return self . array . shape <EOL> def __array__ ( self , dtype = None ) : <EOL> return np . asarray ( self [ ... ] , dtype = dtype ) <EOL> def __getitem__ ( self , key ) : <EOL> return self . array [ key ] <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( type ( self ) . __name__ , self . array ) <EOL> @ contextlib . contextmanager <EOL> def close_on_error ( f ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> yield <EOL> except Exception : <EOL> f . close ( ) <EOL> raise <EOL> def is_remote_uri ( path ) : <EOL> return bool ( re . search ( '<STR_LIT>' , path ) ) <EOL> def is_uniform_spaced ( arr , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> arr = np . array ( arr , dtype = float ) <EOL> diffs = np . diff ( arr ) <EOL> return np . isclose ( diffs . min ( ) , diffs . max ( ) , ** kwargs ) <EOL> def hashable ( v ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> hash ( v ) <EOL> except TypeError : <EOL> return False <EOL> return True <EOL> def not_implemented ( * args , ** kwargs ) : <EOL> return NotImplemented </s>
