<s> from pandas . compat import lrange <EOL> import numpy as np <EOL> import pandas . core . common as com <EOL> from pandas . core . frame import DataFrame <EOL> import pandas . core . nanops as nanops <EOL> def pivot_annual ( series , freq = None ) : <EOL> """<STR_LIT>""" <EOL> index = series . index <EOL> year = index . year <EOL> years = nanops . unique1d ( year ) <EOL> if freq is not None : <EOL> freq = freq . upper ( ) <EOL> else : <EOL> freq = series . index . freq <EOL> if freq == '<STR_LIT:D>' : <EOL> width = <NUM_LIT> <EOL> offset = index . dayofyear - <NUM_LIT:1> <EOL> offset [ ( ~ isleapyear ( year ) ) & ( offset >= <NUM_LIT> ) ] += <NUM_LIT:1> <EOL> columns = lrange ( <NUM_LIT:1> , <NUM_LIT> ) <EOL> elif freq in ( '<STR_LIT:M>' , '<STR_LIT>' ) : <EOL> width = <NUM_LIT:12> <EOL> offset = index . month - <NUM_LIT:1> <EOL> columns = lrange ( <NUM_LIT:1> , <NUM_LIT> ) <EOL> elif freq == '<STR_LIT:H>' : <EOL> width = <NUM_LIT> <EOL> grouped = series . groupby ( series . index . year ) <EOL> defaulted = grouped . apply ( lambda x : x . reset_index ( drop = True ) ) <EOL> defaulted . index = defaulted . index . droplevel ( <NUM_LIT:0> ) <EOL> offset = np . asarray ( defaulted . index ) <EOL> offset [ ~ isleapyear ( year ) & ( offset >= <NUM_LIT> ) ] += <NUM_LIT> <EOL> columns = lrange ( <NUM_LIT:1> , <NUM_LIT> ) <EOL> else : <EOL> raise NotImplementedError ( freq ) <EOL> flat_index = ( year - years . min ( ) ) * width + offset <EOL> flat_index = com . _ensure_platform_int ( flat_index ) <EOL> values = np . empty ( ( len ( years ) , width ) ) <EOL> values . fill ( np . nan ) <EOL> values . put ( flat_index , series . values ) <EOL> return DataFrame ( values , index = years , columns = columns ) <EOL> def isleapyear ( year ) : <EOL> """<STR_LIT>""" <EOL> year = np . asarray ( year ) <EOL> return np . logical_or ( year % <NUM_LIT> == <NUM_LIT:0> , <EOL> np . logical_and ( year % <NUM_LIT:4> == <NUM_LIT:0> , year % <NUM_LIT:100> > <NUM_LIT:0> ) ) </s>
