@@ -3293,7 +3293,7 @@ def group_by(self, keys):
32933293 """
32943294 return groups .table_group_by (self , keys )
32953295
3296- def to_pandas (self , index = None ):
3296+ def to_pandas (self , index = None , use_nullable_int = True ):
32973297 """
32983298 Return a :class:`pandas.DataFrame` instance
32993299
@@ -3304,20 +3304,28 @@ def to_pandas(self, index=None):
33043304 then no DataFrame index will be specified. If ``index`` is the name of
33053305 a column in the table then that will be the DataFrame index.
33063306
3307- In additional to vanilla columns or masked columns, this supports Table
3307+ In addition to vanilla columns or masked columns, this supports Table
33083308 mixin columns like Quantity, Time, or SkyCoord. In many cases these
33093309 objects have no analog in pandas and will be converted to a "encoded"
33103310 representation using only Column or MaskedColumn. The exception is
33113311 Time or TimeDelta columns, which will be converted to the corresponding
33123312 representation in pandas using ``np.datetime64`` or ``np.timedelta64``.
33133313 See the example below.
33143314
3315+ Parameters
3316+ ----------
3317+ index : None, bool, str
3318+ Specify DataFrame index mode
3319+ use_nullable_int : bool, default=True
3320+ Convert integer MaskedColumn to pandas nullable integer type.
3321+ If ``use_nullable_int=False`` or the pandas version does not support
3322+ nullable integer types (version < 0.24), then the column is converted
3323+ to float with NaN for missing elements and a warning is issued.
3324+
33153325 Returns
33163326 -------
33173327 dataframe : :class:`pandas.DataFrame`
33183328 A pandas :class:`pandas.DataFrame` instance
3319- index : None, bool, str
3320- Specify DataFrame index mode
33213329
33223330 Raises
33233331 ------
@@ -3353,7 +3361,7 @@ def to_pandas(self, index=None):
33533361 2002-01-01 2.0 6.0 8.0 00:03:20
33543362
33553363 """
3356- from pandas import DataFrame
3364+ from pandas import DataFrame , Series
33573365
33583366 if index is not False :
33593367 if index in (None , True ):
@@ -3417,18 +3425,26 @@ def _encode_mixins(tbl):
34173425 for name , column in tbl .columns .items ():
34183426 if isinstance (column , MaskedColumn ) and np .any (column .mask ):
34193427 if column .dtype .kind in ['i' , 'u' ]:
3420- out [name ] = column .astype (float ).filled (np .nan )
3421- warnings .warn (
3422- "converted column '{}' from integer to float" .format (
3423- name ), TableReplaceWarning , stacklevel = 3 )
3428+ pd_dtype = str (column .dtype )
3429+ if use_nullable_int :
3430+ # Convert int64 to Int64, uint32 to UInt32, etc for nullable types
3431+ pd_dtype = pd_dtype .replace ('i' , 'I' ).replace ('u' , 'U' )
3432+ out [name ] = Series (column , dtype = pd_dtype )
3433+
3434+ # If pandas is older than 0.24 the type may have turned to float
3435+ if column .dtype .kind != out [name ].dtype .kind :
3436+ warnings .warn (
3437+ f"converted column '{ name } ' from { column .dtype } to { out [name ].dtype } " ,
3438+ TableReplaceWarning , stacklevel = 3 )
34243439 elif column .dtype .kind in ['f' , 'c' ]:
3425- out [name ] = column . filled ( np . nan )
3440+ out [name ] = column
34263441 else :
34273442 out [name ] = column .astype (object ).filled (np .nan )
34283443 else :
34293444 out [name ] = column
34303445
3431- if out [name ].dtype .byteorder not in ('=' , '|' ):
3446+ if (hasattr (out [name ].dtype , 'byteorder' )
3447+ and out [name ].dtype .byteorder not in ('=' , '|' )):
34323448 out [name ] = out [name ].byteswap ().newbyteorder ()
34333449
34343450 kwargs = {'index' : out .pop (index )} if index else {}
@@ -3526,6 +3542,14 @@ def from_pandas(cls, dataframe, index=False, units=None):
35263542
35273543 for name , column , data , mask , unit in zip (names , columns , datas , masks , units ):
35283544
3545+ if column .dtype .kind in ['u' , 'i' ] and np .any (mask ):
3546+ # Special-case support for pandas nullable int
3547+ np_dtype = str (column .dtype ).lower ()
3548+ data = np .zeros (shape = column .shape , dtype = np_dtype )
3549+ data [~ mask ] = column [~ mask ]
3550+ out [name ] = MaskedColumn (data = data , name = name , mask = mask , unit = unit , copy = False )
3551+ continue
3552+
35293553 if data .dtype .kind == 'O' :
35303554 # If all elements of an object array are string-like or np.nan
35313555 # then coerce back to a native numpy str/unicode array.
0 commit comments