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
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With