Skip to content
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

Added csv_to_parquet() #88

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ dependencies = [
"dimcli",
"polars>=1.2",
"pyalex",
"more-itertools"
"more-itertools",
"pyarrow"
]

[tool.pytest.ini_options]
Expand Down
22 changes: 21 additions & 1 deletion rialto_airflow/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import csv
import datetime
from pathlib import Path
import re
import sys
from itertools import batched
from pathlib import Path

import pyarrow
from pyarrow.parquet import ParquetWriter


def create_snapshot_dir(data_dir):
Expand Down Expand Up @@ -63,3 +68,18 @@ def normalize_doi(doi):
doi = re.sub("^doi: ", "", doi)

return doi


def csv_to_parquet(csv_file, parquet_file, batch_size=10_000):
csv.field_size_limit(sys.maxsize)

csv_input = open(csv_file)
reader = csv.DictReader(csv_input)

# naively assume all columns are strings
schema = pyarrow.schema([(name, pyarrow.string()) for name in reader.fieldnames])

with ParquetWriter(open(parquet_file, "wb"), schema, compression="zstd") as writer:
for rows in batched(reader, batch_size):
table = pyarrow.Table.from_pylist(rows, schema)
writer.write_table(table)
12 changes: 12 additions & 0 deletions test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from pathlib import Path

import pytest
import polars

from rialto_airflow import utils

Expand Down Expand Up @@ -67,3 +68,14 @@ def test_normalize_doi():
== "10.1103/physrevlett.96.07390"
)
assert utils.normalize_doi(" doi: 10.1234/5678 ") == "10.1234/5678"


def test_csv_to_parquet(tmp_path):
csv_file = Path("test/data/authors.csv")
parquet_file = tmp_path / "authors.parquet"
utils.csv_to_parquet(csv_file, parquet_file)

assert parquet_file.is_file()
df = polars.read_parquet(parquet_file)

assert df.shape == (10, 2)
Loading