Skip to content

Commit 295d1e6

Browse files
committed
Use pd.Index in doc example
Use is_list_like Add GH ticket #
1 parent 1c49291 commit 295d1e6

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

pandas/core/reshape.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -894,10 +894,10 @@ def wide_to_long(df, stubnames, i, j, sep="", suffix='\d+'):
894894
----------
895895
df : DataFrame
896896
The wide-format DataFrame
897-
stubnames : list or string
897+
stubnames : str or list-like
898898
The stub name(s). The wide format variables are assumed to
899899
start with the stub names.
900-
i : list or string
900+
i : str or list-like
901901
Column(s) to use as id variable(s)
902902
j : str
903903
The name of the subobservation variable. What you wish to name your
@@ -993,7 +993,7 @@ def wide_to_long(df, stubnames, i, j, sep="", suffix='\d+'):
993993
Going from long back to wide just takes some creative use of `unstack`
994994
995995
>>> w = l.reset_index().set_index(['famid', 'birth', 'age']).unstack()
996-
>>> w.columns = [name + suffix for name, suffix in wide.columns.tolist()]
996+
>>> w.columns = pd.Index(w.columns).str.join('')
997997
>>> w.reset_index()
998998
famid birth ht1 ht2
999999
0 1 1 2.8 3.4
@@ -1065,11 +1065,15 @@ def melt_stub(df, stub, i, j, value_vars, sep):
10651065
if any(map(lambda s: s in df.columns.tolist(), stubnames)):
10661066
raise ValueError("stubname can't be identical to a column name")
10671067

1068-
if not isinstance(stubnames, list):
1068+
if not is_list_like(stubnames):
10691069
stubnames = [stubnames]
1070+
else:
1071+
stubnames = list(stubnames)
10701072

1071-
if not isinstance(i, list):
1073+
if not is_list_like(i):
10721074
i = [i]
1075+
else:
1076+
i = list(i)
10731077

10741078
value_vars = list(map(lambda stub:
10751079
get_var_names(df, stub, sep, suffix), stubnames))

pandas/tests/test_reshape.py

+1
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,7 @@ def test_stubs(self):
717717
self.assertEqual(stubs, ['inc', 'edu'])
718718

719719
def test_separating_character(self):
720+
# GH14779
720721
np.random.seed(123)
721722
x = np.random.randn(3)
722723
df = pd.DataFrame({"A.1970": {0: "a",

0 commit comments

Comments
 (0)