Skip to content

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

Closed
@jorisvandenbossche

Description

@jorisvandenbossche

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions