def convert_to(self, typecode): converted = None if typecode == utils.type_code_byte_column(): vals = np.empty([self._values.shape[0]], dtype=np.int8) for i, x in np.ndenumerate(self._values): vals[i] = int(chr(x)) if x is not None else 0 converted = dataframe.DataFrame.ByteColumn(values=vals) elif typecode == utils.type_code_short_column(): vals = np.empty([self._values.shape[0]], dtype=np.int16) for i, x in np.ndenumerate(self._values): vals[i] = int(chr(x)) if x is not None else 0 converted = dataframe.DataFrame.ShortColumn(values=vals) elif typecode == utils.type_code_int_column(): vals = np.empty([self._values.shape[0]], dtype=np.int32) for i, x in np.ndenumerate(self._values): vals[i] = int(chr(x)) if x is not None else 0 converted = dataframe.DataFrame.IntColumn(values=vals) elif typecode == utils.type_code_long_column(): vals = np.empty([self._values.shape[0]], dtype=np.int64) for i, x in np.ndenumerate(self._values): vals[i] = int(chr(x)) if x is not None else 0 converted = dataframe.DataFrame.LongColumn(values=vals) elif typecode == utils.type_code_string_column(): vals = np.empty([self._values.shape[0]], dtype=np.object) for i, x in np.ndenumerate(self._values): vals[i] = chr( x) if x is not None else utils.default_value_string_column( ) converted = dataframe.DataFrame.StringColumn(values=vals) elif typecode == utils.type_code_float_column(): vals = np.empty([self._values.shape[0]], dtype=np.float32) for i, x in np.ndenumerate(self._values): vals[i] = float(chr(x)) if x is not None else 0.0 converted = dataframe.DataFrame.FloatColumn(values=vals) elif typecode == utils.type_code_double_column(): vals = np.empty([self._values.shape[0]], dtype=np.float64) for i, x in np.ndenumerate(self._values): vals[i] = float(chr(x)) if x is not None else 0.0 converted = dataframe.DataFrame.DoubleColumn(values=vals) elif typecode == utils.type_code_char_column(): vals = np.empty([self._values.shape[0]], dtype=np.uint8) ord_default = ord(CharColumn.DEFAULT_VALUE) for i, x in np.ndenumerate(self._values): vals[i] = x if x is not None else ord_default converted = dataframe.DataFrame.CharColumn(values=vals) elif typecode == utils.type_code_boolean_column(): values_true = {"t", "1", "y"} values_false = {"f", "0", "n"} vals = np.empty([self._values.shape[0]], dtype=np.bool) for i, x in np.ndenumerate(self._values): if x is not None: x = chr(x).lower() is_true = x in values_true is_false = x in values_false if not is_true and not is_false: raise dataframe.DataFrameException( ("Invalid boolean character: '{}'".format( self._values[i]))) vals[i] = is_true else: vals[i] = False converted = dataframe.DataFrame.BooleanColumn(values=vals) elif typecode == utils.type_code_binary_column(): vals = np.empty([self._values.shape[0]], dtype=np.object) for i, x in np.ndenumerate(self._values): if x is not None: vals[i] = bytearray(chr(x).encode("utf-8")) else: vals[i] = bytearray.fromhex("00") converted = dataframe.DataFrame.BinaryColumn(values=vals) elif typecode == utils.type_code_nullable_byte_column(): vals = np.empty([self._values.shape[0]], dtype=np.object) for i, x in np.ndenumerate(self._values): vals[i] = int(chr(x)) if x is not None else None converted = dataframe.DataFrame.NullableByteColumn(values=vals) elif typecode == utils.type_code_nullable_short_column(): vals = np.empty([self._values.shape[0]], dtype=np.object) for i, x in np.ndenumerate(self._values): vals[i] = int(chr(x)) if x is not None else None converted = dataframe.DataFrame.NullableShortColumn(values=vals) elif typecode == utils.type_code_nullable_int_column(): vals = np.empty([self._values.shape[0]], dtype=np.object) for i, x in np.ndenumerate(self._values): vals[i] = int(chr(x)) if x is not None else None converted = dataframe.DataFrame.NullableIntColumn(values=vals) elif typecode == utils.type_code_nullable_long_column(): vals = np.empty([self._values.shape[0]], dtype=np.object) for i, x in np.ndenumerate(self._values): vals[i] = int(chr(x)) if x is not None else None converted = dataframe.DataFrame.NullableLongColumn(values=vals) elif typecode == utils.type_code_nullable_string_column(): vals = np.empty([self._values.shape[0]], dtype=np.object) for i, x in np.ndenumerate(self._values): vals[i] = chr(x) if x is not None else None converted = dataframe.DataFrame.NullableStringColumn(values=vals) elif typecode == utils.type_code_nullable_float_column(): vals = np.empty([self._values.shape[0]], dtype=np.object) for i, x in np.ndenumerate(self._values): vals[i] = float(chr(x)) if x is not None else None converted = dataframe.DataFrame.NullableFloatColumn(values=vals) elif typecode == utils.type_code_nullable_double_column(): vals = np.empty([self._values.shape[0]], dtype=np.object) for i, x in np.ndenumerate(self._values): vals[i] = float(chr(x)) if x is not None else None converted = dataframe.DataFrame.NullableDoubleColumn(values=vals) elif typecode == NullableCharColumn.TYPE_CODE: converted = self.clone() elif typecode == utils.type_code_nullable_boolean_column(): values_true = {"t", "1", "y"} values_false = {"f", "0", "n"} vals = np.empty([self._values.shape[0]], dtype=np.object) for i, x in np.ndenumerate(self._values): if x is not None: x = chr(x).lower() is_true = x in values_true is_false = x in values_false if not is_true and not is_false: raise dataframe.DataFrameException( ("Invalid boolean character: '{}'".format( self._values[i]))) vals[i] = is_true else: vals[i] = None converted = dataframe.DataFrame.NullableBooleanColumn(values=vals) elif typecode == utils.type_code_nullable_binary_column(): vals = np.empty([self._values.shape[0]], dtype=np.object) for i, x in np.ndenumerate(self._values): if x is not None: vals[i] = bytearray(chr(x).encode("utf-8")) else: vals[i] = None converted = dataframe.DataFrame.NullableBinaryColumn(values=vals) else: raise dataframe.DataFrameException( "Unknown column type code: {}".format(typecode)) # pylint: disable=protected-access converted._name = self._name return converted
def convert_to(self, typecode): converted = None if typecode == utils.type_code_byte_column(): converted = dataframe.DataFrame.ByteColumn( values=self._values.astype(np.int8)) elif typecode == utils.type_code_short_column(): converted = dataframe.DataFrame.ShortColumn( values=self._values.astype(np.int16)) elif typecode == utils.type_code_int_column(): converted = dataframe.DataFrame.IntColumn( values=self._values.astype(np.int32)) elif typecode == LongColumn.TYPE_CODE: converted = self.clone() elif typecode == utils.type_code_string_column(): vals = self._values.astype(np.object) for i, x in np.ndenumerate(vals): vals[i] = str(x) converted = dataframe.DataFrame.StringColumn(values=vals) elif typecode == utils.type_code_float_column(): converted = dataframe.DataFrame.FloatColumn( values=self._values.astype(np.float32)) elif typecode == utils.type_code_double_column(): converted = dataframe.DataFrame.DoubleColumn( values=self._values.astype(np.float64)) elif typecode == utils.type_code_char_column(): vals = self._values.astype(np.uint8) for i, x in np.ndenumerate(vals): vals[i] = ord(str(x)[0]) converted = dataframe.DataFrame.CharColumn(values=vals) elif typecode == utils.type_code_boolean_column(): converted = dataframe.DataFrame.BooleanColumn( values=self._values.astype(np.bool)) elif typecode == utils.type_code_binary_column(): vals = np.empty([self._values.shape[0]], dtype=np.object) for i, x in np.ndenumerate(self._values): vals[i] = bytearray( int(x).to_bytes(8, byteorder="big", signed=True)) converted = dataframe.DataFrame.BinaryColumn(values=vals) elif typecode == utils.type_code_nullable_byte_column(): vals = self._values.astype(np.int8) converted = dataframe.DataFrame.NullableByteColumn( values=vals.astype(np.object)) elif typecode == utils.type_code_nullable_short_column(): vals = self._values.astype(np.int16) converted = dataframe.DataFrame.NullableShortColumn( values=vals.astype(np.object)) elif typecode == utils.type_code_nullable_int_column(): vals = self._values.astype(np.int32) converted = dataframe.DataFrame.NullableIntColumn( values=vals.astype(np.object)) elif typecode == NullableLongColumn.TYPE_CODE: converted = NullableLongColumn( values=self._values.astype(np.object)) elif typecode == utils.type_code_nullable_string_column(): vals = self._values.astype(np.object) for i, x in np.ndenumerate(vals): vals[i] = str(x) converted = dataframe.DataFrame.NullableStringColumn(values=vals) elif typecode == utils.type_code_nullable_float_column(): vals = self._values.astype(np.float32) vals = vals.astype(np.object) converted = dataframe.DataFrame.NullableFloatColumn(values=vals) elif typecode == utils.type_code_nullable_double_column(): vals = self._values.astype(np.float64) vals = vals.astype(np.object) converted = dataframe.DataFrame.NullableDoubleColumn(values=vals) elif typecode == utils.type_code_nullable_char_column(): vals = self._values.astype(np.object) for i, x in np.ndenumerate(vals): vals[i] = ord(str(x)[0]) converted = dataframe.DataFrame.NullableCharColumn(values=vals) elif typecode == utils.type_code_nullable_boolean_column(): vals = self._values.astype(np.bool) vals = vals.astype(np.object) converted = dataframe.DataFrame.NullableBooleanColumn(values=vals) elif typecode == utils.type_code_nullable_binary_column(): vals = np.empty([self._values.shape[0]], dtype=np.object) for i, x in np.ndenumerate(self._values): vals[i] = bytearray( int(x).to_bytes(8, byteorder="big", signed=True)) converted = dataframe.DataFrame.NullableBinaryColumn(values=vals) else: raise dataframe.DataFrameException( "Unknown column type code: {}".format(typecode)) # pylint: disable=protected-access converted._name = self._name return converted
def convert_to(self, typecode): converted = None if typecode == utils.type_code_byte_column(): vals = np.empty([self._values.shape[0]], dtype=np.int8) for i, x in np.ndenumerate(self._values): if x is not None: vals[i] = int(np.int8(x)) else: vals[i] = 0 converted = dataframe.DataFrame.ByteColumn(values=vals) elif typecode == ShortColumn.TYPE_CODE: vals = np.empty([self._values.shape[0]], dtype=np.int16) for i, x in np.ndenumerate(self._values): if x is not None: vals[i] = int(np.int16(x)) else: vals[i] = 0 converted = ShortColumn(values=vals) elif typecode == utils.type_code_int_column(): vals = np.empty([self._values.shape[0]], dtype=np.int32) for i, x in np.ndenumerate(self._values): if x is not None: vals[i] = int(np.int32(x)) else: vals[i] = 0 converted = dataframe.DataFrame.IntColumn(values=vals) elif typecode == utils.type_code_long_column(): vals = np.empty([self._values.shape[0]], dtype=np.int64) for i, x in np.ndenumerate(self._values): if x is not None: vals[i] = int(np.int64(x)) else: vals[i] = 0 converted = dataframe.DataFrame.LongColumn(values=vals) elif typecode == utils.type_code_string_column(): vals = np.empty([self._values.shape[0]], dtype=np.object) for i, x in np.ndenumerate(self._values): if x is not None: vals[i] = str(x) else: vals[i] = utils.default_value_string_column() converted = dataframe.DataFrame.StringColumn(values=vals) elif typecode == utils.type_code_float_column(): vals = np.empty([self._values.shape[0]], dtype=np.float32) for i, x in np.ndenumerate(self._values): if x is not None: vals[i] = float(x) else: vals[i] = 0.0 converted = dataframe.DataFrame.FloatColumn(values=vals) elif typecode == utils.type_code_double_column(): vals = np.empty([self._values.shape[0]], dtype=np.float64) for i, x in np.ndenumerate(self._values): if x is not None: vals[i] = float(x) else: vals[i] = 0.0 converted = dataframe.DataFrame.DoubleColumn(values=vals) elif typecode == utils.type_code_char_column(): vals = np.zeros([self._values.shape[0]], dtype=np.uint8) ord_default = ord(utils.default_value_char_column()) for i, x in np.ndenumerate(self._values): if x is not None: vals[i] = ord(str(x)[0]) else: vals[i] = ord_default converted = dataframe.DataFrame.CharColumn(values=vals) elif typecode == utils.type_code_boolean_column(): vals = np.empty([self._values.shape[0]], dtype=np.bool) for i, x in np.ndenumerate(self._values): if x is not None: vals[i] = (x != 0) else: vals[i] = False converted = dataframe.DataFrame.BooleanColumn(values=vals) elif typecode == utils.type_code_binary_column(): vals = np.empty([self._values.shape[0]], dtype=np.object) for i, x in np.ndenumerate(self._values): if x is not None: vals[i] = bytearray( int(x).to_bytes(2, byteorder="big", signed=True)) else: vals[i] = bytearray( int(0).to_bytes(2, byteorder="big", signed=True)) converted = dataframe.DataFrame.BinaryColumn(values=vals) elif typecode == utils.type_code_nullable_byte_column(): vals = np.empty([self._values.shape[0]], dtype=np.object) for i, x in np.ndenumerate(self._values): vals[i] = int(np.int8(x)) if x is not None else None converted = dataframe.DataFrame.NullableByteColumn(values=vals) elif typecode == NullableShortColumn.TYPE_CODE: converted = self.clone() elif typecode == utils.type_code_nullable_int_column(): vals = np.empty([self._values.shape[0]], dtype=np.object) for i, x in np.ndenumerate(self._values): vals[i] = int(np.int32(x)) if x is not None else None converted = dataframe.DataFrame.NullableIntColumn(values=vals) elif typecode == utils.type_code_nullable_long_column(): vals = np.empty([self._values.shape[0]], dtype=np.object) for i, x in np.ndenumerate(self._values): vals[i] = int(np.int64(x)) if x is not None else None converted = dataframe.DataFrame.NullableLongColumn(values=vals) elif typecode == utils.type_code_nullable_string_column(): vals = np.empty([self._values.shape[0]], dtype=np.object) for i, x in np.ndenumerate(self._values): if x is not None: vals[i] = str(x) else: vals[i] = None converted = dataframe.DataFrame.NullableStringColumn(values=vals) elif typecode == utils.type_code_nullable_float_column(): vals = np.empty([self._values.shape[0]], dtype=np.object) for i, x in np.ndenumerate(self._values): if x is not None: vals[i] = float(x) else: vals[i] = None converted = dataframe.DataFrame.NullableFloatColumn(values=vals) elif typecode == utils.type_code_nullable_double_column(): vals = np.empty([self._values.shape[0]], dtype=np.object) for i, x in np.ndenumerate(self._values): if x is not None: vals[i] = float(x) else: vals[i] = None converted = dataframe.DataFrame.NullableDoubleColumn(values=vals) elif typecode == utils.type_code_nullable_char_column(): vals = np.empty([self._values.shape[0]], dtype=np.object) for i, x in np.ndenumerate(self._values): if x is not None: vals[i] = ord(str(x)[0]) else: vals[i] = None converted = dataframe.DataFrame.NullableCharColumn(values=vals) elif typecode == utils.type_code_nullable_boolean_column(): vals = np.empty([self._values.shape[0]], dtype=np.object) for i, x in np.ndenumerate(self._values): if x is not None: vals[i] = (x != 0) else: vals[i] = None converted = dataframe.DataFrame.NullableBooleanColumn(values=vals) elif typecode == utils.type_code_nullable_binary_column(): vals = np.empty([self._values.shape[0]], dtype=np.object) for i, x in np.ndenumerate(self._values): if x is not None: vals[i] = bytearray( int(x).to_bytes(2, byteorder="big", signed=True)) else: vals[i] = None converted = dataframe.DataFrame.NullableBinaryColumn(values=vals) else: raise dataframe.DataFrameException( "Unknown column type code: {}".format(typecode)) # pylint: disable=protected-access converted._name = self._name return converted
def convert_to(self, typecode): converted = None if typecode == utils.type_code_byte_column(): vals = np.empty([self._values.shape[0]], dtype=np.int8) for i, x in np.ndenumerate(self._values): vals[i] = int(np.int8(x)) converted = dataframe.DataFrame.ByteColumn(values=vals) elif typecode == utils.type_code_short_column(): vals = np.empty([self._values.shape[0]], dtype=np.int16) for i, x in np.ndenumerate(self._values): vals[i] = int(np.int16(x)) converted = dataframe.DataFrame.ShortColumn(values=vals) elif typecode == utils.type_code_int_column(): vals = np.empty([self._values.shape[0]], dtype=np.int32) for i, x in np.ndenumerate(self._values): vals[i] = int(np.int32(x)) converted = dataframe.DataFrame.IntColumn(values=vals) elif typecode == utils.type_code_long_column(): vals = np.empty([self._values.shape[0]], dtype=np.int64) for i, x in np.ndenumerate(self._values): vals[i] = int(np.int64(x)) converted = dataframe.DataFrame.LongColumn(values=vals) elif typecode == StringColumn.TYPE_CODE: converted = self.clone() elif typecode == utils.type_code_float_column(): vals = np.empty([self._values.shape[0]], dtype=np.float32) for i, x in np.ndenumerate(self._values): vals[i] = float(np.float32(x)) converted = dataframe.DataFrame.FloatColumn(values=vals) elif typecode == utils.type_code_double_column(): vals = np.empty([self._values.shape[0]], dtype=np.float64) for i, x in np.ndenumerate(self._values): vals[i] = float(np.float64(x)) converted = dataframe.DataFrame.DoubleColumn(values=vals) elif typecode == utils.type_code_char_column(): vals = np.empty([self._values.shape[0]], dtype=np.uint8) for i, x in np.ndenumerate(self._values): if x is not None: vals[i] = ord(x[0]) else: vals[i] = utils.default_value_char_column() converted = dataframe.DataFrame.CharColumn(values=vals) elif typecode == utils.type_code_boolean_column(): values_true = {"true", "t", "1", "yes", "y", "on"} values_false = {"false", "f", "0", "no", "n", "off"} vals = np.empty([self._values.shape[0]], dtype=np.bool) for i, x in np.ndenumerate(self._values): if x is not None: x = x.lower() is_true = x in values_true is_false = x in values_false if not is_true and not is_false: raise dataframe.DataFrameException( ("Invalid boolean string: '{}'".format(self._values[i]))) vals[i] = is_true else: vals[i] = False converted = dataframe.DataFrame.BooleanColumn(values=vals) elif typecode == utils.type_code_binary_column(): vals = np.empty([self._values.shape[0]], dtype=np.object) for i, x in np.ndenumerate(self._values): if x is not None: vals[i] = bytearray.fromhex(x) else: vals[i] = bytearray(b'\x00') converted = dataframe.DataFrame.BinaryColumn(values=vals) elif typecode == utils.type_code_nullable_byte_column(): vals = np.empty([self._values.shape[0]], dtype=np.object) for i, x in np.ndenumerate(self._values): if x is not None: vals[i] = int(np.int8(x)) else: vals[i] = None converted = dataframe.DataFrame.NullableByteColumn(values=vals) elif typecode == utils.type_code_nullable_short_column(): vals = np.empty([self._values.shape[0]], dtype=np.object) for i, x in np.ndenumerate(self._values): if x is not None: vals[i] = int(np.int16(x)) else: vals[i] = None converted = dataframe.DataFrame.NullableShortColumn(values=vals) elif typecode == utils.type_code_nullable_int_column(): vals = np.empty([self._values.shape[0]], dtype=np.object) for i, x in np.ndenumerate(self._values): if x is not None: vals[i] = int(np.int32(x)) else: vals[i] = None converted = dataframe.DataFrame.NullableIntColumn(values=vals) elif typecode == utils.type_code_nullable_long_column(): vals = np.empty([self._values.shape[0]], dtype=np.object) for i, x in np.ndenumerate(self._values): if x is not None: vals[i] = int(np.int64(x)) else: vals[i] = None converted = dataframe.DataFrame.NullableLongColumn(values=vals) elif typecode == NullableStringColumn.TYPE_CODE: vals = np.empty([self._values.shape[0]], dtype=np.object) for i, x in np.ndenumerate(vals): vals[i] = x converted = NullableStringColumn(values=vals) elif typecode == utils.type_code_nullable_float_column(): vals = np.empty([self._values.shape[0]], dtype=np.object) for i, x in np.ndenumerate(self._values): if x is not None: vals[i] = float(np.float32(x)) else: vals[i] = None converted = dataframe.DataFrame.NullableFloatColumn(values=vals) elif typecode == utils.type_code_nullable_double_column(): vals = np.empty([self._values.shape[0]], dtype=np.object) for i, x in np.ndenumerate(self._values): if x is not None: vals[i] = float(np.float64(x)) else: vals[i] = None converted = dataframe.DataFrame.NullableDoubleColumn(values=vals) elif typecode == utils.type_code_nullable_char_column(): vals = np.empty([self._values.shape[0]], dtype=np.object) for i, x in np.ndenumerate(self._values): if x is not None: vals[i] = ord(x[0]) else: vals[i] = None converted = dataframe.DataFrame.NullableCharColumn(values=vals) elif typecode == utils.type_code_nullable_boolean_column(): values_true = {"true", "t", "1", "yes", "y", "on"} values_false = {"false", "f", "0", "no", "n", "off"} vals = np.empty([self._values.shape[0]], dtype=np.object) for i, x in np.ndenumerate(self._values): if x is not None: x = x.lower() is_true = x in values_true is_false = x in values_false if not is_true and not is_false: raise dataframe.DataFrameException( ("Invalid boolean string: '{}'".format(self._values[i]))) vals[i] = is_true else: vals[i] = None converted = dataframe.DataFrame.NullableBooleanColumn(values=vals) elif typecode == utils.type_code_nullable_binary_column(): vals = np.empty([self._values.shape[0]], dtype=np.object) for i, x in np.ndenumerate(self._values): if x is not None: vals[i] = bytearray.fromhex(x) else: vals[i] = None converted = dataframe.DataFrame.NullableBinaryColumn(values=vals) else: raise dataframe.DataFrameException( "Unknown column type code: {}".format(typecode)) # pylint: disable=protected-access converted._name = self._name return converted