-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
130 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
Empty file.
This file contains 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,72 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Process the stuff in duckdb | ||
Resources: | ||
1. DuckDB documentation: ... | ||
""" | ||
|
||
import argparse | ||
import time | ||
|
||
import duckdb | ||
import pandas as pd | ||
|
||
from src.process_data.base import BaseProcessDataInterface | ||
|
||
|
||
class DuckDBInterface(BaseProcessDataInterface): | ||
""" | ||
Interface for executing duckDB transformations. | ||
Use as follows: | ||
>> df = DuckDBInterface.in_memory("foo.txt") | ||
""" | ||
|
||
@classmethod | ||
def in_memory(cls, filename: str) -> pd.DataFrame: | ||
""" | ||
Process the whole thing in memory | ||
Args: | ||
filename: Name of the file. Should be relative path to the location where | ||
this script is invoked. | ||
""" | ||
|
||
with duckdb.connect() as conn: | ||
data = conn.sql(f""" | ||
select | ||
station_name, | ||
min(measurement) as min_measurement, | ||
cast(avg(measurement) as decimal(8, 1)) as mean_measurement, | ||
max(measurement) as max_measurement | ||
from read_csv( | ||
"{filename}", | ||
header=false, | ||
columns={{'station_name': 'varchar', 'measurement': 'decimal(8, 1)'}}, | ||
delim=';', | ||
parallel=true | ||
) | ||
group by station_name | ||
order by station_name | ||
""") | ||
|
||
return data.df() | ||
|
||
@classmethod | ||
def streaming(cls, filename: str): | ||
raise NotImplementedError | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser(description="Analyze measurement file") | ||
parser.add_argument("-f", "--file_name", dest="file_name", type=str, help="File name", default="measurements.txt") | ||
args = parser.parse_args() | ||
|
||
start = time.time() | ||
df = DuckDBInterface().in_memory(args.file_name) | ||
|
||
duration = time.time() - start | ||
print(f"Duration = {duration: .2f}s") | ||
print(df.query("station_name == 'Alexandria'").head()) |