/
polars_lazyframe_extensions.py
275 lines (238 loc) · 9.1 KB
/
polars_lazyframe_extensions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import dataclasses
from io import BytesIO
from pathlib import Path
from typing import (
Any,
BinaryIO,
Collection,
Dict,
List,
Mapping,
Optional,
Sequence,
TextIO,
Tuple,
Type,
Union,
)
try:
import polars as pl
from polars import PolarsDataType
except ImportError:
raise NotImplementedError("Polars is not installed.")
# for polars <0.16.0 we need to determine whether type_aliases exist.
has_alias = False
if hasattr(pl, "type_aliases"):
has_alias = True
# for polars 0.18.0 we need to check what to do.
if has_alias and hasattr(pl.type_aliases, "CsvEncoding"):
from polars.type_aliases import CsvEncoding
else:
CsvEncoding = Type
# import these types to make type hinting work
from polars.datatypes import DataType, DataTypeClass # noqa: F401
from hamilton import registry
from hamilton.io import utils
from hamilton.io.data_adapters import DataLoader
DATAFRAME_TYPE = pl.LazyFrame
COLUMN_TYPE = None
COLUMN_FRIENDLY_DF_TYPE = False
def register_types():
"""Function to register the types for this extension."""
registry.register_types("polars_lazyframe", DATAFRAME_TYPE, COLUMN_TYPE)
register_types()
@dataclasses.dataclass
class PolarsScanCSVReader(DataLoader):
"""Class specifically to handle loading CSV files with Polars.
Should map to https://pola-rs.github.io/polars/py-polars/html/reference/api/polars.read_csv.html
"""
file: Union[str, TextIO, BytesIO, Path, BinaryIO, bytes]
# kwargs:
has_header: bool = True
columns: Union[Sequence[int], Sequence[str]] = None
new_columns: Sequence[str] = None
separator: str = ","
comment_char: str = None
quote_char: str = '"'
skip_rows: int = 0
dtypes: Union[Mapping[str, PolarsDataType], Sequence[PolarsDataType]] = None
null_values: Union[str, Sequence[str], Dict[str, str]] = None
missing_utf8_is_empty_string: bool = False
ignore_errors: bool = False
try_parse_dates: bool = False
n_threads: int = None
infer_schema_length: int = 100
batch_size: int = 8192
n_rows: int = None
encoding: Union[CsvEncoding, str] = "utf8"
low_memory: bool = False
rechunk: bool = True
use_pyarrow: bool = False
storage_options: Dict[str, Any] = None
skip_rows_after_header: int = 0
row_count_name: str = None
row_count_offset: int = 0
eol_char: str = "\n"
raise_if_empty: bool = True
def _get_loading_kwargs(self):
kwargs = {}
if self.has_header is not None:
kwargs["has_header"] = self.has_header
if self.columns is not None:
kwargs["columns"] = self.columns
if self.new_columns is not None:
kwargs["new_columns"] = self.new_columns
if self.separator is not None:
kwargs["separator"] = self.separator
if self.comment_char is not None:
kwargs["comment_char"] = self.comment_char
if self.quote_char is not None:
kwargs["quote_char"] = self.quote_char
if self.skip_rows is not None:
kwargs["skip_rows"] = self.skip_rows
if self.dtypes is not None:
kwargs["dtypes"] = self.dtypes
if self.null_values is not None:
kwargs["null_values"] = self.null_values
if self.missing_utf8_is_empty_string is not None:
kwargs["missing_utf8_is_empty_string"] = self.missing_utf8_is_empty_string
if self.ignore_errors is not None:
kwargs["ignore_errors"] = self.ignore_errors
if self.try_parse_dates is not None:
kwargs["try_parse_dates"] = self.try_parse_dates
if self.n_threads is not None:
kwargs["n_threads"] = self.n_threads
if self.infer_schema_length is not None:
kwargs["infer_schema_length"] = self.infer_schema_length
if self.n_rows is not None:
kwargs["n_rows"] = self.n_rows
if self.encoding is not None:
kwargs["encoding"] = self.encoding
if self.low_memory is not None:
kwargs["low_memory"] = self.low_memory
if self.rechunk is not None:
kwargs["rechunk"] = self.rechunk
if self.storage_options is not None:
kwargs["storage_options"] = self.storage_options
if self.skip_rows_after_header is not None:
kwargs["skip_rows_after_header"] = self.skip_rows_after_header
if self.row_count_name is not None:
kwargs["row_count_name"] = self.row_count_name
if self.row_count_offset is not None:
kwargs["row_count_offset"] = self.row_count_offset
if self.eol_char is not None:
kwargs["eol_char"] = self.eol_char
if self.raise_if_empty is not None:
kwargs["raise_if_empty"] = self.raise_if_empty
return kwargs
@classmethod
def applicable_types(cls) -> Collection[Type]:
return [DATAFRAME_TYPE]
def load_data(self, type_: Type) -> Tuple[DATAFRAME_TYPE, Dict[str, Any]]:
df = pl.scan_csv(self.file, **self._get_loading_kwargs())
metadata = utils.get_file_and_dataframe_metadata(self.file, df)
return df, metadata
@classmethod
def name(cls) -> str:
return "csv"
@dataclasses.dataclass
class PolarsScanParquetReader(DataLoader):
"""Class specifically to handle loading parquet files with polars
Should map to https://pola-rs.github.io/polars/py-polars/html/reference/api/polars.read_parquet.html
"""
file: Union[str, TextIO, BytesIO, Path, BinaryIO, bytes]
# kwargs:
columns: Union[List[int], List[str]] = None
n_rows: int = None
use_pyarrow: bool = False
memory_map: bool = True
storage_options: Dict[str, Any] = None
parallel: Any = "auto"
row_count_name: str = None
row_count_offset: int = 0
low_memory: bool = False
use_statistics: bool = True
rechunk: bool = True
@classmethod
def applicable_types(cls) -> Collection[Type]:
return [DATAFRAME_TYPE]
def _get_loading_kwargs(self):
kwargs = {}
if self.columns is not None:
kwargs["columns"] = self.columns
if self.n_rows is not None:
kwargs["n_rows"] = self.n_rows
if self.storage_options is not None:
kwargs["storage_options"] = self.storage_options
if self.parallel is not None:
kwargs["parallel"] = self.parallel
if self.row_count_name is not None:
kwargs["row_count_name"] = self.row_count_name
if self.row_count_offset is not None:
kwargs["row_count_offset"] = self.row_count_offset
if self.low_memory is not None:
kwargs["low_memory"] = self.low_memory
if self.use_statistics is not None:
kwargs["use_statistics"] = self.use_statistics
if self.rechunk is not None:
kwargs["rechunk"] = self.rechunk
return kwargs
def load_data(self, type_: Type) -> Tuple[DATAFRAME_TYPE, Dict[str, Any]]:
df = pl.scan_parquet(self.file, **self._get_loading_kwargs())
metadata = utils.get_file_and_dataframe_metadata(self.file, df)
return df, metadata
@classmethod
def name(cls) -> str:
return "parquet"
@dataclasses.dataclass
class PolarsScanFeatherReader(DataLoader):
"""
Class specifically to handle loading Feather/Arrow IPC files with Polars.
Should map to https://pola-rs.github.io/polars/py-polars/html/reference/api/polars.read_ipc.html
"""
source: Union[str, BinaryIO, BytesIO, Path, bytes]
# kwargs:
columns: Optional[Union[List[str], List[int]]] = None
n_rows: Optional[int] = None
use_pyarrow: bool = False
memory_map: bool = True
storage_options: Optional[Dict[str, Any]] = None
row_count_name: Optional[str] = None
row_count_offset: int = 0
rechunk: bool = True
@classmethod
def applicable_types(cls) -> Collection[Type]:
return [DATAFRAME_TYPE]
def _get_loading_kwargs(self):
kwargs = {}
if self.columns is not None:
kwargs["columns"] = self.columns
if self.n_rows is not None:
kwargs["n_rows"] = self.n_rows
if self.memory_map is not None:
kwargs["memory_map"] = self.memory_map
if self.storage_options is not None:
kwargs["storage_options"] = self.storage_options
if self.row_count_name is not None:
kwargs["row_count_name"] = self.row_count_name
if self.row_count_offset is not None:
kwargs["row_count_offset"] = self.row_count_offset
if self.rechunk is not None:
kwargs["rechunk"] = self.rechunk
return kwargs
def load_data(self, type_: Type) -> Tuple[DATAFRAME_TYPE, Dict[str, Any]]:
df = pl.scan_ipc(self.source, **self._get_loading_kwargs())
metadata = utils.get_file_metadata(self.source)
return df, metadata
@classmethod
def name(cls) -> str:
return "feather"
def register_data_loaders():
"""Function to register the data loaders for this extension."""
for loader in [
PolarsScanCSVReader,
PolarsScanParquetReader,
PolarsScanFeatherReader,
]:
registry.register_adapter(loader)
register_data_loaders()