Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python pandas series: convert float to string, preserving nulls

How can I preserve nulls after converting to string? I'm working with social security numbers, where it's necessary to go back and forth between float and string.

import pandas as pd
import numpy as np    
x = pd.Series([np.nan, 123., np.nan, 456.], dtype = float)
x.isnull()

...Has nulls

y = x.astype(str)
y.isnull()

...No nulls

So ideally x.isnull() and y.isnull() would be the same.

I think it's dangerous to use a Series of mixed dtypes, but thinking this is the best solution for the time being:

z = y.copy()
z[z == 'nan'] = np.nan
z.isnull() # works as desired
type(z[0]) # but has floats for nulls
type(z[1]) # and strings for values
like image 612
mef jons Avatar asked Mar 30 '17 18:03

mef jons


1 Answers

I encountered this problem too, but for DataFrames. A method which works on both pandas Series and DataFrame is to make use of mask():

data = pd.Series([np.NaN, 10, 30, np.NaN]) # Also works for pd.DataFrame
null_cells = data.isnull()
data = data.astype(str).mask(null_cells, np.NaN)
like image 143
Greg Dooley Avatar answered Oct 17 '22 07:10

Greg Dooley