Encapsulates details of reading a list of Feather files. Parameters ---------- path_or_paths : List[str] A list of file names validate_schema : bool, default True Check that individual file schemas are all the same / compatible
| 28 | |
| 29 | |
| 30 | class FeatherDataset: |
| 31 | """ |
| 32 | Encapsulates details of reading a list of Feather files. |
| 33 | |
| 34 | Parameters |
| 35 | ---------- |
| 36 | path_or_paths : List[str] |
| 37 | A list of file names |
| 38 | validate_schema : bool, default True |
| 39 | Check that individual file schemas are all the same / compatible |
| 40 | """ |
| 41 | |
| 42 | def __init__(self, path_or_paths, validate_schema=True): |
| 43 | self.paths = path_or_paths |
| 44 | self.validate_schema = validate_schema |
| 45 | |
| 46 | def read_table(self, columns=None): |
| 47 | """ |
| 48 | Read multiple feather files as a single pyarrow.Table |
| 49 | |
| 50 | Parameters |
| 51 | ---------- |
| 52 | columns : List[str] |
| 53 | Names of columns to read from the file |
| 54 | |
| 55 | Returns |
| 56 | ------- |
| 57 | pyarrow.Table |
| 58 | Content of the file as a table (of columns) |
| 59 | """ |
| 60 | _fil = read_table(self.paths[0], columns=columns) |
| 61 | self._tables = [_fil] |
| 62 | self.schema = _fil.schema |
| 63 | |
| 64 | for path in self.paths[1:]: |
| 65 | table = read_table(path, columns=columns) |
| 66 | if self.validate_schema: |
| 67 | self.validate_schemas(path, table) |
| 68 | self._tables.append(table) |
| 69 | return concat_tables(self._tables) |
| 70 | |
| 71 | def validate_schemas(self, piece, table): |
| 72 | if not self.schema.equals(table.schema): |
| 73 | raise ValueError(f'Schema in {piece} was different. \n' |
| 74 | f'{self.schema}\n\nvs\n\n{table.schema}') |
| 75 | |
| 76 | def read_pandas(self, columns=None, use_threads=True): |
| 77 | """ |
| 78 | Read multiple Parquet files as a single pandas DataFrame |
| 79 | |
| 80 | Parameters |
| 81 | ---------- |
| 82 | columns : List[str] |
| 83 | Names of columns to read from the file |
| 84 | use_threads : bool, default True |
| 85 | Use multiple threads when converting to pandas |
| 86 | |
| 87 | Returns |