-
Notifications
You must be signed in to change notification settings - Fork 431
Add a new Python module example with parquet files #27084
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
data.parquet | ||
readParquet.good |
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 @@ | ||
../../CLEANFILES |
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 @@ | ||
../../COMPOPTS |
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 @@ | ||
../../EXECENV |
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 @@ | ||
2 |
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,4 @@ | ||
#!/usr/bin/env bash | ||
|
||
FILE_DIR=$(cd $(dirname ${BASH_SOURCE[0]}) ; pwd) | ||
$FILE_DIR/../../checkAndInstallPackage.sh $FILE_DIR pyarrow pandas numpy |
13 changes: 13 additions & 0 deletions
13
test/library/packages/Python/examples/parquet/create_file.py
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,13 @@ | ||
import pandas as pd | ||
import numpy as np | ||
|
||
n = 1000 | ||
data = { | ||
'Integers': np.random.randint(1, 100, size=n), | ||
'Floats1': np.random.uniform(1.0, 100.0, size=n), | ||
'Floats2': np.random.uniform(1.0, 100.0, size=n) | ||
} | ||
df = pd.DataFrame(data) | ||
df.to_parquet("data.parquet", index=False, row_group_size=100) | ||
for c in data.keys(): | ||
print(f"Column: {c} Sum: {round(df[c].sum(), 1)}") |
Empty file.
55 changes: 55 additions & 0 deletions
55
test/library/packages/Python/examples/parquet/readParquet.chpl
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,55 @@ | ||
use Python, List; | ||
|
||
config const filename = "data.parquet"; | ||
|
||
|
||
proc getArray(type eltType, ref data_chunks, num_rows) { | ||
var arr: [0..<num_rows] eltType; | ||
var i = 0; | ||
for chunk in data_chunks { | ||
var chunk_arr = | ||
chunk!.call( | ||
owned PyArray(eltType, 1), | ||
"to_numpy", kwargs=["zero_copy_only"=>false, "writable"=>true]); | ||
arr[i..#chunk_arr.size] = chunk_arr.array(); | ||
i += chunk_arr.size; | ||
} | ||
return arr; | ||
} | ||
|
||
proc main() { | ||
|
||
var interp = new Interpreter(); | ||
|
||
var pa = interp.importModule("pyarrow"); | ||
var pq = interp.importModule("pyarrow.parquet"); | ||
|
||
var parquet_file = pq.call("ParquetFile", filename); | ||
var columns = parquet_file.get("schema").get("names"):list(string); | ||
var num_rows = parquet_file.get("metadata").get("num_rows"):int; | ||
var data_chunks: [0..<columns.size] list(owned Value?); | ||
|
||
for batch in parquet_file.call("iter_batches", kwargs=["batch_size"=>300]) { | ||
for (col, idx) in zip(columns, 0..) { | ||
data_chunks[idx].pushBack(batch.call("__getitem__", col)); | ||
} | ||
} | ||
|
||
var schema_arrow = parquet_file.get('schema_arrow'); | ||
for (col, idx) in zip(columns, 0..) { | ||
write("Column: ", col); | ||
|
||
var rowType = schema_arrow.call('field', col).get('type'); | ||
if pa.call("int64") == rowType { | ||
var arr = getArray(int(64), data_chunks[idx], num_rows); | ||
writeln(" Sum: ", + reduce arr); | ||
} else if pa.call("float64") == rowType { | ||
var arr = getArray(real(64), data_chunks[idx], num_rows); | ||
writeln(" Sum: ", + reduce arr); | ||
} else { | ||
writeln("Unknown type"); | ||
} | ||
} | ||
} | ||
|
||
|
2 changes: 2 additions & 0 deletions
2
test/library/packages/Python/examples/parquet/readParquet.cleanfiles
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,2 @@ | ||
data.parquet | ||
readParquet.good | ||
17 changes: 17 additions & 0 deletions
17
test/library/packages/Python/examples/parquet/readParquet.precomp
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,17 @@ | ||
#!/usr/bin/env bash | ||
|
||
# respect CHPL_TEST_VENV_DIR if it is set and not none | ||
if [ -n "$CHPL_TEST_VENV_DIR" ] && [ "$CHPL_TEST_VENV_DIR" != "none" ]; then | ||
chpl_python=$CHPL_TEST_VENV_DIR/bin/python3 | ||
else | ||
chpl_python=$($CHPL_HOME/util/config/find-python.sh) | ||
fi | ||
|
||
# make sure to add the libs dir to the PYTHONPATH if it exists | ||
FILE_DIR=$(cd $(dirname ${BASH_SOURCE[0]}) ; pwd) | ||
MY_LIB_DIR=$FILE_DIR/python_libs | ||
if [ -d "$MY_LIB_DIR" ]; then | ||
export PYTHONPATH=$MY_LIB_DIR:$PYTHONPATH | ||
fi | ||
|
||
$chpl_python create_file.py > $1.good |
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.
Uh oh!
There was an error while loading. Please reload this page.