How to use the csvkit.typeinference.normalize_column_type function in csvkit

To help you get started, we’ve selected a few csvkit examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github wireservice / csvkit / tests / test_typeinference.py View on Github external
def test_strings_with_blanks(self):
        self.assertEqual((six.text_type, [u'A', u'', u'C', None]), typeinference.normalize_column_type([u'A', u'', u'C', None], blanks_as_nulls=False))
github wireservice / csvkit / tests / test_typeinference.py View on Github external
def test_ints_floats(self):
        self.assertEqual((float, [1.01, -87, 418000000, None]), typeinference.normalize_column_type([u'1.01', u'-87', u'418000000', u'']))
github wireservice / csvkit / tests / test_typeinference.py View on Github external
def test_datetimes_coerce(self):
        self.assertEqual((datetime.datetime, [datetime.datetime(2008, 1, 1, 4, 40, 0), datetime.datetime(2010, 1, 27, 3, 45, 0), datetime.datetime(2008, 3, 1, 16, 14, 45), None]), typeinference.normalize_column_type([u'Jan 1, 2008 at 4:40 AM', u'2010-01-27T03:45:00', u'3/1/08 16:14:45', u''], normal_type=datetime.datetime))
github wireservice / csvkit / tests / test_typeinference.py View on Github external
def test_comma_ints(self):
        self.assertEqual((int, [1, -87, 418000000, None]), typeinference.normalize_column_type([u'1', u'-87', u'418,000,000', u'']))
github wireservice / csvkit / tests / test_typeinference.py View on Github external
def test_datetimes_coerce_fail(self):
        try:
            typeinference.normalize_column_type([u'Jan 1, 2008 at 4:40 AM', u'2010-01-27T03:45:00', u'3/1/08 16:14:45', u'4:45 AM'], normal_type=datetime.datetime)
        except InvalidValueForTypeException as e:
            self.assertEqual(e.index, 3)
            self.assertEqual(e.value, '4:45 AM')
            self.assertEqual(e.normal_type, datetime.datetime)
        else:
            raise AssertionError('Expected InvalidValueForTypeException')
github wireservice / csvkit / tests / test_typeinference.py View on Github external
def test_ints_coerce(self):
        self.assertEqual((int, [1, -87, 418000000, None]), typeinference.normalize_column_type([u'1', u'-87', u'418000000', u''], normal_type=int))
github wireservice / csvkit / tests / test_typeinference.py View on Github external
def test_floats_coerce_fail(self):
        try:
            typeinference.normalize_column_type([u'1', u'-87.413', u'418000000.0', u'Hello, world!'], normal_type=float)
        except InvalidValueForTypeException as e:
            self.assertEqual(e.index, 3)
            self.assertEqual(e.value, 'Hello, world!')
            self.assertEqual(e.normal_type, float)
        else:
            raise AssertionError('Expected InvalidValueForTypeException')
github wireservice / csvkit / tests / test_typeinference.py View on Github external
def test_dates_coerce_fail(self):
        try:
            typeinference.normalize_column_type([u'Jan 1, 2008 at 4:40 AM', u'2010-01-27T03:45:00', u'3/1/08 16:14:45', u'4:45 AM'], normal_type=datetime.datetime)
        except InvalidValueForTypeException as e:
            self.assertEqual(e.index, 3)
            self.assertEqual(e.value, '4:45 AM')
            self.assertEqual(e.normal_type, datetime.datetime)
        else:
            raise AssertionError('Expected InvalidValueForTypeException')
github wireservice / csvkit / tests / test_typeinference.py View on Github external
def test_floats(self):
        self.assertEqual((float, [1.01, -87.413, 418000000.0, None]), typeinference.normalize_column_type([u'1.01', u'-87.413', u'418000000.0', u'']))
github wireservice / csvkit / csvkit / table.py View on Github external
def __init__(self, order, name, l, normal_type=InvalidType, blanks_as_nulls=True, infer_types=True):
        """
        Construct a column from a sequence of values.

        If normal_type is not InvalidType, inference will be skipped and values assumed to have already been normalized.
        If infer_types is False, type inference will be skipped and the type assumed to be unicode.
        """
        if normal_type != InvalidType:
            t = normal_type
            data = l
        elif not infer_types:
            t = six.text_type
            data = l
        else:
            t, data = typeinference.normalize_column_type(l, blanks_as_nulls=blanks_as_nulls)

        list.__init__(self, data)
        self.order = order
        self.name = name or '_unnamed'  # empty column names don't make sense
        self.type = t