-
Notifications
You must be signed in to change notification settings - Fork 198
Issue 627 parquet output #683
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
muhammadbadar1998
wants to merge
16
commits into
petl-developers:master
Choose a base branch
from
muhammadbadar1998:issue-627-parquet-output
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
8b302aa
Implement Parquet I/O and add docs/tests (closes #627)
muhammadbadar1998 3ed6af4
docs: add Parquet to supported I/O formats list
muhammadbadar1998 aebd345
docs: add Parquet to supported I/O formats list
muhammadbadar1998 2331fa7
fixed spacing in io.rst
muhammadbadar1998 5bdd2a7
make parquet example self-contained with tempfile
muhammadbadar1998 407c5ed
Remove hard-coded example from parquet.fromparquet docstring
muhammadbadar1998 f592993
fixed panas version
muhammadbadar1998 d6562e9
fixed pyarrow version
muhammadbadar1998 35cbf59
fixed ancii charachter problem in commit
muhammadbadar1998 2a81e48
fixed ancii charachter problem in commit
muhammadbadar1998 03cec66
fixed ancii charachter problem in commit
muhammadbadar1998 20b7e07
updated to use pyarrow and support multiple arrow formats including p…
muhammadbadar1998 7425a60
updated install.rst to refrence corrent io
muhammadbadar1998 e82ca7f
adjusted arrow to work with python 2 and 3
muhammadbadar1998 cfad14b
fixed python 2 logic
muhammadbadar1998 56071d9
fixed arrow test to be compatible with python 2
muhammadbadar1998 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| # -*- coding: utf-8 -*- | ||
| from __future__ import absolute_import, print_function, division | ||
|
|
||
| # internal dependencies | ||
| from petl.util.base import Table, header, data | ||
|
|
||
| __all__ = ( | ||
| 'fromarrow', 'toarrow', | ||
| ) | ||
|
|
||
| def fromarrow(source, **kwargs): | ||
| # Lazy import so module can load on Python 2 | ||
| import pyarrow as pa | ||
| import pyarrow.dataset as ds | ||
| fmt = kwargs.pop('format', 'parquet') | ||
| cols_opt = kwargs.pop('columns', None) | ||
| dataset = ds.dataset(source, format=fmt, **kwargs) | ||
| column_names = [field.name for field in dataset.schema] | ||
|
|
||
| def all_rows(): | ||
| # header row | ||
| yield tuple(column_names) | ||
| # data rows | ||
| for batch in dataset.to_batches(columns=cols_opt): | ||
| for rec in batch.to_pylist(): | ||
| yield tuple(rec.get(c) for c in column_names) | ||
|
|
||
| return Table(all_rows()) | ||
|
|
||
|
|
||
| def toarrow(table, target, **kwargs): | ||
| # Lazy imports so module can load on Python 2 | ||
| import pyarrow as pa | ||
| import pyarrow.parquet as pq | ||
| import pyarrow.dataset as ds | ||
| fmt = kwargs.pop('format', 'parquet') | ||
| schema = kwargs.pop('schema', None) | ||
|
|
||
| hdr = header(table) | ||
| rows = data(table) | ||
|
|
||
| # accumulate data by column | ||
| arrays = {c: [] for c in hdr} | ||
| for row in rows: | ||
| for c, v in zip(hdr, row): | ||
| arrays[c].append(v) | ||
|
|
||
| # build Arrow Table | ||
| arrow_tbl = pa.Table.from_pydict(arrays, schema=schema) | ||
|
|
||
| if fmt == 'parquet': | ||
| # single-file Parquet write | ||
| pq.write_table(arrow_tbl, target, **kwargs) | ||
| else: | ||
| # directory-based dataset write for other formats | ||
| ds.write_dataset(arrow_tbl, target, format=fmt, **kwargs) | ||
|
|
||
| return table | ||
|
|
||
| # attach to Table class | ||
| Table.fromarrow = staticmethod(fromarrow) | ||
| Table.toarrow = staticmethod(toarrow) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| # -*- coding: utf-8 -*- | ||
| from __future__ import absolute_import, print_function, division | ||
|
|
||
| # internal dependencies | ||
| from petl.util.base import Table, header, data # core PETL Table helpers | ||
|
|
||
| __all__ = ( | ||
| 'fromarrow', 'toarrow', | ||
| ) | ||
|
|
||
| def fromarrow(source, **kwargs): | ||
| """ | ||
| Extract data from an Arrow-compatible dataset into a PETL Table. | ||
|
|
||
| :param source: file path, list of paths, or directory | ||
| :param format: dataset format (e.g., 'parquet', 'orc', 'ipc'); default 'parquet' | ||
| :param columns: list of columns to load; default None (all) | ||
| :param kwargs: other keyword arguments passed to pyarrow.dataset.dataset | ||
| :returns: a PETL Table with streaming rows | ||
| """ | ||
| # Lazy imports for PyArrow | ||
| import pyarrow.dataset as ds | ||
| fmt = kwargs.pop('format', 'parquet') | ||
| cols_opt = kwargs.pop('columns', None) | ||
|
|
||
| dataset = ds.dataset(source, format=fmt, **kwargs) | ||
| column_names = [field.name for field in dataset.schema] | ||
|
|
||
| def all_rows(): | ||
| # header row | ||
| yield tuple(column_names) | ||
| # data rows | ||
| for batch in dataset.to_batches(columns=cols_opt): | ||
| for rec in batch.to_pylist(): | ||
| yield tuple(rec.get(c) for c in column_names) | ||
|
|
||
| return Table(all_rows()) | ||
|
|
||
|
|
||
| def toarrow(table, target, **kwargs): | ||
| """ | ||
| Write a PETL Table to an Arrow dataset or file. | ||
|
|
||
| :param table: PETL Table (first row is header) | ||
| :param target: output file path or directory | ||
| :param format: format name (e.g., 'parquet', 'ipc', 'orc'); default 'parquet' | ||
| :param schema: optional pa.Schema; default None (infer schema) | ||
| :param kwargs: passed to writer (pq.write_table or ds.write_dataset) | ||
| :returns: the original PETL Table | ||
| """ | ||
| # Lazy imports for PyArrow | ||
| import pyarrow as pa | ||
| import pyarrow.parquet as pq | ||
| import pyarrow.dataset as ds | ||
|
|
||
| fmt = kwargs.pop('format', 'parquet') | ||
| schema = kwargs.pop('schema', None) | ||
|
|
||
| hdr = header(table) | ||
| rows = data(table) | ||
|
|
||
| # accumulate data by column | ||
| arrays = {c: [] for c in hdr} | ||
| for row in rows: | ||
| for c, v in zip(hdr, row): | ||
| arrays[c].append(v) | ||
|
|
||
| # build Arrow Table | ||
| arrow_tbl = pa.Table.from_pydict(arrays, schema=schema) | ||
|
|
||
| if fmt == 'parquet': | ||
| # single-file Parquet write | ||
| pq.write_table(arrow_tbl, target, **kwargs) | ||
| else: | ||
| # directory-based dataset write for other formats | ||
| ds.write_dataset(arrow_tbl, target, format=fmt, **kwargs) | ||
|
|
||
| return table | ||
|
|
||
| # Attach methods to the PETL Table class | ||
| Table.fromarrow = staticmethod(fromarrow) | ||
| Table.toarrow = staticmethod(toarrow) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,3 +8,7 @@ rinohtype | |
|
|
||
| setuptools | ||
| setuptools-scm | ||
|
|
||
| # add pyarrow dependencies | ||
| pandas | ||
| pyarrow | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ intervaltree>=3.0.2 | |
| lxml>=4.6.5 | ||
| openpyxl>=2.6.2 | ||
| pandas | ||
| pyarrow | ||
| Whoosh>=2.7.4 | ||
| xlrd>=2.0.1 | ||
| xlwt>=1.3.0 | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check warning
Code scanning / Ruff (reported by Codacy)
Module level import not at top of file (E402)