Data types

Data types define how data should be imported during the creation of a Table.

If column types are not explicitly specified when a Table is created, agate will attempt to guess them. The TypeTester class can be used to control how types are guessed.

agate.DataType Specifies how values should be parsed when creating a Table.

Supported types

agate.Text Data representing text.
agate.Number Data representing numbers.
agate.Boolean Data representing true and false.
agate.Date Data representing dates alone.
agate.DateTime Data representing dates with times.
agate.TimeDelta Data representing the interval between two dates and/or times.

Detailed list

class agate.DataType(null_values=('', 'na', 'n/a', 'none', 'null', '.'))

Bases: object

Specifies how values should be parsed when creating a Table.

Parameters:null_values – A sequence of values which should be cast to None when encountered by this data type.
cast(d)

Coerce a given string value into this column’s data type.

csvify(d)

Format a given native value for CSV serialization.

jsonify(d)

Format a given native value for JSON serialization.

test(d)

Test, for purposes of type inference, if a value could possibly be coerced to this data type.

This is really just a thin wrapper around DataType.cast().

class agate.Text(cast_nulls=True, **kwargs)

Bases: agate.data_types.base.DataType

Data representing text.

Parameters:cast_nulls – If True, values in DEFAULT_NULL_VALUES will be converted to None. Disable to retain them as strings.
cast(d)

Cast a single value to unicode() (str() in Python 3).

Parameters:d – A value to cast.
Returns:unicode() (str() in Python 3) or None
class agate.Number(locale='en_US', group_symbol=None, decimal_symbol=None, currency_symbols=[u'u060b', u'$', u'u0192', u'u17db', u'xa5', u'u20a1', u'u20b1', u'xa3', u'u20ac', u'xa2', u'ufdfc', u'u20aa', u'u20a9', u'u20ad', u'u20ae', u'u20a6', u'u0e3f', u'u20a4', u'u20ab'], **kwargs)

Bases: agate.data_types.base.DataType

Data representing numbers.

Parameters:
  • locale – A locale specification such as en_US or de_DE to use for parsing formatted numbers.
  • group_symbol – A grouping symbol used in the numbers. Overrides the value provided by the specified locale.
  • decimal_symbol – A decimal separate symbol used in the numbers. Overrides the value provided by the specified locale.
  • currency_symbols – A sequence of currency symbols to strip from numbers.
cast(d)

Cast a single value to a decimal.Decimal.

Returns:decimal.Decimal or None.
class agate.Boolean(true_values=('yes', 'y', 'true', 't', '1'), false_values=('no', 'n', 'false', 'f', '0'), null_values=('', 'na', 'n/a', 'none', 'null', '.'))

Bases: agate.data_types.base.DataType

Data representing true and false.

Note that by default numerical 1 and 0 are considered valid boolean values, but other numbers are not.

Parameters:
  • true_values – A sequence of values which should be cast to True when encountered with this type.
  • false_values – A sequence of values which should be cast to False when encountered with this type.
cast(d)

Cast a single value to bool.

Parameters:d – A value to cast.
Returns:bool or None.
class agate.Date(date_format=None, **kwargs)

Bases: agate.data_types.base.DataType

Data representing dates alone.

Parameters:date_format – A formatting string for datetime.datetime.strptime() to use instead of using regex-based parsing.
cast(d)

Cast a single value to a datetime.date.

Parameters:date_format – An optional datetime.strptime() format string for parsing datetimes in this column.
Returns:datetime.date or None.
class agate.DateTime(datetime_format=None, timezone=None, **kwargs)

Bases: agate.data_types.base.DataType

Data representing dates with times.

Parameters:
  • datetime_format – A formatting string for datetime.datetime.strptime() to use instead of using regex-based parsing.
  • timezone – A pytz timezone to apply to each parsed date.
cast(d)

Cast a single value to a datetime.datetime.

Parameters:datetime_format – An optional datetime.strptime() format string for parsing datetimes in this column.
Returns:datetime.datetime or None.
class agate.TimeDelta(null_values=('', 'na', 'n/a', 'none', 'null', '.'))

Bases: agate.data_types.base.DataType

Data representing the interval between two dates and/or times.

cast(d)

Cast a single value to datetime.timedelta.

Parameters:d – A value to cast.
Returns:datetime.timedelta or None