def to_json_stat(input_df, value='value', output='list', version='1.3'):
    """Encode pandas.DataFrame object into JSON-stat format. The DataFrames
       must have exactly one value column.

    Args:
      df(pandas.DataFrame): pandas data frame (or list of data frames) to
      encode.
      value (string, optional): name of the value column. Defaults to 'value'.
      output(string): accepts two values: 'list' or 'dict'. Produce list of\
                      dicts or dict of dicts as output.
      version(string): desired json-stat version. 2.0 is preferred now.\
                       Apart from this, only older 1.3 format is accepted,
                       which is the default parameter in order to preserve
                       backwards compatibility.

    Returns:
      output(string): String with JSON-stat object.

    """

    warnings.warn(
        "Shouldn't use this function anymore! Now use write() methods of"
        "Dataset, Collection or Dimension.",
        DeprecationWarning
    )
    data = []
    if output == 'list':
        result = []
    elif output == 'dict':
        result = OrderedDict({})
    if isinstance(input_df, pd.DataFrame):
        data.append(input_df)
    else:
        data = input_df
    for row, dataframe in enumerate(data):
        dims = data[row].filter([item for item in data[row].columns.values
                                 if item not in value])
        if len(dims.columns.values) != len(set(dims.columns.values)):
            raise ValueError('Non-value columns must constitute a unique ID')
        dim_names = list(dims)
        categories = [{to_int(i):
                           {"label": to_str(i),
                            "category":
                                {"index":
                                     OrderedDict([(to_str(j), to_int(k))
                                                  for k, j in enumerate(
                                                      uniquify(dims[i]))]),
                                 "label":
                                     OrderedDict([(to_str(j), to_str(j))
                                                  for k, j in enumerate(
                                                      uniquify(dims[i]))])}}}
                      for i in dims.columns.values]
        if float(version) >= 2.0:

            dataset = {"dimension": OrderedDict(),
                       value: [None if np.isnan(x) else x
                               for x in dataframe[value].values]}

            dataset["version"] = version
            dataset["class"] = "dataset"
            for category in categories:
                dataset["dimension"].update(category)
            dataset.update({"id": dim_names})
            dataset.update({"size": [len(dims[i].unique())
                                     for i in dims.columns.values]})
            for category in categories:
                dataset["dimension"].update(category)
        else:
            dataset = {"dataset" +
                       str(row + 1):
                       {"dimension": OrderedDict(),
                        value: [None if np.isnan(x) else x
                                for x in dataframe[value].values]}}
            for category in categories:
                dataset["dataset" + str(row + 1)][
                    "dimension"].update(category)
            dataset["dataset" + str(row + 1)][
                "dimension"].update({"id": dim_names})
            dataset["dataset" + str(row + 1)][
                "dimension"].update({"size": [len(dims[i].unique())
                                              for i in dims.columns.values]})
            for category in categories:
                dataset["dataset" + str(row + 1)][
                    "dimension"].update(category)

        if output == 'list':
            result.append(dataset)
        elif output == 'dict':
            result.update(dataset)
        else:
            result = None
    return json.dumps(result, cls=NumpyEncoder)
def to_json_stat ( input_df , value = 'value' , output = 'list' , version = '1.3' ) : warnings . warn ( "Shouldn't use this function anymore! Now use write() methods of" "Dataset, Collection or Dimension." , DeprecationWarning ) data = [ ] if output == 'list' : result = [ ] elif output == 'dict' : result = OrderedDict ( { } ) if isinstance ( input_df , pd . DataFrame ) : data . append ( input_df ) else : data = input_df for row , dataframe in enumerate ( data ) : dims = data [ row ] . filter ( [ item for item in data [ row ] . columns . values if item not in value ] ) if len ( dims . columns . values ) != len ( set ( dims . columns . values ) ) : raise ValueError ( 'Non-value columns must constitute a unique ID' ) dim_names = list ( dims ) categories = [ { to_int ( i ) : { "label" : to_str ( i ) , "category" : { "index" : OrderedDict ( [ ( to_str ( j ) , to_int ( k ) ) for k , j in enumerate ( uniquify ( dims [ i ] ) ) ] ) , "label" : OrderedDict ( [ ( to_str ( j ) , to_str ( j ) ) for k , j in enumerate ( uniquify ( dims [ i ] ) ) ] ) } } } for i in dims . columns . values ] if float ( version ) >= 2.0 : dataset = { "dimension" : OrderedDict ( ) , value : [ None if np . isnan ( x ) else x for x in dataframe [ value ] . values ] } dataset [ "version" ] = version dataset [ "class" ] = "dataset" for category in categories : dataset [ "dimension" ] . update ( category ) dataset . update ( { "id" : dim_names } ) dataset . update ( { "size" : [ len ( dims [ i ] . unique ( ) ) for i in dims . columns . values ] } ) for category in categories : dataset [ "dimension" ] . update ( category ) else : dataset = { "dataset" + str ( row + 1 ) : { "dimension" : OrderedDict ( ) , value : [ None if np . isnan ( x ) else x for x in dataframe [ value ] . values ] } } for category in categories : dataset [ "dataset" + str ( row + 1 ) ] [ "dimension" ] . update ( category ) dataset [ "dataset" + str ( row + 1 ) ] [ "dimension" ] . update ( { "id" : dim_names } ) dataset [ "dataset" + str ( row + 1 ) ] [ "dimension" ] . update ( { "size" : [ len ( dims [ i ] . unique ( ) ) for i in dims . columns . values ] } ) for category in categories : dataset [ "dataset" + str ( row + 1 ) ] [ "dimension" ] . update ( category ) if output == 'list' : result . append ( dataset ) elif output == 'dict' : result . update ( dataset ) else : result = None return json . dumps ( result , cls = NumpyEncoder )
Encode pandas.DataFrame object into JSON-stat format. The DataFrames
       must have exactly one value column.

    Args:
      df(pandas.DataFrame): pandas data frame (or list of data frames) to
      encode.
      value (string, optional): name of the value column. Defaults to 'value'.
      output(string): accepts two values: 'list' or 'dict'. Produce list of\
                      dicts or dict of dicts as output.
      version(string): desired json-stat version. 2.0 is preferred now.\
                       Apart from this, only older 1.3 format is accepted,
                       which is the default parameter in order to preserve
                       backwards compatibility.

    Returns:
      output(string): String with JSON-stat object.