Skip to content

Commit 2c5939b

Browse files
committed
ENH: Allow non-int j vals
1 parent 798cf28 commit 2c5939b

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

pandas/core/reshape.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,8 @@ def wide_to_long(df, stubnames, i, j):
801801
The name of the id variable.
802802
j : str
803803
The name of the subobservation variable.
804+
stubend : str
805+
Regex to match for the end of the stubs.
804806
805807
Returns
806808
-------
@@ -821,10 +823,10 @@ def wide_to_long(df, stubnames, i, j):
821823
... })
822824
>>> df["id"] = df.index
823825
>>> df
824-
A1970 A1980 B1970 B1980 X
825-
0 a d 2.5 3.2 -1.085631
826-
1 b e 1.2 1.3 0.997345
827-
2 c f 0.7 0.1 0.282978
826+
A1970 A1980 B1970 B1980 X id
827+
0 a d 2.5 3.2 -1.085631 0
828+
1 b e 1.2 1.3 0.997345 1
829+
2 c f 0.7 0.1 0.282978 2
828830
>>> wide_to_long(df, ["A", "B"], i="id", j="year")
829831
X A B
830832
id year
@@ -848,7 +850,12 @@ def melt_stub(df, stub, i, j):
848850
varnames = get_var_names(df, "^"+stub)
849851
newdf = melt(df, id_vars=i, value_vars=varnames,
850852
value_name=stub, var_name=j)
851-
newdf[j] = newdf[j].str.replace(stub, "").astype(int)
853+
newdf_j = newdf[j].str.replace(stub, "")
854+
try:
855+
newdf_j = newdf_j.astype(int)
856+
except ValueError:
857+
pass
858+
newdf[j] = newdf_j
852859
return newdf
853860

854861
id_vars = get_var_names(df, "^(?!%s)" % "|".join(stubnames))

0 commit comments

Comments
 (0)