pandas.SparseDtype.update_dtype

SparseDtype.update_dtype(dtype)[source]

Convert the SparseDtype to a new dtype.

This takes care of converting the fill_value.

Parameters:
dtype : Union[str, numpy.dtype, SparseDtype]

The new dtype to use.

  • For a SparseDtype, it is simply returned
  • For a NumPy dtype (or str), the current fill value is converted to the new dtype, and a SparseDtype with dtype and the new fill value is returned.
Returns:
SparseDtype

A new SparseDtype with the corret dtype and fill value for that dtype.

Raises:
ValueError

When the current fill value cannot be converted to the new dtype (e.g. trying to convert np.nan to an integer dtype).

Examples

>>> SparseDtype(int, 0).update_dtype(float)
Sparse[float64, 0.0]
>>> SparseDtype(int, 1).update_dtype(SparseDtype(float, np.nan))
Sparse[float64, nan]
Scroll To Top