if not isinstance(string, str): raise TypeError( "'construct_from_string' expects a string, got {}".format( type(string))) elif string == cls.name: return cls() else: raise TypeError("Cannot construct a '{}' from '{}'".format( cls.__name__, string)) @classmethod def construct_array_type(cls): return GeometryArray register_extension_dtype(GeometryDtype) def _isna(value): """ Check if scalar value is NA-like (None, np.nan or pd.NA). Custom version that only works for scalars (returning True or False), as `pd.isna` also works for array-like input returning a boolean array. """ if value is None: return True elif isinstance(value, float) and np.isnan(value): return True elif compat.PANDAS_GE_10 and value is pd.NA: return True
# Note: the base tests provided by pandas only test the basics. # We do not test # 1. Values outside the range of the `data_for_sorting` fixture # 2. Values between the values in the `data_for_sorting` fixture # 3. Missing values. arr = self._data if isinstance(value, _Quantity): value = value.to(self.units).magnitude elif is_list_like(value) and isinstance(value[0], _Quantity): value = [item.to(self.units).magnitude for item in value] return arr.searchsorted(value, side=side, sorter=sorter) PintArray._add_arithmetic_ops() PintArray._add_comparison_ops() register_extension_dtype(PintType) @register_dataframe_accessor("pint") class PintDataFrameAccessor(object): def __init__(self, pandas_obj): self._obj = pandas_obj def quantify(self, level=-1): df = self._obj df_columns = df.columns.to_frame() unit_col_name = df_columns.columns[level] units = df_columns[unit_col_name] df_columns = df_columns.drop(columns=unit_col_name) df_new = DataFrame(
else: raise TypeError( f'Cannot construct a "{cls.__name__}" from "{string}"') @classmethod def construct_array_type(cls): """ Return the array type associated with this dtype. Return: GeosArray: Associated ExtensionArray. """ return GeosArray register_extension_dtype(GeosDtype) class GeosArray(ExtensionArray): dtype = GeosDtype() #: Dtype for this ExtensionArray ndim = 1 #: Number of dimensions of this ExtensionArray # ------------------------------------------------------------------------- # (De-)Serialization # ------------------------------------------------------------------------- def __init__(self, data): """ Create a GeosArray. Args: data (Iterable): Data for the GeosArray (see Note)
na_value = None @classmethod def construct_from_string(cls, string): if string == cls.name: return cls() else: raise TypeError(f"Cannot construct a 'TimeDtype' from '{string}'") @classmethod def construct_array_type(cls): return TimeArray # Register the datatype with pandas register_extension_dtype(TimeDtype) # ------------------------------------------------------------------------------ # Constructors / converters to other formats # ------------------------------------------------------------------------------ def from_list(data): """ Create TimeArray object from list of timeseries-like objects Parameters ---------- data : list each element in `data` represents a single timeseries that will be converted into a row in the TimeArray. Elements of `data` need to
na_value = np.nan @classmethod def construct_from_string(cls, string): if string == cls.name: return cls() else: raise TypeError("Cannot construct a '{}' from '{}'".format( cls.__name__, string)) @classmethod def construct_array_type(cls): return QAArray register_extension_dtype(QADtype) class QAArray(ExtensionArray): """ Class wrapping a numpy array of QA bits and holding the array-based implementations. """ _dtype = QADtype() def __init__(self, data): if isinstance(data, self.__class__): data = data.data elif not isinstance(data, np.ndarray): raise TypeError("'data' should be array of qa objects.")