lisa.datautils.series_convert#

lisa.datautils.series_convert(series, dtype, nullable=None)[source]#

Convert a pandas.Series with a best effort strategy.

Nullable types may be used if necessary and possible, otherwise object dtype will be used.

Parameters:
  • series (pandas.Series) – Series of another type than the target one. Strings are allowed.

  • dtype (str or collections.abc.Callable) –

    dtype to convert to. If it is a string (like "uint8"), the following strategy will be used:

    1. Convert to the given dtype

    2. If it failed, try converting to an equivalent nullable dtype

    3. If it failed, try to parse it with an equivalent Python object constructor, and then convert it to the dtype.

    4. If an integer dtype was requested, parsing as hex string will be attempted too

    If it is a callable, it will be applied on the series, converting all values considered as nan by pandas.isna() into None values. The result will have object dtype. The callable has a chance to handle the conversion from nan itself.

    Note

    In some cases, asking for an unsigned dtype might let through negative values, as there is no way to reliably distinguish between conversion failures reasons.

  • nullable (bool or None) –

    If:

    • True, use the nullable dtype equivalent of the requested dtype.

    • None, use the equivalent nullable dtype if there is any missing

      data, otherwise a non-nullable dtype will be used for lower memory consumption.