Skip to content

[TST] Fix test failures for pivot_longer #1155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
- [BUG] Force `math.softmax` returning `Series`. PR #1139 @Zeroto521
- [INF] Set independent environment for building documentation. PR #1141 @Zeroto521
- [DOC] Add local documentation preview via github action artifact. PR #1149 @Zeroto521
- [ENH] Enabel `encode_categorical` handle 2 (or more ) dimensions array. PR #1153 @Zeroto521
- [ENH] Enable `encode_categorical` handle 2 (or more ) dimensions array. PR #1153 @Zeroto521

## [v0.23.1] - 2022-05-03

Expand Down
30 changes: 16 additions & 14 deletions tests/functions/test_pivot_longer.py
Original file line number Diff line number Diff line change
Expand Up @@ -663,8 +663,9 @@ def test_names_pattern_str(test_df):
column_names="*_*",
names_to=["set", ".value"],
names_pattern="(.+)_(.+)",
sort_by_appearance=True,
sort_by_appearance=False,
)
result = result.sort_values(result.columns.tolist(), ignore_index=True)

actual = test_df.copy()
actual.columns = actual.columns.str.split("_").str[::-1].str.join("_")
Expand All @@ -680,6 +681,7 @@ def test_names_pattern_str(test_df):
.reset_index("set")
.reset_index(drop=True)
)
actual = actual.sort_values(actual.columns.tolist(), ignore_index=True)

assert_frame_equal(result, actual)

Expand All @@ -706,6 +708,9 @@ def test_names_sep(test_df):
.reset_index(drop=True)
)

result = result.sort_values(result.columns.tolist(), ignore_index=True)
actual = actual.sort_values(actual.columns.tolist(), ignore_index=True)

assert_frame_equal(result, actual)


Expand Down Expand Up @@ -1188,18 +1193,12 @@ def test_names_transform_numeric():
}
)

expected = (
df.pivot_longer(
index="A",
names_to=(".value", "colname"),
names_sep="_",
names_transform=float,
)
.sort_values(
["A", "colname", "result", "treatment"], ignore_index=True
)
.loc[:, ["A", "colname", "result", "treatment"]]
)
result = df.pivot_longer(
index="A",
names_to=(".value", "colname"),
names_sep="_",
names_transform=float,
).loc[:, ["A", "colname", "result", "treatment"]]

actual = pd.wide_to_long(
df,
Expand All @@ -1210,7 +1209,10 @@ def test_names_transform_numeric():
sep="_",
).reset_index()

assert_frame_equal(actual, expected)
result = result.sort_values(result.columns.tolist(), ignore_index=True)
actual = actual.sort_values(actual.columns.tolist(), ignore_index=True)

assert_frame_equal(actual, result)


def test_duplicated_columns():
Expand Down
10 changes: 5 additions & 5 deletions tests/utils/test__select_column.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pandas as pd
import pytest

from pandas.testing import assert_index_equal
from pandas.testing import assert_index_equal, assert_frame_equal
from janitor.functions.utils import _select_column_names, patterns


Expand Down Expand Up @@ -165,10 +165,10 @@ def test_strings_dates_range(df_dates):
def test_unsorted_dates(df_dates):
"""Test output if the dates are unsorted, and a string is passed."""
df_dates = df_dates.iloc[:, [10, 4, 7, 2, 1, 3, 5, 6, 8, 9, 11, 0]]
assert_index_equal(
df_dates.loc[:, ["2011-01-31"]].columns,
_select_column_names("2011-01-31", df_dates),
)
expected = df_dates.loc[:, ["2011-01-31"]]
actual = _select_column_names("2011-01-31", df_dates)
actual = df_dates.loc[:, actual]
assert_frame_equal(expected, actual)


def test_regex(df1):
Expand Down