def df2arff(df, dataset_name, pods_data):
def java_simple_date(date_format):<EOL><INDENT>date_format = date_format.replace('<STR_LIT>', '<STR_LIT>').replace('<STR_LIT>', '<STR_LIT>').replace('<STR_LIT>', '<STR_LIT>').replace('<STR_LIT>', '<STR_LIT>')<EOL>return date_format.replace('<STR_LIT>', '<STR_LIT>').replace('<STR_LIT>', '<STR_LIT>').replace('<STR_LIT>', '<STR_LIT>').replace('<STR_LIT>', '<STR_LIT>')<EOL><DEDENT>def tidy_field(atr):<EOL><INDENT>return str(atr).replace('<STR_LIT>', '<STR_LIT:/>').replace('<STR_LIT:U+0020>', '<STR_LIT:_>')<EOL><DEDENT>types = {'<STR_LIT>': [str], '<STR_LIT>': [int, np.int64, np.uint8], '<STR_LIT>': [np.float64]}<EOL>d = {}<EOL>d['<STR_LIT>'] = []<EOL>for atr in df.columns:<EOL><INDENT>if isinstance(atr, str):<EOL><INDENT>if len(atr)><NUM_LIT:8> and atr[:<NUM_LIT:9>] == '<STR_LIT>':<EOL><INDENT>import json<EOL>elements = json.loads(atr[<NUM_LIT:9>:-<NUM_LIT:1>])<EOL>d['<STR_LIT>'].append((tidy_field(elements[<NUM_LIT:1>]),<EOL>list(elements[<NUM_LIT:0>].keys())))<EOL>mask = {}<EOL>c = pd.Series(index=df.index)<EOL>for key, val in elements[<NUM_LIT:0>].items():<EOL><INDENT>mask = df[atr]==val<EOL>c[mask] = key<EOL><DEDENT>df[atr] = c<EOL>continue<EOL><DEDENT>if len(atr)><NUM_LIT:7> and atr[:<NUM_LIT:8>] == '<STR_LIT>':<EOL><INDENT>name = atr[<NUM_LIT:8>:-<NUM_LIT:1>]<EOL>d['<STR_LIT>'].append((tidy_field(name), '<STR_LIT>'))<EOL>df[atr] = df[atr].astype(int)<EOL>continue<EOL><DEDENT>if len(atr)><NUM_LIT:7> and atr[:<NUM_LIT:8>]=='<STR_LIT>':<EOL><INDENT>from matplotlib.dates import num2date<EOL>elements = atr[<NUM_LIT:8>:-<NUM_LIT:1>].split('<STR_LIT:U+002C>')<EOL>d['<STR_LIT>'].append((elements[<NUM_LIT:0>] + '<STR_LIT>' + java_simple_date(elements[<NUM_LIT:1>]), '<STR_LIT>'))<EOL>df[atr] = num2date(df[atr].values) <EOL>df[atr] = df[atr].dt.strftime(elements[<NUM_LIT:1>])<EOL>continue<EOL><DEDENT>if len(atr)><NUM_LIT:9> and atr[:<NUM_LIT:10>]=='<STR_LIT>':<EOL><INDENT>def timestamp2date(values):<EOL><INDENT>import datetime<EOL>"""<STR_LIT>"""<EOL>new = []<EOL>for value in values:<EOL><INDENT>new.append(np.datetime64(datetime.datetime.fromtimestamp(value)))<EOL><DEDENT>return np.asarray(new)<EOL><DEDENT>elements = atr[<NUM_LIT:10>:-<NUM_LIT:1>].split('<STR_LIT:U+002C>')<EOL>d['<STR_LIT>'].append((elements[<NUM_LIT:0>] + '<STR_LIT>' + java_simple_date(elements[<NUM_LIT:1>]), '<STR_LIT>'))<EOL>df[atr] = timestamp2date(df[atr].values) <EOL>df[atr] = df[atr].dt.strftime(elements[<NUM_LIT:1>])<EOL>continue<EOL><DEDENT>if len(atr)><NUM_LIT:10> and atr[:<NUM_LIT:11>]=='<STR_LIT>':<EOL><INDENT>elements = atr[<NUM_LIT:11>:-<NUM_LIT:1>].split('<STR_LIT:U+002C>')<EOL>d['<STR_LIT>'].append((elements[<NUM_LIT:0>] + '<STR_LIT>' + java_simple_date(elements[<NUM_LIT:1>]), '<STR_LIT>'))<EOL>df[atr] = df[atr].dt.strftime(elements[<NUM_LIT:1>])<EOL>continue<EOL><DEDENT>if len(atr)><NUM_LIT:11> and atr[:<NUM_LIT:12>]=='<STR_LIT>':<EOL><INDENT>def decyear2date(values):<EOL><INDENT>"""<STR_LIT>"""<EOL>new = []<EOL>for i, decyear in enumerate(values):<EOL><INDENT>year = int(np.floor(decyear))<EOL>dec = decyear-year<EOL>end = np.datetime64(str(year+<NUM_LIT:1>)+'<STR_LIT>')<EOL>start = np.datetime64(str(year)+'<STR_LIT>')<EOL>diff=end-start<EOL>days = dec*(diff/np.timedelta64(<NUM_LIT:1>, '<STR_LIT:D>'))<EOL>add = np.timedelta64(int(np.round(days)), '<STR_LIT:D>')<EOL>new.append(start+add)<EOL><DEDENT>return np.asarray(new)<EOL><DEDENT>elements = atr[<NUM_LIT:12>:-<NUM_LIT:1>].split('<STR_LIT:U+002C>')<EOL>d['<STR_LIT>'].append((elements[<NUM_LIT:0>] + '<STR_LIT>' + java_simple_date(elements[<NUM_LIT:1>]), '<STR_LIT>'))<EOL>df[atr] = decyear2date(df[atr].values) <EOL>df[atr] = df[atr].dt.strftime(elements[<NUM_LIT:1>])<EOL>continue<EOL><DEDENT><DEDENT>field = tidy_field(atr)<EOL>el = df[atr][<NUM_LIT:0>]<EOL>type_assigned=False<EOL>for t in types:<EOL><INDENT>if isinstance(el, tuple(types[t])):<EOL><INDENT>d['<STR_LIT>'].append((field, t))<EOL>type_assigned=True<EOL>break<EOL><DEDENT><DEDENT>if not type_assigned:<EOL><INDENT>import json<EOL>d['<STR_LIT>'].append((field+'<STR_LIT>', '<STR_LIT>'))<EOL>df[atr] = df[atr].apply(json.dumps)<EOL><DEDENT><DEDENT>d['<STR_LIT:data>'] = []<EOL>for ind, row in df.iterrows():<EOL><INDENT>d['<STR_LIT:data>'].append(list(row))<EOL><DEDENT>import textwrap as tw<EOL>width = <NUM_LIT><EOL>d['<STR_LIT:description>'] = dataset_name + "<STR_LIT>"<EOL>if '<STR_LIT:info>' in pods_data and pods_data['<STR_LIT:info>']:<EOL><INDENT>d['<STR_LIT:description>'] += "<STR_LIT:\n>".join(tw.wrap(pods_data['<STR_LIT:info>'], width)) + "<STR_LIT>"<EOL><DEDENT>if '<STR_LIT>' in pods_data and pods_data['<STR_LIT>']:<EOL><INDENT>d['<STR_LIT:description>'] += "<STR_LIT:\n>".join(tw.wrap(pods_data['<STR_LIT>'], width))<EOL><DEDENT>if '<STR_LIT>' in pods_data and pods_data['<STR_LIT>']:<EOL><INDENT>d['<STR_LIT:description>'] += "<STR_LIT>" + "<STR_LIT>" "<STR_LIT>" + "<STR_LIT:\n>".join(tw.wrap(pods_data['<STR_LIT>'], width))<EOL><DEDENT>d['<STR_LIT>'] = dataset_name<EOL>import arff<EOL>string = arff.dumps(d)<EOL>import re<EOL>string = re.sub(r'<STR_LIT>',<EOL>r'<STR_LIT>',<EOL>string)<EOL>f = open(dataset_name + '<STR_LIT>', '<STR_LIT:w>')<EOL>f.write(string)<EOL>f.close()<EOL>
Write an arff file from a data set loaded in from pods