Skip to content

Commit e70d844

Browse files
committed
chore: add unit tests
1 parent 42a1b61 commit e70d844

File tree

5 files changed

+376
-347
lines changed

5 files changed

+376
-347
lines changed

pyretailscience/plots/histogram.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,14 @@ def plot(
9696
if isinstance(df, pd.Series):
9797
df = df.to_frame(name=value_col[0])
9898

99-
df = apply_range_clipping(
100-
df=df,
101-
value_col=value_col,
102-
range_lower=range_lower,
103-
range_upper=range_upper,
104-
range_method=range_method,
105-
)
99+
if range_lower is not None and range_upper is not None:
100+
df = _apply_range_clipping(
101+
df=df,
102+
value_col=value_col,
103+
range_lower=range_lower,
104+
range_upper=range_upper,
105+
range_method=range_method,
106+
)
106107

107108
num_histograms = _get_num_histograms(df=df, value_col=value_col, group_col=group_col)
108109

@@ -159,28 +160,25 @@ def _prepare_value_col(df: pd.DataFrame | pd.Series, value_col: str | list[str]
159160
return value_col
160161

161162

162-
def apply_range_clipping(
163+
def _apply_range_clipping(
163164
df: pd.DataFrame,
164165
value_col: list[str],
165-
range_lower: float | None,
166-
range_upper: float | None,
166+
range_lower: float,
167+
range_upper: float,
167168
range_method: Literal["clip", "fillna"],
168169
) -> pd.DataFrame:
169170
"""Applies range clipping or filling based on the provided method and returns the modified dataframe.
170171
171172
Args:
172173
df (pd.DataFrame): The dataframe to apply range clipping to.
173174
value_col (list of str): The column(s) to apply clipping or filling to.
174-
range_lower (float, optional): Lower bound for clipping or filling NA values.
175-
range_upper (float, optional): Upper bound for clipping or filling NA values.
175+
range_lower (float): Lower bound for clipping or filling NA values.
176+
range_upper (float): Upper bound for clipping or filling NA values.
176177
range_method (Literal, optional): Whether to "clip" values outside the range or "fillna". Defaults to "clip".
177178
178179
Returns:
179180
pd.DataFrame: The modified dataframe with the clipping or filling applied.
180181
"""
181-
if range_lower is None and range_upper is None:
182-
return df
183-
184182
if range_method == "clip":
185183
return df.assign(**{col: df[col].clip(lower=range_lower, upper=range_upper) for col in value_col})
186184

pyretailscience/plots/line.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ def plot(
9090
)
9191

9292
if group_col is None:
93-
pivot_df = df.set_index(x_col if x_col is not None else df.index)[value_col]
93+
pivot_df = df.set_index(x_col if x_col is not None else df.index)[
94+
[value_col] if isinstance(value_col, str) else value_col
95+
]
9496
else:
9597
pivot_df = df.pivot(index=x_col if x_col is not None else None, columns=group_col, values=value_col)
9698

0 commit comments

Comments
 (0)