Esempio n. 1
0
 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]]
Esempio n. 2
0
 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)
Esempio n. 3
0
 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)
Esempio n. 4
0
    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)
Esempio n. 5
0
 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
Esempio n. 6
0
def read_json(path, *, encoding="utf-8", keys=[], types={}, **kwargs):
    return ListOfDicts.read_json(path,
                                 encoding=encoding,
                                 keys=keys,
                                 types=types,
                                 **kwargs)
Esempio n. 7
0
 def test_to_json(self):
     orig = test.list_of_dicts("downloads.json")
     text = orig.to_json()
     data = ListOfDicts.from_json(text)
     assert data == orig
Esempio n. 8
0
 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
Esempio n. 9
0
 def test___init___empty(self):
     data = ListOfDicts()
     assert len(data) == 0
Esempio n. 10
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"]
Esempio n. 11
0
 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
Esempio n. 12
0
 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))
Esempio n. 13
0
 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)
Esempio n. 14
0
 def test_read_json_path(self):
     ListOfDicts.read_csv(test.get_data_path("vehicles.json"))
Esempio n. 15
0
 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"]
Esempio n. 16
0
 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)
Esempio n. 17
0
 def test_read_csv_path(self):
     ListOfDicts.read_csv(test.get_data_path("vehicles.csv"))