def test_split(self): data = ListOfDicts({ "x": x, "y": y } for x, y in zip( [1, 2, 2, 3, 3, 3], [1, 1, 1, 1, 1, 2], )) rows = data.split("x", "y") assert rows == [[0], [1, 2], [3, 4], [5]]
def test_read_csv_types(self): path = str(test.get_data_path("vehicles.csv")) types = {"hwy": int, "cty": int} data = ListOfDicts.read_csv(path, types=types) assert isinstance(data[0].hwy, int) assert isinstance(data[0].cty, int) assert isinstance(data[100].hwy, int) assert isinstance(data[100].cty, int)
def test_read_json_types(self): path = str(test.get_data_path("downloads.json")) # datetime.date.fromisoformat requires Python >= 3.7. date = lambda x: datetime.date(*map(int, x.split("-"))) types = {"date": date, "downloads": int} data = ListOfDicts.read_json(path, types=types) assert isinstance(data[0].date, datetime.date) assert isinstance(data[0].downloads, int) assert isinstance(data[100].date, datetime.date) assert isinstance(data[100].downloads, int)
def to_list_of_dicts(self): """ Return data frame converted to a :class:`.ListOfDicts`. >>> data = di.read_csv("data/listings.csv") >>> data.to_list_of_dicts() """ from dataiter import ListOfDicts data = [{} for i in range(self.nrow)] for colname in self.colnames: for i, value in enumerate(self[colname].tolist()): data[i][colname] = value return ListOfDicts(data)
def test_write_pickle(self): orig = test.list_of_dicts("downloads.json") handle, path = tempfile.mkstemp(".pkl") orig.write_pickle(path) data = ListOfDicts.read_pickle(path) assert data == orig
def read_json(path, *, encoding="utf-8", keys=[], types={}, **kwargs): return ListOfDicts.read_json(path, encoding=encoding, keys=keys, types=types, **kwargs)
def test_to_json(self): orig = test.list_of_dicts("downloads.json") text = orig.to_json() data = ListOfDicts.from_json(text) assert data == orig
def test_write_csv(self): orig = test.list_of_dicts("vehicles.csv") handle, path = tempfile.mkstemp(".csv") orig.write_csv(path) data = ListOfDicts.read_csv(path) assert data == orig
def test___init___empty(self): data = ListOfDicts() assert len(data) == 0
def test_read_csv_keys(self): path = str(test.get_data_path("vehicles.csv")) data = ListOfDicts.read_csv(path, keys=["make", "model"]) assert list(data[0].keys()) == ["make", "model"] assert list(data[100].keys()) == ["make", "model"]
def test___init__(self): item = dict(a=1, b=2, c=3) data = ListOfDicts([item]) assert len(data) == 1 assert data[0] == item assert data[0] is not item
def test_read_pickle_path(self): orig = test.list_of_dicts("downloads.json") handle, path = tempfile.mkstemp(".pkl") orig.write_pickle(path) ListOfDicts.read_pickle(Path(path))
def test_read_csv(self): path = str(test.get_data_path("vehicles.csv")) data = ListOfDicts.read_csv(path) assert len(data) == 33442 assert all(len(x) == 12 for x in data)
def test_read_json_path(self): ListOfDicts.read_csv(test.get_data_path("vehicles.json"))
def test_read_json_keys(self): path = str(test.get_data_path("downloads.json")) data = ListOfDicts.read_json(path, keys=["date", "downloads"]) assert list(data[0].keys()) == ["date", "downloads"] assert list(data[100].keys()) == ["date", "downloads"]
def test_read_json(self): path = str(test.get_data_path("downloads.json")) data = ListOfDicts.read_json(path) assert len(data) == 905 assert all(len(x) == 3 for x in data)
def test_read_csv_path(self): ListOfDicts.read_csv(test.get_data_path("vehicles.csv"))