Skip to content

Commit

Permalink
refactor: Rewrite pd_ellipse
Browse files Browse the repository at this point in the history
- Fixed a type ignore (causes by incomplete stubs)
- Renamed variables
- Make replace the implicit `"index"` column with naming it `"order"`

#3747 (comment)
  • Loading branch information
dangotbanned committed Jan 10, 2025
1 parent 182baea commit fa280f1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
21 changes: 10 additions & 11 deletions tests/examples_arguments_syntax/deviation_ellipses.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,15 @@ def np_ellipse(
def pd_ellipse(
df: pd.DataFrame, col_x: str, col_y: str, col_group: str
) -> pd.DataFrame:
cols = col_x, col_y
groups = []
# TODO: Rewrite in a more readable way
categories = df[col_group].unique()
for category in categories:
sliced = df.loc[df[col_group] == category, cols]
ell_df = pd.DataFrame(np_ellipse(sliced.to_numpy()), columns=cols) # type: ignore
ell_df[col_group] = category
groups.append(ell_df)
return pd.concat(groups).reset_index()
cols = [col_x, col_y]
ellipses = []
ser: pd.Series[float] = df[col_group]
for group in ser.drop_duplicates():
arr = df.loc[ser == group, cols].to_numpy()
ellipse = pd.DataFrame(np_ellipse(arr), columns=cols)
ellipse[col_group] = group
ellipses.append(ellipse)
return pd.concat(ellipses).reset_index(names="order")


col_x = "petalLength"
Expand All @@ -84,7 +83,7 @@ def pd_ellipse(
lines = (
alt.Chart(ellipse)
.mark_line(filled=True, fillOpacity=0.2)
.encode(x, y, color, order="index")
.encode(x, y, color, order="order")
)

chart = (lines + points).properties(height=500, width=500)
Expand Down
20 changes: 10 additions & 10 deletions tests/examples_methods_syntax/deviation_ellipses.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ def np_ellipse(
def pd_ellipse(
df: pd.DataFrame, col_x: str, col_y: str, col_group: str
) -> pd.DataFrame:
cols = col_x, col_y
groups = []
categories = df[col_group].unique()
for category in categories:
sliced = df.loc[df[col_group] == category, cols]
ell_df = pd.DataFrame(np_ellipse(sliced.to_numpy()), columns=cols) # type: ignore
ell_df[col_group] = category
groups.append(ell_df)
return pd.concat(groups).reset_index()
cols = [col_x, col_y]
ellipses = []
ser: pd.Series[float] = df[col_group]
for group in ser.drop_duplicates():
arr = df.loc[ser == group, cols].to_numpy()
ellipse = pd.DataFrame(np_ellipse(arr), columns=cols)
ellipse[col_group] = group
ellipses.append(ellipse)
return pd.concat(ellipses).reset_index(names="order")


col_x = "petalLength"
Expand All @@ -83,7 +83,7 @@ def pd_ellipse(
lines = (
alt.Chart(ellipse)
.mark_line(filled=True, fillOpacity=0.2)
.encode(x, y, color, order="index")
.encode(x, y, color, order="order")
)

chart = (lines + points).properties(height=500, width=500)
Expand Down

0 comments on commit fa280f1

Please sign in to comment.