How to prevent automatic assignment of values to missing data imported from SPSS
Question: Let's say I have an spss file named "ab.sav" which looks like this:

    
    
    gender  value  value2
    
    F       433    329
    .        .     787
    .        .      .
    M       121     .
    F       311    120
    .        .     899
    M       341     .
    

In spss (Variable View) I defined the labels of `gender` with the values `1`
and `2` for `M` and `F` respectively.

When I load this in python using the following commands:

    
    
    >>> from rpy2.robjects.packages import importr
    >>> from rpy2.robjects import pandas2ri  
    >>> foreign=importr("foreign")
    >>> data=foreign.read_spss("ab.sav", to_data_frame=True, use_value_labels=True)
    >>> pandas2ri.activate()
    >>> data2=pandas2ri.ri2py(data)
    

I get the following dataframe:

    
    
    >>> data2
    
      gender   value   value2
    0   F        433    329
    1   M        NaN    787
    2   M        NaN    NaN
    3   M        121    NaN
    4   F        311    120
    5   M        NaN    899
    6   M        341    NaN
    

So the missing values in the `gender` column for a given case are replaced by
the subsequent known value of the subsequent case. Is there a simple way to
prevent this?

When I change `use_value_labels` to `False` I get the expected result though:

    
    
    >>> data2
    
      gender   value   value2
    0   2        433   329
    1   NaN      NaN   787
    2   NaN      NaN   NaN
    3   1        121   NaN
    4   2        311   120
    5   NaN      NaN   899
    6   1        341   NaN
    

However I'd like to be able to use the labels instead of numeric values for
`gender` as above. Ideally the output should be:

    
    
    >>> data2
    
      gender   value    value2
    0   F        433     329
    1   NaN      NaN     787
    2   NaN      NaN     NaN
    3   M        121     NaN
    4   F        311     120
    5   NaN      NaN     899
    6   M        341     NaN
    


Answer: Assuming `data2` is a pandas DataFrame, and there's a 1-to-1 mapping between
nulls in `value` and `gender`, you can do the following:

    
    
    nulls = pandas.isnull(data2['value'])
    data2.loc[nulls, 'gender'] = np.nan
    

And that turns it into:

    
    
      gender  value  value2
    0      F    433     329
    1    NaN    NaN     787
    2    NaN    NaN     NaN
    3      M    121     NaN
    4      F    311     120
    5    NaN    NaN     899
    6      M    341     NaN
    

