parquet_editor module¶
- class ParquetEditor(statistics_name, id_vars, data_source, data_period, varselector_filtering=False, output=None, output_varselector_name=None, allow_risky_column_names=False, height=None, **kwargs)¶
Bases:
ModuleBaseSimple module with the sole purpose of editing a parquet file.
Accomplishes this functionality by writing a processlog in a json lines file and recording any edits in this jsonl file.
Example
>>> id_variabler = ["orgnr", "aar", "kvartal"] >>> my_parquet_editor = ParquetEditor( statistics_name="Demo", data_period="2024", id_vars=id_variabler, data_source="/buckets/produkt/editering-eksempel/inndata/test_p2024_v1.parquet", )
Notes
The process log is automatically created in the correct folder structure and is named after your parquet file.
- Parameters:
statistics_name (str)
id_vars (list[str])
data_source (str)
data_period (str)
varselector_filtering (bool)
output (str | list[str] | None)
output_varselector_name (str | list[str] | None)
allow_risky_column_names (bool)
height (str | None)
- get_data()¶
Reads the parquet file at the supplied file path.
- Return type:
DataFrame
- icon: str | Component¶
- implemented_as: Optional[Literal['Tab', 'Window']]¶
- label: str¶
- layout()¶
Create the layout of the module itself, without tab/window wrapping.
- Return type:
Div
- module_callbacks()¶
Sets up the callbacks for the module.
- Return type:
None
- module_id: str¶
- module_layout: Div | Tab¶
- module_name: str¶
- module_number: int¶
- class ParquetEditorChangelog(id_vars, data_source)¶
Bases:
ModuleBaseSimple module with the sole purpose of showing the changes made using ParquetEditor.
Notes
The process log is automatically created in the correct folder structure and is named after your parquet file.
- Parameters:
id_vars (list[str])
data_source (str)
- icon: str | Component¶
- implemented_as: Optional[Literal['Tab', 'Window']]¶
- label: str¶
- layout()¶
Create the layout of the module itself, without tab/window wrapping.
- Return type:
Textarea
- module_callbacks()¶
Sets up the callbacks for the module.
- Return type:
None
- module_id: str¶
- module_layout: Div | Tab¶
- module_name: str¶
- module_number: int¶
- apply_edits(parquet_path, allow_risky_column_names=False)¶
Applies edits from the jsonl log to a parquet file.
- Parameters:
parquet_path (
str|Path) – The file path for the parquet file.allow_risky_column_names (
bool) – Controls whether or not the function allows potentially bug-inducing column names. Defaults to False.
- Return type:
DataFrame- Returns:
A pd.DataFrame with updated data.
- check_for_bucket_path(path)¶
Temporary check to make sure users keep to using ‘/buckets/’ paths.
Need to test more with UPath to make sure nothing unexpected happens.
- Parameters:
path (
str|Path) – The path to check.- Raises:
NotImplementedError – If path doesn’t start with ‘/buckets/’
- Return type:
None
- export_from_parqueteditor(data_source, data_target, force_overwrite=False, allow_risky_column_names=False)¶
Export edited data from parquet editor.
Reads the jsonl log, updates data_target from placeholder to the supplied value, and saves the updated log next to the exported parquet file. Also applies edits and exports the data.
- Parameters:
data_source (
str) – Path to the source parquet filedata_target (
str) – Path where the exported file will be writtenforce_overwrite (
bool) – If True, overwrites existing parquet and jsonl files when exporting. Defaults to False.allow_risky_column_names (
bool) – Controls whether or not the function allows potentially bug-inducing column names. Defaults to False.
- Raises:
FileNotFoundError – if no log file is found.
FileExistsError – If any of the files to export already exists and force_overwrite is False.
- Return type:
None
- get_export_log_path(target_path)¶
Derive the correct path to save the exported log file to.
- Parameters:
target_path (
Path) – The path where the exported data is to be written to.- Return type:
Path- Returns:
The correct path to save the processlog.
- Raises:
ValueError – If a valid data state is not found in the path.
- get_log_path(parquet_path)¶
Return the expected log file path (.jsonl) for a given parquet file.
The function searches for known data-state subfolders in the parquet path (/inndata/, /klargjorte-data/, /statistikk/, /utdata/) and rewrites the path to the corresponding temp folder under /<state>/temp/parqueteditor/. If none match, the log file is assumed to be in the same directory as the parquet file.
- Return type:
Path- Parameters:
parquet_path (str | Path)
- log_as_text(file_path)¶
Convert a JSONL string of change logs into a human-readable text format.
Returns a single string.
- Return type:
str- Parameters:
file_path (str | Path)
- read_jsonl_file_to_string(file_path)¶
Reads a JSONL file and returns its contents as a single string.
If the file does not exists an empty string is returned.
- Return type:
str- Parameters:
file_path (str | Path)
- read_jsonl_log(path)¶
Reads the jsonl log.
- Parameters:
path (
str|Path) – The path that leads to the jsonl log.- Return type:
list[Any]- Returns:
A list where each instance is a line in the jsonl file.