def __init__(self, values, dtype: Dtype | None = None, freq=None, copy: bool = False): freq = validate_dtype_freq(dtype, freq) if freq is not None: freq = Period._maybe_convert_freq(freq) if isinstance(values, ABCSeries): values = values._values if not isinstance(values, type(self)): raise TypeError("Incorrect dtype") elif isinstance(values, ABCPeriodIndex): values = values._values if isinstance(values, type(self)): if freq is not None and freq != values.freq: raise raise_on_incompatible(values, freq) values, freq = values._ndarray, values.freq values = np.array(values, dtype="int64", copy=copy) if freq is None: raise ValueError("freq is not specified and cannot be inferred") NDArrayBacked.__init__(self, values, PeriodDtype(freq))
def _from_sequence(cls, scalars, *, dtype: Dtype | None = None, copy=False): if dtype and not (isinstance(dtype, str) and dtype == "string"): dtype = pandas_dtype(dtype) assert isinstance(dtype, StringDtype) and dtype.storage == "python" from pandas.core.arrays.masked import BaseMaskedArray if isinstance(scalars, BaseMaskedArray): # avoid costly conversion to object dtype na_values = scalars._mask result = scalars._data result = lib.ensure_string_array(result, copy=copy, convert_na_value=False) result[na_values] = StringDtype.na_value else: # convert non-na-likes to str, and nan-likes to StringDtype.na_value result = lib.ensure_string_array(scalars, na_value=StringDtype.na_value, copy=copy) # Manually creating new array avoids the validation step in the __init__, so is # faster. Refactor need for validation? new_string_array = cls.__new__(cls) NDArrayBacked.__init__(new_string_array, result, StringDtype(storage="python")) return new_string_array
def _init_by_arrow(self, values, dtype: ArrowDtype = None, copy=False): if isinstance(values, (pd.Index, pd.Series)): # for pandas Index and Series, # convert to PandasArray values = values.array if isinstance(values, type(self)): arrow_array = values._arrow_array elif isinstance(values, ExtensionArray): # if come from pandas object like index, # convert to pandas StringArray first, # validation will be done in construct arrow_array = pa.chunked_array([pa.array(values, from_pandas=True)]) elif isinstance(values, pa.ChunkedArray): arrow_array = values elif isinstance(values, pa.Array): arrow_array = pa.chunked_array([values]) else: arrow_array = pa.chunked_array([pa.array(values, type=dtype.arrow_type)]) if copy: arrow_array = copy_obj(arrow_array) self._use_arrow = True self._arrow_array = arrow_array if NDArrayBacked is not None and isinstance(self, NDArrayBacked): NDArrayBacked.__init__(self, np.array([]), dtype) else: self._dtype = dtype
def __init__(self, values, copy=False): values = extract_array(values) super().__init__(values, copy=copy) if not isinstance(values, type(self)): self._validate() NDArrayBacked.__init__(self, self._ndarray, StringDtype(storage="python"))
def __init__(self, values, dtype=TD64NS_DTYPE, freq=lib.no_default, copy: bool = False): values = extract_array(values, extract_numpy=True) if isinstance(values, IntegerArray): values = values.to_numpy("int64", na_value=tslibs.iNaT) inferred_freq = getattr(values, "_freq", None) explicit_none = freq is None freq = freq if freq is not lib.no_default else None if isinstance(values, type(self)): if explicit_none: # dont inherit from values pass elif freq is None: freq = values.freq elif freq and values.freq: freq = to_offset(freq) freq, _ = dtl.validate_inferred_freq(freq, values.freq, False) values = values._ndarray if not isinstance(values, np.ndarray): msg = ( f"Unexpected type '{type(values).__name__}'. 'values' must be a " "TimedeltaArray, ndarray, or Series or Index containing one of those." ) raise ValueError(msg) if values.ndim not in [1, 2]: raise ValueError("Only 1-dimensional input arrays are supported.") if values.dtype == "i8": # for compat with datetime/timedelta/period shared methods, # we can sometimes get here with int64 values. These represent # nanosecond UTC (or tz-naive) unix timestamps values = values.view(TD64NS_DTYPE) _validate_td64_dtype(values.dtype) dtype = _validate_td64_dtype(dtype) if freq == "infer": msg = ( "Frequency inference not allowed in TimedeltaArray.__init__. " "Use 'pd.array()' instead.") raise ValueError(msg) if copy: values = values.copy() if freq: freq = to_offset(freq) NDArrayBacked.__init__(self, values=values, dtype=dtype) self._freq = freq if inferred_freq is None and freq is not None: type(self)._validate_frequency(self, freq)
def __init__(self, values, copy=False): values = extract_array(values) super().__init__(values, copy=copy) # error: Incompatible types in assignment (expression has type "StringDtype", # variable has type "PandasDtype") NDArrayBacked.__init__(self, self._ndarray, StringDtype(storage="python")) if not isinstance(values, type(self)): self._validate()
def _init_by_numpy(self, values, dtype: ArrowDtype = None, copy=False): self._use_arrow = False ndarray = np.array(values, copy=copy) if NDArrayBacked is not None and isinstance(self, NDArrayBacked): NDArrayBacked.__init__(self, ndarray, dtype) else: self._dtype = dtype self._ndarray = np.array(values, copy=copy)
def test_engine_type(self, dtype, engine_type): if dtype != np.int64: # num. of uniques required to push CategoricalIndex.codes to a # dtype (128 categories required for .codes dtype to be int16 etc.) num_uniques = {np.int8: 1, np.int16: 128, np.int32: 32768}[dtype] ci = CategoricalIndex(range(num_uniques)) else: # having 2**32 - 2**31 categories would be very memory-intensive, # so we cheat a bit with the dtype ci = CategoricalIndex(range(32768)) # == 2**16 - 2**(16 - 1) arr = ci.values._ndarray.astype("int64") NDArrayBacked.__init__(ci._data, arr, ci.dtype) assert np.issubdtype(ci.codes.dtype, dtype) assert isinstance(ci._engine, engine_type)
def load_reduce(self): stack = self.stack args = stack.pop() func = stack[-1] try: stack[-1] = func(*args) return except TypeError as err: # If we have a deprecated function, # try to replace and try again. msg = "_reconstruct: First argument must be a sub-type of ndarray" if msg in str(err): try: cls = args[0] stack[-1] = object.__new__(cls) return except TypeError: pass elif args and isinstance(args[0], type) and issubclass( args[0], BaseOffset): # TypeError: object.__new__(Day) is not safe, use Day.__new__() cls = args[0] stack[-1] = cls.__new__(*args) return elif args and issubclass(args[0], PeriodArray): cls = args[0] stack[-1] = NDArrayBacked.__new__(*args) return raise