Skip to content

Commit

Permalink
Fixed Issue #124
Browse files Browse the repository at this point in the history
Values_to is no longer a required variable for pivot_longer to work
  • Loading branch information
OscarDeGar committed Sep 9, 2021
1 parent f693cb4 commit fcf27cf
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
6 changes: 5 additions & 1 deletion grama/tran_pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,18 @@ def tran_pivot_longer (

### Check for .value input
dot_value = False
if names_str == False:
if names_str is False:
for i, v in enumerate(names_to):
if names_to[i] == ".value":
dot_value = True
else:
if names_to == ".value":
dot_value = True

### Check values_to argument
if values_to is None:
values_to = "values"

#######################################


Expand Down
32 changes: 30 additions & 2 deletions tests/test_pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from context import grama as gr
from context import data
from numpy import NaN
from numpy import NaN, random
from pandas import DataFrame, RangeIndex
from pandas.testing import assert_frame_equal

Expand Down Expand Up @@ -205,6 +205,7 @@ def test_pivot_longer_names_sep(self):
names_to=("property", "angle"),
values_to="val"
)

names_to = ["property","angle"]
names_to_check = [x for x in long.columns.values if x in names_to]

Expand Down Expand Up @@ -335,7 +336,6 @@ def test_pivot_longer_dot_value(self):
names_to=".value",
values_to="val"
)

check = ["E_00","mu_00","E_45","mu_45","E_90","mu_90"]
col_check = [x for x in long.columns.values if x in check]

Expand All @@ -345,6 +345,34 @@ def test_pivot_longer_dot_value(self):

self.assertTrue(result)

def test_pivot_longer_dot_value_and_names_sep(self):
""" Test pivot_longer when it receives the .value and names_sep
"""
DF = gr.Intention()
wide = gr.df_make(x=range(0, 6))
wide = gr.tran_mutate(
wide,
y_Trend=DF.x**2,
y_Variability=random.normal(size=6),
y_Mixed=DF.x**2 + random.normal(size=6),
)

long = gr.tran_pivot_longer(
wide,
columns=["y_Trend", "y_Variability", "y_Mixed"],
names_to=(".value", "type"),
names_sep="_"
)

check = ["x", "type", "y"]
col_check = [x for x in long.columns.values if x in check]

result = False
if set(col_check) == set(check):
result = True

self.assertTrue(result)


def test_pivot_longer_dot_value_and_index_to(self):
""" Test pivot_longer when it receives the .value input and an input
Expand Down

0 comments on commit fcf27cf

Please sign in to comment.