Using a masked array to create a pandas DataFrame

I think I get the following behavior. b / c my input array is masked, which I hardly understand. I looked at this pandas doc at gotchas , but am not quite sure what the “control” value is. Is it something like "-9999" that is used when creating a DataFrame, which is subsequently set to NULL?

What is a good way to handle these values ​​when creating a DataFrame? I'm glad if masked entries are set to NULL.

Here is an example:

[Dbg]>>> segDF = pd.DataFrame(segArrNew)
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
  File "C:\Anaconda\envs\esri\Lib\site-packages\pandas\core\frame.py", line 393, in __init__
    datacopy[mask] = NA
  File "C:\Python27\ArcGIS10.2\lib\site-packages\numpy\ma\core.py", line 3027, in __setitem__
    ndarray.__setitem__(_data, indx, dval)
IndexError: arrays used as indices must be of integer (or boolean) type

[Dbg]>>> segArrNew
masked_array(data = [(1, u'01', 1, --) (2, u'01', 2, --) (3, u'01', 3, --) ...,
 (56853, u'21', 241, --) (56854, u'21', 242, --) (56855, u'21', 243, --)],
             mask = [(False, False, False, True) (False, False, False, True)
 (False, False, False, True) ..., (False, False, False, True)
 (False, False, False, True) (False, False, False, True)],
       fill_value = (999999, u'N/A', 999999, u'N/A'),
            dtype = [('seg_id_nat', '<i4'), ('region', '<U255'), ('seg_id_reg', '<i4'), ('refGage', '<U30')])

, --. (refGage), , , , .

[Dbg]>>> segArrNew2 = npFuncs.drop_fields(segArrNew, ['refGage'])
[Dbg]>>> segDF = pd.DataFrame(segArrNew2)
[Dbg]>>> segDF
<class 'pandas.core.frame.DataFrame'>
Int64Index: 57186 entries, 0 to 57185
Data columns:
seg_id_nat    57186  non-null values
region        57186  non-null values
seg_id_reg    57186  non-null values
dtypes: int64(2), object(1)
+4
1

, "" , numpy.ndarray numpy.ma.filled() ( ).

[Dbg]>>> np.ma.filled(segArrNew)
array([(1, u'01', 1, u'N/A'), (2, u'01', 2, u'N/A'), (3, u'01', 3, u'N/A'),
       ..., (56853, u'21', 241, u'N/A'), (56854, u'21', 242, u'N/A'),
       (56855, u'21', 243, u'N/A')], 
      dtype=[('seg_id_nat', '<i4'), ('region', '<U255'), ('seg_id_reg', '<i4'), ('refGage', '<U30')])

[Dbg]>>> df = pandas.DataFrame(numpy.ma.filled(segArrNew))
[Dbg]>>> df.ix[df['refGage'] == u'N/A'], 'refGage'] = ''

DataFrame fill_value segArrNew. df.ix(), , .

+1

All Articles