Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG/ERR: adding column with float label to Int64Index gives ValueError #8720

Closed
jorisvandenbossche opened this issue Nov 3, 2014 · 3 comments · Fixed by #30554
Closed

BUG/ERR: adding column with float label to Int64Index gives ValueError #8720

jorisvandenbossche opened this issue Nov 3, 2014 · 3 comments · Fixed by #30554
Labels
good first issue Needs Tests Unit test(s) needed to prevent regressions
Milestone

Comments

@jorisvandenbossche
Copy link
Member

When adding a column with a float label to a DataFame with integer column index, you get a ValueError: cannot insert 0.5, already exists error with an incorrect message:

In [15]: df = pd.DataFrame(np.random.randn(8,4))

In [16]: df
Out[16]:
          0         1         2         3
0 -0.482266 -1.239583 -1.177658  1.676881
1  0.314620 -0.373801 -1.535093  0.829534
2  0.887573 -0.054425  0.313028  0.590081
3 -0.665164 -0.887124  0.720014 -0.361315
4 -0.082880 -0.435376 -0.124390 -0.883354
5  2.122313  0.155019  0.762171 -0.401316
6  1.368269  0.469080 -0.274609 -1.799257
7 -0.631479  0.302715  0.008982 -0.356644

In [17]: df[0.5] = np.NaN
...
ValueError: cannot insert 0.5, already exists

In [18]: df.loc[:,0.5] = np.NaN
...
TypeError: the label [0.5] is not a proper indexer for this index type (Int64Index)

Of course, when the column labels are converted to floats (df.columns = pd.Float64Index(df.columns)), this does work.

@jorisvandenbossche jorisvandenbossche added the Error Reporting Incorrect or improved errors from pandas label Nov 3, 2014
@jorisvandenbossche jorisvandenbossche changed the title BUG: adding column with float label to Int64Index gives ValueError BUG/ERR: adding column with float label to Int64Index gives ValueError Nov 3, 2014
@jreback
Copy link
Contributor

jreback commented Nov 3, 2014

the problem is [] tries to coerce it to an int (e.g. it sort of acts like .ix here.
guess this should work though (maybe)

@wesm
Copy link
Member

wesm commented Jul 6, 2018

This is still broken

@mroeschke
Copy link
Member

Looks to work on master. Could use a test.

In [21]: In [17]: In [15]: df = pd.DataFrame(np.random.randn(8,4))

In [22]: In [17]: df[0.5] = np.NaN
    ...:

In [23]: df
Out[23]:
        0.0       1.0       2.0       3.0  0.5
0 -0.893317 -0.999960  0.227483 -0.616143  NaN
1 -1.557036 -0.661066  1.256377 -0.251763  NaN
2  0.874911  1.208229  0.496551  0.602761  NaN
3 -0.143278  1.483060 -0.441488 -0.036813  NaN
4 -0.423643  0.937388 -0.736469 -1.473181  NaN
5 -1.169715  0.048951  0.463316  0.474189  NaN
6 -0.064023  0.252234 -1.152730  0.603041  NaN
7  0.080979 -0.022521 -0.040599  1.601390  NaN

In [24]: In [15]: df = pd.DataFrame(np.random.randn(8,4))
    ...:

In [25]: In [18]: df.loc[:,0.5] = np.NaN
    ...:

In [26]: df
Out[26]:
        0.0       1.0       2.0       3.0  0.5
0  0.324355 -0.360758 -2.094797  1.482548  NaN
1 -1.335983  0.264566  0.436681  0.716613  NaN
2 -1.386648  1.968785  0.383127 -0.103017  NaN
3 -1.090121  1.289168  1.373147  0.181029  NaN
4  1.072814  1.546031 -0.376255  0.139408  NaN
5 -0.169053  1.594981  0.882534 -0.962147  NaN
6 -0.884327  0.781743  0.298201  0.177668  NaN
7  0.433245 -2.354488  1.608168 -0.835746  NaN

In [27]: pd.__version__
Out[27]: '0.26.0.dev0+684.g953757a3e'

@mroeschke mroeschke added good first issue Needs Tests Unit test(s) needed to prevent regressions and removed Error Reporting Incorrect or improved errors from pandas labels Oct 27, 2019
@simonjayhawkins simonjayhawkins added this to the 1.0 milestone Dec 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Needs Tests Unit test(s) needed to prevent regressions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants