Closed sotte closed 2 years ago
This would be the fix: var_list = var_create(*args or __data.columns)
.
And with some context and a doctest:
@singledispatch2(pd.DataFrame)
def gather(__data, key, value, *args, drop_na=False, convert=False):
"""Reshape table by gathering it in to long format.
Examples
--------
>>> import pandas as pd
>>> from siuba import _, gather
>>> df = pd.DataFrame({"x": [1, 2], "y": [3, None]})
>>> gather(df, key="key", value="value")
key value
0 x 1.0
1 x 2.0
2 y 3.0
3 y NaN
"""
# TODO: implement var selection over *args
if convert:
raise NotImplementedError("convert not yet implemented")
# TODO: copied from nest and select
var_list = var_create(*args or __data.columns) # <-- this is the fix
od = var_select(__data.columns, *var_list)
value_vars = list(od) or None
id_vars = [col for col in __data.columns if col not in od]
long = pd.melt(__data, id_vars, value_vars, key, value)
if drop_na:
return long[~long[value].isna()].reset_index(drop=True)
return long
However there is a TODO and maybe you want to implement a more systematic solution.
Should be fixed in v0.4.0!
The docs for
gather
state for*args
:However, if not
*args
are specified, nothing is selected.Code to reproduce this: