@@ -801,6 +801,8 @@ def wide_to_long(df, stubnames, i, j):
801
801
The name of the id variable.
802
802
j : str
803
803
The name of the subobservation variable.
804
+ stubend : str
805
+ Regex to match for the end of the stubs.
804
806
805
807
Returns
806
808
-------
@@ -821,10 +823,10 @@ def wide_to_long(df, stubnames, i, j):
821
823
... })
822
824
>>> df["id"] = df.index
823
825
>>> 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
828
830
>>> wide_to_long(df, ["A", "B"], i="id", j="year")
829
831
X A B
830
832
id year
@@ -848,7 +850,12 @@ def melt_stub(df, stub, i, j):
848
850
varnames = get_var_names (df , "^" + stub )
849
851
newdf = melt (df , id_vars = i , value_vars = varnames ,
850
852
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
852
859
return newdf
853
860
854
861
id_vars = get_var_names (df , "^(?!%s)" % "|" .join (stubnames ))
0 commit comments