def test_resolve_dtype_c(self): a1 = np.array(['2019-01', '2019-02'], dtype=np.datetime64) a2 = np.array(['2019-01-01', '2019-02-01'], dtype=np.datetime64) a3 = np.array([0, 1], dtype='datetime64[ns]') a4 = np.array([0, 1]) self.assertEqual(str(resolve_dtype(a1.dtype, a2.dtype)), 'datetime64[D]') self.assertEqual(resolve_dtype(a1.dtype, a3.dtype), np.dtype('<M8[ns]')) self.assertEqual(resolve_dtype(a1.dtype, a4.dtype), np.dtype('O'))
def append(self, value): '''append a value ''' if value in self._map: raise KeyError(f'duplicate key append attempted: {value}') # the new value is the count self._map[value] = self._positions_mutable_count if self._labels_mutable_dtype is not None: self._labels_mutable_dtype = resolve_dtype( np.array(value).dtype, self._labels_mutable_dtype) else: self._labels_mutable_dtype = np.array(value).dtype self._labels_mutable.append(value) # check value before incrementing if self._loc_is_iloc: if isinstance(value, int) and value == self._positions_mutable_count: pass # an increment that keeps loc is iloc relationship else: self._loc_is_iloc = False self._positions_mutable_count += 1 self._recache = True
def append(self, value: tp.Hashable) -> None: '''append a value ''' if self.__contains__(value): #type: ignore raise KeyError(f'duplicate key append attempted: {value}') # we might need to initialize map if not an increment that keeps loc_is_iloc relationship initialize_map = False if self._map is None: # loc_is_iloc if not (isinstance(value, INT_TYPES) and value == self._positions_mutable_count): initialize_map = True else: self._map.add(value) if self._labels_mutable_dtype is not None: self._labels_mutable_dtype = resolve_dtype( np.array(value).dtype, self._labels_mutable_dtype) else: self._labels_mutable_dtype = np.array(value).dtype self._labels_mutable.append(value) if initialize_map: self._map = AutoMap(self._labels_mutable) self._positions_mutable_count += 1 self._recache = True
def _update_array_cache(self): if self._labels_mutable_dtype is not None and len(self._labels): # only update if _labels_mutable_dtype has been set and _labels exist self._labels_mutable_dtype = resolve_dtype( self._labels.dtype, self._labels_mutable_dtype) self._labels = np.array(self._labels_mutable, dtype=self._labels_mutable_dtype) self._labels.flags.writeable = False self._positions = PositionsAllocator.get(self._positions_mutable_count) self._recache = False
def test_resolve_dtype_a(self) -> None: a1 = np.array([1, 2, 3]) a2 = np.array([False, True, False]) a3 = np.array(['b', 'c', 'd']) a4 = np.array([2.3, 3.2]) a5 = np.array(['test', 'test again'], dtype='S') a6 = np.array([2.3,5.4], dtype='float32') self.assertEqual(resolve_dtype(a1.dtype, a1.dtype), a1.dtype) self.assertEqual(resolve_dtype(a1.dtype, a2.dtype), np.object_) self.assertEqual(resolve_dtype(a2.dtype, a3.dtype), np.object_) self.assertEqual(resolve_dtype(a2.dtype, a4.dtype), np.object_) self.assertEqual(resolve_dtype(a3.dtype, a4.dtype), np.object_) self.assertEqual(resolve_dtype(a3.dtype, a6.dtype), np.object_) self.assertEqual(resolve_dtype(a1.dtype, a4.dtype), np.float64) self.assertEqual(resolve_dtype(a1.dtype, a6.dtype), np.float64) self.assertEqual(resolve_dtype(a4.dtype, a6.dtype), np.float64)
def test_resolve_dtype(self, dtype_pair: tp.Tuple[np.dtype, np.dtype]) -> None: x = util.resolve_dtype(*dtype_pair) self.assertTrue(isinstance(x, np.dtype))
def test_resolve_dtype_b(self) -> None: self.assertEqual( resolve_dtype(np.array('a').dtype, np.array('aaa').dtype), np.dtype(('U', 3)) )
def pivot_index_map( *, index_src: IndexBase, depth_level: DepthLevelSpecifier, dtypes_src: tp.Optional[tp.Sequence[np.dtype]], ) -> PivotIndexMap: ''' Args: dtypes_src: must be of length equal to axis ''' # We are always moving levels from one axis to another; after application, the expanded axis will always be hierarchical, while the contracted axis may or may not be. From the contract axis, we need to divide the depths into two categories: targets (the depths to be moved and added to expand axis) and groups (unique combinations that remain on the contract axis after removing targets). # Unique target labels are added to labels on the expand axis; unique group labels become the new contract axis. target_select = np.full(index_src.depth, False) target_select[depth_level] = True group_select = ~target_select group_arrays = [] target_arrays = [] for i, v in enumerate(target_select): if v: target_arrays.append(index_src.values_at_depth(i)) else: group_arrays.append(index_src.values_at_depth(i)) group_depth = len(group_arrays) target_depth = len(target_arrays) group_to_dtype: tp.Dict[tp.Optional[tp.Hashable], np.dtype] = {} targets_unique: tp.Iterable[tp.Hashable] if group_depth == 0: # targets must be a tuple group_to_target_map = { None: {v: idx for idx, v in enumerate(zip(*target_arrays))} } targets_unique = [k for k in group_to_target_map[None]] if dtypes_src is not None: group_to_dtype[None] = resolve_dtype_iter(dtypes_src) else: group_to_target_map = defaultdict(dict) targets_unique = dict() # Store targets in order observed for axis_idx, (group, target, dtype) in enumerate( zip( zip(*group_arrays), # get tuples of len 1 to depth zip(*target_arrays), (dtypes_src if dtypes_src is not None else repeat(None)), )): if group_depth == 1: group = group[0] # targets are transfered labels; groups are the new columns group_to_target_map[group][target] = axis_idx targets_unique[target] = None #type: ignore if dtypes_src is not None: if group in group_to_dtype: group_to_dtype[group] = resolve_dtype( group_to_dtype[group], dtype) else: group_to_dtype[group] = dtype return PivotIndexMap( #pylint: disable=E1120 targets_unique=targets_unique, target_depth=target_depth, target_select=target_select, group_to_target_map=group_to_target_map, #type: ignore group_depth=group_depth, group_select=group_select, group_to_dtype=group_to_dtype)
def test_resolve_dtype(self, dtype_pair): x = util.resolve_dtype(*dtype_pair) self.assertTrue(isinstance(x, np.dtype))