def testArrayNumpyLabelled(self): input = {"a": []} output = ujson.loads(ujson.dumps(input), numpy=True, labelled=True) self.assertTrue((np.empty((1, 0)) == output[0]).all()) self.assertTrue((np.array(["a"]) == output[1]).all()) self.assertTrue(output[2] is None) input = [{"a": 42}] output = ujson.loads(ujson.dumps(input), numpy=True, labelled=True) self.assertTrue((np.array([42]) == output[0]).all()) self.assertTrue(output[1] is None) self.assertTrue((np.array([u"a"]) == output[2]).all()) input = [{"a": 42, "b": 31}, {"a": 24, "c": 99}, {"a": 2.4, "b": 78}] output = ujson.loads(ujson.dumps(input), numpy=True, labelled=True) expectedvals = np.array([42, 31, 24, 99, 2.4, 78], dtype=int).reshape((3, 2)) self.assertTrue((expectedvals == output[0]).all()) self.assertTrue(output[1] is None) self.assertTrue((np.array([u"a", "b"]) == output[2]).all()) input = {1: {"a": 42, "b": 31}, 2: {"a": 24, "c": 99}, 3: {"a": 2.4, "b": 78}} output = ujson.loads(ujson.dumps(input), numpy=True, labelled=True) expectedvals = np.array([42, 31, 24, 99, 2.4, 78], dtype=int).reshape((3, 2)) self.assertTrue((expectedvals == output[0]).all()) self.assertTrue((np.array(["1", "2", "3"]) == output[1]).all()) self.assertTrue((np.array(["a", "b"]) == output[2]).all())
def testArrayNumpyLabelled(self): input = {'a': []} output = ujson.loads(ujson.dumps(input), numpy=True, labelled=True) self.assertTrue((np.empty((1, 0)) == output[0]).all()) self.assertTrue((np.array(['a']) == output[1]).all()) self.assertTrue(output[2] is None) input = [{'a': 42}] output = ujson.loads(ujson.dumps(input), numpy=True, labelled=True) self.assertTrue((np.array([42]) == output[0]).all()) self.assertTrue(output[1] is None) self.assertTrue((np.array([u'a']) == output[2]).all()) input = [{'a': 42, 'b':31}, {'a': 24, 'c': 99}, {'a': 2.4, 'b': 78}] output = ujson.loads(ujson.dumps(input), numpy=True, labelled=True) expectedvals = np.array([42, 31, 24, 99, 2.4, 78], dtype=int).reshape((3,2)) self.assertTrue((expectedvals == output[0]).all()) self.assertTrue(output[1] is None) self.assertTrue((np.array([u'a', 'b']) == output[2]).all()) input = {1: {'a': 42, 'b':31}, 2: {'a': 24, 'c': 99}, 3: {'a': 2.4, 'b': 78}} output = ujson.loads(ujson.dumps(input), numpy=True, labelled=True) expectedvals = np.array([42, 31, 24, 99, 2.4, 78], dtype=int).reshape((3,2)) self.assertTrue((expectedvals == output[0]).all()) self.assertTrue((np.array(['1','2','3']) == output[1]).all()) self.assertTrue((np.array(['a', 'b']) == output[2]).all())
def from_json(cls, json, orient="columns", dtype=None, numpy=True): """ Convert JSON string to DataFrame Parameters ---------- json : The JSON string to parse. orient : {'split', 'records', 'index', 'columns', 'values'}, default 'columns' The format of the JSON string split : dict like {index -> [index], columns -> [columns], data -> [values]} records : list like [{column -> value}, ... , {column -> value}] index : dict like {index -> {column -> value}} columns : dict like {column -> {index -> value}} values : just the values array dtype : dtype of the resulting DataFrame nupmpy: direct decoding to numpy arrays. default True but falls back to standard decoding if a problem occurs. Returns ------- result : DataFrame """ df = None if dtype is not None and orient == "split": numpy = False if numpy: try: if orient == "columns": args = loads(json, dtype=dtype, numpy=True, labelled=True) if args: args = (args[0].T, args[2], args[1]) df = DataFrame(*args) elif orient == "split": decoded = loads(json, dtype=dtype, numpy=True) decoded = dict((str(k), v) for k, v in decoded.iteritems()) df = DataFrame(**decoded) elif orient == "values": df = DataFrame(loads(json, dtype=dtype, numpy=True)) else: df = DataFrame(*loads(json, dtype=dtype, numpy=True, labelled=True)) except ValueError: numpy = False if not numpy: if orient == "columns": df = DataFrame(loads(json), dtype=dtype) elif orient == "split": decoded = dict((str(k), v) for k, v in loads(json).iteritems()) df = DataFrame(dtype=dtype, **decoded) elif orient == "index": df = DataFrame(loads(json), dtype=dtype).T else: df = DataFrame(loads(json), dtype=dtype) return df
def from_json(cls, json, orient="columns", dtype=None, numpy=True): """ Convert JSON string to DataFrame Parameters ---------- json : The JSON string to parse. orient : {'split', 'records', 'index', 'columns', 'values'}, default 'columns' The format of the JSON string split : dict like {index -> [index], columns -> [columns], data -> [values]} records : list like [{column -> value}, ... , {column -> value}] index : dict like {index -> {column -> value}} columns : dict like {column -> {index -> value}} values : just the values array dtype : dtype of the resulting DataFrame nupmpy: direct decoding to numpy arrays. default True but falls back to standard decoding if a problem occurs. Returns ------- result : DataFrame """ df = None if dtype is not None and orient == "split": numpy = False if numpy: try: if orient == "columns": args = loads(json, dtype=dtype, numpy=True, labelled=True) if args: args = (args[0].T, args[2], args[1]) df = DataFrame(*args) elif orient == "split": decoded = loads(json, dtype=dtype, numpy=True) decoded = dict((str(k), v) for k, v in decoded.iteritems()) df = DataFrame(**decoded) elif orient == "values": df = DataFrame(loads(json, dtype=dtype, numpy=True)) else: df = DataFrame( *loads(json, dtype=dtype, numpy=True, labelled=True)) except ValueError: numpy = False if not numpy: if orient == "columns": df = DataFrame(loads(json), dtype=dtype) elif orient == "split": decoded = dict((str(k), v) for k, v in loads(json).iteritems()) df = DataFrame(dtype=dtype, **decoded) elif orient == "index": df = DataFrame(loads(json), dtype=dtype).T else: df = DataFrame(loads(json), dtype=dtype) return df
def from_json(cls, json, orient="index", dtype=None, numpy=True): """ Convert JSON string to Series Parameters ---------- json : The JSON string to parse. orient : {'split', 'records', 'index'}, default 'index' The format of the JSON string split : dict like {index -> [index], name -> name, data -> [values]} records : list like [value, ... , value] index : dict like {index -> value} dtype : dtype of the resulting Series nupmpy: direct decoding to numpy arrays. default True but falls back to standard decoding if a problem occurs. Returns ------- result : Series """ s = None if dtype is not None and orient == "split": numpy = False if numpy: try: if orient == "split": decoded = loads(json, dtype=dtype, numpy=True) decoded = dict((str(k), v) for k, v in decoded.iteritems()) s = Series(**decoded) elif orient == "columns" or orient == "index": s = Series(*loads(json, dtype=dtype, numpy=True, labelled=True)) else: s = Series(loads(json, dtype=dtype, numpy=True)) except ValueError: numpy = False if not numpy: if orient == "split": decoded = dict((str(k), v) for k, v in loads(json).iteritems()) s = Series(dtype=dtype, **decoded) else: s = Series(loads(json), dtype=dtype) return s
def from_json(cls, json, orient="index", dtype=None, numpy=True): """ Convert JSON string to Series Parameters ---------- json : The JSON string to parse. orient : {'split', 'records', 'index'}, default 'index' The format of the JSON string split : dict like {index -> [index], name -> name, data -> [values]} records : list like [value, ... , value] index : dict like {index -> value} dtype : dtype of the resulting Series nupmpy: direct decoding to numpy arrays. default True but falls back to standard decoding if a problem occurs. Returns ------- result : Series """ s = None if dtype is not None and orient == "split": numpy = False if numpy: try: if orient == "split": decoded = loads(json, dtype=dtype, numpy=True) decoded = dict((str(k), v) for k, v in decoded.iteritems()) s = Series(**decoded) elif orient == "columns" or orient == "index": s = Series( *loads(json, dtype=dtype, numpy=True, labelled=True)) else: s = Series(loads(json, dtype=dtype, numpy=True)) except ValueError: numpy = False if not numpy: if orient == "split": decoded = dict((str(k), v) for k, v in loads(json).iteritems()) s = Series(dtype=dtype, **decoded) else: s = Series(loads(json), dtype=dtype) return s
def on_message(self, message): request = json.loads(message) handler = self._get_handler(request) frame = SOURCES[request['source']] resp = handler(request, frame) return self._respond(resp, request['handler'])