You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A DataFrame will fail to unstack() when one of the columns retained as an index has NaN values. The code below sets up a dataframe with NaN in some index entries, at which point calling unstack() will fail.
In the first failure, the exception message is that the index "has duplicate entries" which is patently false. In the second failure, where a given id only has one NaN, the error message becomes cannot convert float NaN to integer.
A final try, with NaN converted to a sentinel value of 42, shows proper behavior.
Error with all data was: Index contains duplicate entries, cannot reshape
Error dropping first entry was: cannot convert float NaN to integer
change
agent Ag Hg Pb Sn U
s_id dosage
680585148 42.0000 NaN NaN 0.000007 NaN 0
680607017 0.0133 0 -0.00015 NaN 0.000024 NaN
The text was updated successfully, but these errors were encountered:
In [12]: df1
Out[12]:
change
s_id dosage agent
680585148 NaN Hg NaN
U 0.000000
Pb 0.000007
680607017 0.0133 Sn 0.000024
Ag 0.000000
Hg -0.000150
In [13][: df1.unstack()
# ValueError: Index contains duplicate entries, cannot reshape
In [21]: df2
Out[21]:
change
s_id dosage agent
680585148 42.0000 Hg NaN
U 0.000000
Pb 0.000007
680607017 0.0133 Sn 0.000024
Ag 0.000000
Hg -0.000150
In [22]: df2.unstack()
Out[22]:
change
agent Ag Hg Pb Sn U
s_id dosage
680585148 42.0000 NaN NaN 0.000007 NaN 0
680607017 0.0133 0 -0.00015 NaN 0.000024 NaN
{Python 2.6.6, pandas 0.12}
A DataFrame will fail to unstack() when one of the columns retained as an index has NaN values. The code below sets up a dataframe with NaN in some index entries, at which point calling unstack() will fail.
In the first failure, the exception message is that the index "has duplicate entries" which is patently false. In the second failure, where a given id only has one NaN, the error message becomes cannot convert float NaN to integer.
A final try, with NaN converted to a sentinel value of 42, shows proper behavior.
Overall output:
The text was updated successfully, but these errors were encountered: