<s> from skspec . units . abcunits import ConversionUnit , UnitError <EOL> from datetime import timedelta <EOL> from pandas import DatetimeIndex , date_range , Index <EOL> import datetime <EOL> import numpy as np <EOL> class DatetimeCanonicalError ( UnitError ) : <EOL> """<STR_LIT>""" <EOL> class DateTime ( ConversionUnit ) : <EOL> """<STR_LIT>""" <EOL> symbol = '<STR_LIT:t>' <EOL> category = '<STR_LIT:time>' <EOL> short = '<STR_LIT>' <EOL> full = '<STR_LIT>' <EOL> def to_canonical ( self , x ) : <EOL> """<STR_LIT:U+0020>""" <EOL> raise DatetimeCanonicalError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def from_canonical ( self , x ) : <EOL> """<STR_LIT:U+0020>""" <EOL> raise DatetimeCanonicalError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> class IntvlUnit ( ConversionUnit ) : <EOL> """<STR_LIT>""" <EOL> symbol = r'<STR_LIT>' <EOL> category = '<STR_LIT:time>' <EOL> class TimeDelta ( IntvlUnit ) : <EOL> """<STR_LIT>""" <EOL> short = '<STR_LIT>' <EOL> full = '<STR_LIT>' <EOL> @ staticmethod <EOL> def to_canonical ( x ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( x , '<STR_LIT>' ) : <EOL> return np . array ( [ v . total_seconds ( ) for v in x ] , dtype = '<STR_LIT>' ) <EOL> else : <EOL> return x . total_seconds ( ) <EOL> @ staticmethod <EOL> def from_canonical ( x ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( x , '<STR_LIT>' ) : <EOL> return np . array ( [ datetime . timedelta ( seconds = v ) for v in x ] ) <EOL> else : <EOL> return datetime . timedelta ( seconds = x ) <EOL> class Seconds ( IntvlUnit ) : <EOL> short = '<STR_LIT:s>' <EOL> full = '<STR_LIT>' <EOL> _canonical = True <EOL> @ staticmethod <EOL> def to_canonical ( x ) : <EOL> return x <EOL> @ staticmethod <EOL> def from_canonical ( x ) : <EOL> return x <EOL> class Minutes ( IntvlUnit ) : <EOL> short = '<STR_LIT:m>' <EOL> full = '<STR_LIT>' <EOL> @ staticmethod <EOL> def to_canonical ( x ) : <EOL> return x * <NUM_LIT> <EOL> @ staticmethod <EOL> def from_canonical ( x ) : <EOL> return x / <NUM_LIT> <EOL> class Nanoseconds ( IntvlUnit ) : <EOL> short = '<STR_LIT>' <EOL> full = '<STR_LIT>' <EOL> @ staticmethod <EOL> def to_canonical ( x ) : <EOL> return x * <NUM_LIT:10> ** - <NUM_LIT:9> <EOL> @ staticmethod <EOL> def from_canonical ( x ) : <EOL> return x / <NUM_LIT:10> ** - <NUM_LIT:9> <EOL> class Microseconds ( IntvlUnit ) : <EOL> short = '<STR_LIT>' <EOL> full = '<STR_LIT>' <EOL> @ staticmethod <EOL> def to_canonical ( x ) : <EOL> return x * <NUM_LIT:10> ** - <NUM_LIT:6> <EOL> @ staticmethod <EOL> def from_canonical ( x ) : <EOL> return x / <NUM_LIT:10> ** - <NUM_LIT:6> <EOL> class Milliseconds ( IntvlUnit ) : <EOL> short = '<STR_LIT>' <EOL> full = '<STR_LIT>' <EOL> @ staticmethod <EOL> def to_canonical ( x ) : <EOL> return x * <NUM_LIT:10> ** - <NUM_LIT:3> <EOL> @ staticmethod <EOL> def from_canonical ( x ) : <EOL> return x / <NUM_LIT:10> ** - <NUM_LIT:3> <EOL> class Hours ( IntvlUnit ) : <EOL> short = '<STR_LIT:h>' <EOL> full = '<STR_LIT>' <EOL> @ staticmethod <EOL> def to_canonical ( x ) : <EOL> return x * <NUM_LIT> <EOL> @ staticmethod <EOL> def from_canonical ( x ) : <EOL> return x / <NUM_LIT> <EOL> class Days ( IntvlUnit ) : <EOL> short = '<STR_LIT:d>' <EOL> full = '<STR_LIT>' <EOL> @ staticmethod <EOL> def to_canonical ( x ) : <EOL> return x * <NUM_LIT> <EOL> @ staticmethod <EOL> def from_canonical ( x ) : <EOL> return x / <NUM_LIT> <EOL> class Years ( IntvlUnit ) : <EOL> short = '<STR_LIT:y>' <EOL> full = '<STR_LIT>' <EOL> @ staticmethod <EOL> def to_canonical ( x ) : <EOL> return x * <NUM_LIT> <EOL> @ staticmethod <EOL> def from_canonical ( x ) : <EOL> return x / <NUM_LIT> <EOL> _intvlunits = ( <EOL> DateTime ( ) , <EOL> Nanoseconds ( ) , <EOL> Microseconds ( ) , <EOL> Milliseconds ( ) , <EOL> Seconds ( ) , <EOL> Minutes ( ) , <EOL> Hours ( ) , <EOL> Days ( ) , <EOL> TimeDelta ( ) <EOL> ) <EOL> INTVLUNITS = dict ( ( obj . short , obj ) for obj in _intvlunits ) <EOL> print INTVLUNITS . keys ( ) </s>
