Pandas: how to convert a column with missing values to string? Pandas: how to convert a column with missing values to string? pandas pandas

Pandas: how to convert a column with missing values to string?


Using np.where would certainly be a bit faster compared to replace i.e

df['text'] = np.where(pd.isnull(df['text']),df['text'],df['text'].astype(str))

Timings :

%%timeitdf['text'].astype(str).replace('nan',np.nan)1000 loops, best of 3: 536 µs per loop%%timeitnp.where(pd.isnull(df['text']),df['text'],df['text'].astype(str))1000 loops, best of 3: 274 µs per loopx = pd.concat([df['text']]*10000)%%timeitnp.where(pd.isnull(x),x,x.astype(str))10 loops, best of 3: 28.8 ms per loop%%timeitx.astype(str).replace('nan',np.nan)10 loops, best of 3: 33.5 ms per loop