Пример #1
0
def drop_outliers():
    try:
        request_dict = request.get_json()
        jsonstr = request_dict['jsonStr']
        z_values = request_dict['z_values']
        standard_deviation = request_dict['standard_deviation']
        keys = list(z_values.keys())
        vals = [z_values[x] for x in keys]
        transpose_vals = pd.np.transpose(vals).tolist()

        bools = []
        for i in transpose_vals:
            b = all(item < standard_deviation for item in i)
            bools.append(b)
        df = pd.read_json(json.dumps(eval(jsonstr)), orient='split')
        filtered_df = df[bools]
        df_json = filtered_df.to_json(orient='split', date_format='iso')
        response = app.response_class(response=df_json,
                                      status=200,
                                      mimetype='application/json')
    except:
        exception = ExceptionHelpers.format_exception(sys.exc_info())
        response = app.response_class(response=exception,
                                      status=400,
                                      mimetype='application/json')
    return response
Пример #2
0
def naive_bayes():
    try:
        request_dict = request.get_json()
        traning_features = request_dict['traning_features']
        traning_targets = request_dict['traning_targets']
        test_features = request_dict['test_features']

        from sklearn.preprocessing import StandardScaler
        sc = StandardScaler()
        X_train = sc.fit_transform(pd.np.array(traning_features))
        X_test = sc.transform(pd.np.array(test_features))

        # Fitting Naive Bayes to the Training set
        from sklearn.naive_bayes import GaussianNB
        classifier = GaussianNB()
        classifier.fit(X_train, traning_targets)

        # Predicting the Test set results
        y_pred = classifier.predict(X_test)
        json_response = json.dumps(y_pred.tolist())
        response = app.response_class(response=json_response,
                                      status=200,
                                      mimetype='application/json')
    except:
        exception = ExceptionHelpers.format_exception(sys.exc_info())
        print(exception)
        response = app.response_class(response=exception,
                                      status=400,
                                      mimetype='application/json')
        print(exception)
    return response
Пример #3
0
def merge():
    try:
        request_dict = request.get_json()
        left_df = request_dict['leftDf']
        right_df = request_dict['rightDf']
        how = request_dict['how']
        left_on = request_dict['left_on']
        right_on = request_dict['right_on']
        left_index = request_dict['left_index']
        right_index = request_dict['right_index']
        left_df = pd.read_json(json.dumps(eval(left_df)), orient='split')
        right_df = pd.read_json(json.dumps(eval(right_df)), orient='split')
        df = left_df.merge(right_df,
                           how=how,
                           left_on=left_on,
                           right_on=right_on,
                           left_index=left_index,
                           right_index=right_index)
        df_json = df.to_json(orient='split')
        response = app.response_class(response=df_json,
                                      status=200,
                                      mimetype='application/json')
    except:
        exception = ExceptionHelpers.format_exception(sys.exc_info())
        response = app.response_class(response=exception,
                                      status=400,
                                      mimetype='application/json')
    return response
Пример #4
0
def by_contains():
    try:
        request_dict = request.get_json()
        jsonstr = request_dict['jsonStr']
        items = request_dict['item']
        axis = request_dict['axis']
        axis = int(axis)
        df = pd.read_json(json.dumps(eval(jsonstr)), orient='split')
        df = df.filter(like=items, axis=axis)
        if df.empty:
            raise EmptyDataframe(
                {
                    "content":
                    "Returned empty dataframe, check your filter index and items"
                },
                status_code=400)
        df_json = df.to_json(orient='split')
        response = app.response_class(response=df_json,
                                      status=200,
                                      mimetype='application/json')
    except:
        exception = ExceptionHelpers.format_exception(sys.exc_info())
        response = app.response_class(response=exception,
                                      status=400,
                                      mimetype='application/json')
    return response
Пример #5
0
def by_dict():
    try:
        request_dict = request.get_json()
        jsonstr = request_dict['jsonStr']
        df = pd.DataFrame(eval(jsonstr))
        df_json = df.to_json(orient='split')
        response = app.response_class(response=df_json,
                                      status=200,
                                      mimetype='application/json')
    except:
        exception = ExceptionHelpers.format_exception(sys.exc_info())
        response = app.response_class(response=exception,
                                      status=400,
                                      mimetype='application/json')
    return response
Пример #6
0
def describe():
    try:
        request_dict = request.get_json()
        jsonstr = request_dict['jsonStr']
        df = pd.read_json(json.dumps(eval(jsonstr)), orient='split')
        dataframe_describe = df.describe(include='all')
        df_json = dataframe_describe.to_json(orient='split', date_format='iso')
        response = app.response_class(response=df_json,
                                      status=200,
                                      mimetype='application/json')
    except:
        exception = ExceptionHelpers.format_exception(sys.exc_info())
        response = app.response_class(response=exception,
                                      status=400,
                                      mimetype='application/json')
    return response
Пример #7
0
def by_bool_expression():
    try:
        request_dict = request.get_json()
        jsonstr = request_dict['jsonStr']
        expression = request_dict['boolExpression']
        df = pd.read_json(json.dumps(eval(jsonstr)), orient='split')
        selected_rows = eval(expression)
        df_json = selected_rows.to_json(orient='split', date_format='iso')
        response = app.response_class(response=df_json,
                                      status=200,
                                      mimetype='application/json')
    except:
        exception = ExceptionHelpers.format_exception(sys.exc_info())
        response = app.response_class(response=exception,
                                      status=400,
                                      mimetype='application/json')
    return response
Пример #8
0
def by_label():
    try:
        request_dict = request.get_json()
        jsonstr = request_dict['jsonStr']
        row_label = request_dict['rowLabel']
        column_label = request_dict['columnLabel']
        df = pd.read_json(json.dumps(eval(jsonstr)), orient='split')
        selected_rows = df.loc[row_label, column_label]
        df_json = selected_rows.to_json(orient='split', date_format='iso')
        response = app.response_class(response=df_json,
                                      status=200,
                                      mimetype='application/json')
    except:
        exception = ExceptionHelpers.format_exception(sys.exc_info())
        response = app.response_class(response=exception,
                                      status=400,
                                      mimetype='application/json')
    return response
Пример #9
0
def by_contains():
    try:
        request_dict = request.get_json()
        jsonstr = request_dict['jsonStr']
        column = request_dict['column']
        contains_str = request_dict['containsString']
        df = pd.read_json(json.dumps(eval(jsonstr)), orient='split')
        selected_rows = df[df[column].str.contains(contains_str, na=False)]
        df_json = selected_rows.to_json(orient='split', date_format='iso')
        response = app.response_class(response=df_json,
                                      status=200,
                                      mimetype='application/json')
    except:
        exception = ExceptionHelpers.format_exception(sys.exc_info())
        response = app.response_class(response=exception,
                                      status=400,
                                      mimetype='application/json')
    return response
Пример #10
0
def tabulate():
    try:
        request_dict = request.get_json()
        jsonstr = request_dict['jsonStr']
        jsonstr = json.dumps(jsonstr)
        df = pd.read_json(eval(jsonstr), orient='split')
        headers = 'keys'
        tableformat = 'orgtbl'
        tabulated_df = tb(df, headers=headers, tablefmt=tableformat)
        response = app.response_class(response=tabulated_df,
                                      status=200,
                                      mimetype='application/json')
    except:
        exception = ExceptionHelpers.format_exception(sys.exc_info())
        response = app.response_class(response=exception,
                                      status=400,
                                      mimetype='application/json')
    return response
Пример #11
0
def min_value():
    try:
        request_dict = request.get_json()
        jsonstr = request_dict['jsonStr']
        df = pd.read_json(json.dumps(eval(jsonstr)), orient='split')
        dataframe_min_value = df.min()
        values = dataframe_min_value.values.tolist()
        keys = dataframe_min_value.index.values.tolist()
        min_value_dict = dict(zip(keys, values))
        response = app.response_class(response=json.dumps(min_value_dict),
                                      status=200,
                                      mimetype='application/json')
    except:
        exception = ExceptionHelpers.format_exception(sys.exc_info())
        response = app.response_class(response=exception,
                                      status=400,
                                      mimetype='application/json')
    return response
Пример #12
0
def cumulative_sum():
    try:
        request_dict = request.get_json()
        jsonstr = request_dict['jsonStr']
        df = pd.read_json(json.dumps(eval(jsonstr)), orient='split')
        dataframe_cumsum = df.cumsum()
        values = list(map(list, zip(*dataframe_cumsum.values.tolist())))
        keys = dataframe_cumsum.columns.values.tolist()
        cum_dict = dict(zip(keys, values))
        response = app.response_class(response=json.dumps(cum_dict),
                                      status=200,
                                      mimetype='application/json')
    except:
        exception = ExceptionHelpers.format_exception(sys.exc_info())
        response = app.response_class(response=exception,
                                      status=400,
                                      mimetype='application/json')
    return response
Пример #13
0
def shape():
    try:
        request_dict = request.get_json()
        jsonstr = request_dict['jsonStr']
        df = pd.read_json(json.dumps(eval(jsonstr)), orient='split')
        dataframe_shape = df.shape
        response = app.response_class(
            response=json.dumps(dataframe_shape),
            status=200,
            mimetype='application/json'
        )
    except:
        exception = ExceptionHelpers.format_exception(sys.exc_info())
        response = app.response_class(
            response=exception,
            status=400,
            mimetype='application/json'
        )
    return response
Пример #14
0
def sum():
    try:
        request_dict = request.get_json()
        jsonstr = request_dict['jsonStr']
        axis = request_dict['axis']
        df = pd.read_json(json.dumps(eval(jsonstr)), orient='split')
        dataframe_sum = df.sum(axis=axis, skipna=True)
        values = dataframe_sum.values.tolist()
        keys = dataframe_sum.index.values.tolist()
        sum_dict = dict(zip(keys, values))
        response = app.response_class(response=json.dumps(sum_dict),
                                      status=200,
                                      mimetype='application/json')
    except:
        exception = ExceptionHelpers.format_exception(sys.exc_info())
        response = app.response_class(response=exception,
                                      status=400,
                                      mimetype='application/json')
    return response
Пример #15
0
def by_excel():
    try:
        request_dict = request.get_json()
        file_path = request_dict['filePath']
        sheet_name = request_dict['sheetName']
        df = pd.read_excel(file_path,
                           sheet_name=sheet_name,
                           header=0,
                           na_values=['', ' '])
        df_json = df.to_json(orient='split', date_format='iso')
        response = app.response_class(response=df_json,
                                      status=200,
                                      mimetype='application/json')
    except:
        exception = ExceptionHelpers.format_exception(sys.exc_info())
        response = app.response_class(response=exception,
                                      status=400,
                                      mimetype='application/json')
    return response
Пример #16
0
def index():
    try:
        request_dict = request.get_json()
        jsonstr = request_dict['jsonStr']
        df = pd.read_json(json.dumps(eval(jsonstr)), orient='split')
        dataframe_index = {"index":df.index.values.tolist(),"dtype":str(df.index.dtype)}
        response = app.response_class(
            response=json.dumps(dataframe_index),
            status=200,
            mimetype='application/json'
        )
    except:
        exception = ExceptionHelpers.format_exception(sys.exc_info())
        response = app.response_class(
            response=exception,
            status=400,
            mimetype='application/json'
        )
    return response
Пример #17
0
def info():
    try:
        request_dict = request.get_json()
        jsonstr = request_dict['jsonStr']
        df = pd.read_json(json.dumps(eval(jsonstr)), orient='split')
        buf = io.StringIO()
        dataframe_info = df.info(buf=buf)
        response = app.response_class(
            response=buf.getvalue(),
            status=200,
            mimetype='application/json'
        )
    except:
        exception = ExceptionHelpers.format_exception(sys.exc_info())
        response = app.response_class(
            response=exception,
            status=400,
            mimetype='application/json'
        )
    return response
