You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: pyretailscience/plots/index.py
+23-21Lines changed: 23 additions & 21 deletions
Original file line number
Diff line number
Diff line change
@@ -53,9 +53,10 @@
53
53
54
54
defplot( # noqa: C901, PLR0913 (ignore complexity and line length)
55
55
df: pd.DataFrame,
56
-
df_index_filter: list[bool],
57
56
value_col: str,
58
57
group_col: str,
58
+
index_col: str,
59
+
value_to_index: str,
59
60
agg_func: str="sum",
60
61
series_col: str|None=None,
61
62
title: str|None=None,
@@ -94,9 +95,10 @@ def plot( # noqa: C901, PLR0913 (ignore complexity and line length)
94
95
95
96
Args:
96
97
df (pd.DataFrame): The dataframe to plot.
97
-
df_index_filter (list[bool]): The filter to apply to the dataframe.
98
98
value_col (str): The column to plot.
99
99
group_col (str): The column to group the data by.
100
+
index_col (str): The column to calculate the index on (e.g., "category").
101
+
value_to_index (str): The baseline category or value to index against (e.g., "A").
100
102
agg_func (str, optional): The aggregation function to apply to the value_col. Defaults to "sum".
101
103
series_col (str, optional): The column to use as the series. Defaults to None.
102
104
title (str, optional): The title of the plot. Defaults to None. When None the title is set to
@@ -136,15 +138,15 @@ def plot( # noqa: C901, PLR0913 (ignore complexity and line length)
136
138
raiseValueError(
137
139
"exclude_groups and include_only_groups cannot be used together.",
138
140
)
139
-
140
141
index_df=get_indexes(
141
142
df=df,
142
-
df_index_filter=df_index_filter,
143
-
index_col=group_col,
143
+
index_col=index_col,
144
+
value_to_index=value_to_index,
144
145
index_subgroup_col=series_col,
145
146
value_col=value_col,
146
147
agg_func=agg_func,
147
148
offset=100,
149
+
group_col=group_col,
148
150
)
149
151
150
152
ifexclude_groupsisnotNone:
@@ -241,44 +243,43 @@ def plot( # noqa: C901, PLR0913 (ignore complexity and line length)
241
243
242
244
defget_indexes(
243
245
df: pd.DataFrame|ibis.Table,
244
-
df_index_filter: list[bool],
246
+
value_to_index: str,
245
247
index_col: str,
246
248
value_col: str,
249
+
group_col: str,
247
250
index_subgroup_col: str|None=None,
248
251
agg_func: str="sum",
249
252
offset: int=0,
250
253
) ->pd.DataFrame:
251
254
"""Calculates the index of the value_col using Ibis for efficient computation at scale.
252
255
253
256
Args:
254
-
df (pd.DataFrame | ibis.Table): The dataframe or Ibis table to calculate the index on.
255
-
df_index_filter (list[bool]): The boolean index to filter the data by.
256
-
index_col (str): The column to calculate the index on.
257
-
value_col (str): The column to calculate the index on.
258
-
index_subgroup_col (str, optional): The column to subgroup the index by. Defaults to None.
259
-
agg_func (str): The aggregation function to apply to the value_col.
260
-
offset (int, optional): The offset to subtract from the index. Defaults to 0.
257
+
df (pd.DataFrame | ibis.Table): The dataframe or Ibis table to calculate the index on. Can be a pandas dataframe or an Ibis table.
258
+
value_to_index (str): The baseline category or value to index against (e.g., "A").
259
+
index_col (str): The column to calculate the index on (e.g., "category").
260
+
value_col (str): The column to calculate the index on (e.g., "sales").
261
+
group_col (str): The column to group the data by (e.g., "region").
262
+
index_subgroup_col (str, optional): The column to subgroup the index by (e.g., "store_type"). Defaults to None.
263
+
agg_func (str, optional): The aggregation function to apply to the `value_col`. Valid options are "sum", "mean", "max", "min", or "nunique". Defaults to "sum".
264
+
offset (int, optional): The offset value to subtract from the index. This allows for adjustments to the index values. Defaults to 0.
261
265
262
266
Returns:
263
267
pd.DataFrame: The calculated index values with grouping columns.
264
268
"""
265
-
ifall(df_index_filter) ornotany(df_index_filter):
266
-
raiseValueError("The df_index_filter cannot be all True or all False.")
0 commit comments