Skip to content

BUG: should astype only TRY to convert string columns? #2718

Closed
@jreback

Description

@jreback

should astype TRY to convert string columns (e.g. if the value of the string column is like '5'), or skip if its an exception (and just return it), rather than raise an exception on the whole operation?

I can fix for 0.10.2

In [4]: df = pd.DataFrame({ 'a' : 'foo', 'b' : 1. },index=np.arange(10))
In [6]: df .dtypes
Out[6]: 
a     object
b    float64

In [8]: df.astype('float64')
/mnt/home/jreback/pandas/pandas/core/internals.pyc in astype(self, dtype)
    613         new_blocks = []
    614         for block in self.blocks:
--> 615             newb = make_block(com._astype_nansafe(block.values, dtype),
    616                               block.items, block.ref_items)
    617             new_blocks.append(newb)

/mnt/home/jreback/pandas/pandas/core/common.pyc in _astype_nansafe(arr, dtype)
   1058         return lib.astype_intsafe(arr.ravel(), dtype).reshape(arr.shape)
   1059 
-> 1060     return arr.astype(dtype)
   1061 
   1062 

ValueError: could not convert string to float: foo

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions