-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
129 lines (100 loc) · 4.55 KB
/
Copy pathutils.py
File metadata and controls
129 lines (100 loc) · 4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import pandas as pd
from typing import List, Any, Dict, Optional
class DataFrameGenerator:
"""A utility class for building pandas DataFrames from column data."""
def __init__(self):
"""Initialize an empty DataFrameGenerator."""
self.data: Dict[str, List[Any]] = {}
def add_data(self, column_name: str, values: List[Any]) -> None:
"""Add data to a specific column.
Args:
column_name: Name of the column to add data to
values: List of values to add to the column
"""
if not isinstance(column_name, str):
raise ValueError("column_name must be a string")
if not isinstance(values, list):
raise ValueError("values must be a list")
if column_name not in self.data:
self.data[column_name] = []
self.data[column_name].extend(values)
def add_single_value(self, column_name: str, value: Any) -> None:
"""Add a single value to a specific column.
Args:
column_name: Name of the column to add data to
value: Single value to add to the column
"""
self.add_data(column_name, [value])
def get_column_lengths(self) -> Dict[str, int]:
"""Get the length of each column.
Returns:
Dictionary mapping column names to their lengths
"""
return {col: len(values) for col, values in self.data.items()}
def is_balanced(self) -> bool:
"""Check if all columns have the same length.
Returns:
True if all columns have the same length, False otherwise
"""
if not self.data:
return True
lengths = set(len(col) for col in self.data.values())
return len(lengths) == 1
def to_dataframe(self, auto_balance: bool = True) -> pd.DataFrame:
"""Convert the stored data to a pandas DataFrame.
Args:
auto_balance: If True, automatically trim columns to the minimum length.
If False, raise an error if columns have different lengths.
Returns:
pandas DataFrame with the stored data
Raises:
ValueError: If auto_balance is False and columns have different lengths
"""
if not self.data:
return pd.DataFrame()
if not auto_balance and not self.is_balanced():
lengths = self.get_column_lengths()
raise ValueError(f"Columns have different lengths: {lengths}")
# Find the minimum length among all columns
min_len = min(len(col) for col in self.data.values())
# Trim each column to the minimum length
trimmed_data = {k: v[:min_len] for k, v in self.data.items()}
return pd.DataFrame(trimmed_data)
def clear(self) -> None:
"""Clear all stored data."""
self.data.clear()
def get_column_names(self) -> List[str]:
"""Get the names of all columns.
Returns:
List of column names
"""
return list(self.data.keys())
def has_column(self, column_name: str) -> bool:
"""Check if a column exists.
Args:
column_name: Name of the column to check
Returns:
True if the column exists, False otherwise
"""
return column_name in self.data
def merge(self, other_dataframe_generator: "DataFrameGenerator"):
"""Merge the stored data with another DataFrameGenerator.
Args:
other_dataframe_generator: Another DataFrameGenerator to merge with
Returns:
Merged DataFrameGenerator
"""
if not isinstance(other_dataframe_generator, DataFrameGenerator):
raise ValueError("other_dataframe_generator must be a DataFrameGenerator")
# Check if this DataFrameGenerator is empty
if not self.data:
self.data = other_dataframe_generator.data
return
# Check if the other DataFrameGenerator has the same column names
if not set(self.get_column_names()) == set(other_dataframe_generator.get_column_names()):
print("The two DataFrameGenerators have different column names")
return
# raise ValueError("The two DataFrameGenerators have different column names")
# Merge the data
for column_name in other_dataframe_generator.get_column_names():
self.add_data(column_name, other_dataframe_generator.data[column_name])