Пример #18
0
def by_datatype():
    try:
        request_dict = request.get_json()
        jsonstr = request_dict['jsonStr']
        include = request_dict['include']
        df = pd.read_json(json.dumps(eval(jsonstr)), orient='split')
        selected_columns = df.select_dtypes(include=include)
        df_json = selected_columns.to_json(orient='split', date_format='iso')
        response = app.response_class(
            response=df_json,
            status=200,
            mimetype='application/json'
        )
    except:
        exception = ExceptionHelpers.format_exception(sys.exc_info())
        response = app.response_class(
            response=exception,
            status=400,
            mimetype='application/json'
        )
    return response
Пример #19
0
def max_index_value():
    try:
        request_dict = request.get_json()
        jsonstr = request_dict['jsonStr']
        axis = request_dict['axis']
        df = pd.read_json(json.dumps(eval(jsonstr)), orient='split')
        df = df.select_dtypes(exclude=['object'])
        dataframe_max_index_value = df.idxmax(axis=axis, skipna=True)
        values = dataframe_max_index_value.values.tolist()
        keys = dataframe_max_index_value.index.values.tolist()
        max_index_value_dict = dict(zip(keys, values))
        response = app.response_class(
            response=json.dumps(max_index_value_dict),
            status=200,
            mimetype='application/json')
    except:
        exception = ExceptionHelpers.format_exception(sys.exc_info())
        response = app.response_class(response=exception,
                                      status=400,
                                      mimetype='application/json')
    return response
Пример #20
0
def concatenate():
    try:
        request_dict = request.get_json()
        df_json_list = request_dict['df_json_list']
        axis = request_dict['axis']
        join = request_dict['join']
        ignore_index = request_dict['ignore_index']
        df_list = []
        for i in df_json_list:
            df_list.append(pd.read_json(json.dumps(eval(i)), orient='split'))
        df = pd.concat(df_list, axis=axis, ignore_index=ignore_index)
        df_json = df.to_json(orient='split')
        response = app.response_class(response=df_json,
                                      status=200,
                                      mimetype='application/json')
    except:
        exception = ExceptionHelpers.format_exception(sys.exc_info())
        response = app.response_class(response=exception,
                                      status=400,
                                      mimetype='application/json')
    return response
Пример #21
0
def count():
    try:
        request_dict = request.get_json()
        jsonstr = request_dict['jsonStr']
        df = pd.read_json(json.dumps(eval(jsonstr)), orient='split')
        from scipy import stats
        dataframe_count = df.count()
        count_dict = dict(zip(dataframe_count.index.values.tolist(), dataframe_count.values.tolist()))
        response = app.response_class(
            response=json.dumps(count_dict),
            status=200,
            mimetype='application/json'
        )
    except:
        exception = ExceptionHelpers.format_exception(sys.exc_info())
        response = app.response_class(
            response=exception,
            status=400,
            mimetype='application/json'
        )
    return response
Пример #22
0
def z_score():
    try:
        request_dict = request.get_json()
        jsonstr = request_dict['jsonStr']
        columns = request_dict['columns']
        df = pd.read_json(json.dumps(eval(jsonstr)), orient='split')
        if (columns is None):
            z_values = pd.np.abs(stats.zscore(df))
            z_values = pd.np.transpose(z_values)
            result = dict(zip(df.columns.values.tolist(), z_values.tolist()))
        else:
            z_values = pd.np.abs(stats.zscore(df[columns]))
            z_values = pd.np.transpose(z_values)
            result = dict(zip(columns, z_values.tolist()))
            print(result)
        response = app.response_class(response=json.dumps(result),
                                      status=200,
                                      mimetype='application/json')
    except:
        exception = ExceptionHelpers.format_exception(sys.exc_info())
        response = app.response_class(response=exception,
                                      status=400,
                                      mimetype='application/json')
    return response