Last updated: 2023-03-16.

tf_quant_finance.datetime.DateTensor#

View source

Represents a tensor of dates.

tf_quant_finance.datetime.DateTensor(
    ordinals, years, months, days
)

Args:#

  • ordinals: Tensor of type int32. Each value is number of days since 1 Jan

    1. 1 Jan 0001 has ordinal=1. years, months and days must represent the same dates as ordinals.

  • years: Tensor of type int32, of same shape as ordinals.

  • months: Tensor of type int32, of same shape as ordinals

  • days: Tensor of type int32, of same shape as ordinals.

Attributes:#

  • rank

  • shape

Methods#

__add__

View source

__add__(
    period_tensor
)

Adds a tensor of periods.

When adding months or years, the resulting day of the month is decreased to the largest valid value if necessary. E.g. 31.03.2020 + 1 month = 30.04.2020, 29.02.2020 + 1 year = 28.02.2021.

Args:#

  • period_tensor: A PeriodTensor object broadcastable to the shape of “self”.

Returns:#

The new instance of DateTensor.

Example#

dates = tff.datetime.dates_from_tuples([(2020, 2, 25), (2020, 3, 31)])
new_dates = dates + tff.datetime.month()
# DateTensor([(2020, 3, 25), (2020, 4, 30)])

new_dates = dates + tff.datetime.month([1, 2])
# DateTensor([(2020, 3, 25), (2020, 5, 31)])

__eq__

View source

__eq__(
    other
)

Compares two DateTensors by “==”, returning a Tensor of bools.

__ge__

View source

__ge__(
    other
)

Compares two DateTensors by “>=”, returning a Tensor of bools.

__getitem__

View source

__getitem__(
    key
)

Implements indexing.

__gt__

View source

__gt__(
    other
)

Compares two DateTensors by “>”, returning a Tensor of bools.

__le__

View source

__le__(
    other
)

Compares two DateTensors by “<=”, returning a Tensor of bools.

__lt__

View source

__lt__(
    other
)

Compares two DateTensors by “<”, returning a Tensor of bools.

__ne__

View source

__ne__(
    other
)

Compares two DateTensors by “!=”, returning a Tensor of bools.

__sub__

View source

__sub__(
    period_tensor
)

Subtracts a tensor of periods.

When subtracting months or years, the resulting day of the month is decreased to the largest valid value if necessary. E.g. 31.03.2020 - 1 month = 29.02.2020, 29.02.2020 - 1 year = 28.02.2019.

Args:#

  • period_tensor: a PeriodTensor object broadcastable to the shape of “self”.

Returns:#

The new instance of DateTensor.

boolean_mask

View source

boolean_mask(
    mask, axis=None
)

See tf.boolean_mask.

broadcast_to

View source

broadcast_to(
    shape
)

See tf.broadcast_to.

concat

View source

@classmethod
concat(
    cls, tensor_wrappers, axis
)

See tf.concat.

day

View source

day()

Returns an int32 tensor of days since the beginning the month.

The result is one-based, i.e. yields 1 for first day of the month.

Example#

dates = tff.datetime.dates_from_tuples([(2019, 1, 25), (2020, 3, 2)])
dates.day()  # [25, 2]

day_of_week

View source

day_of_week()

Returns an int32 tensor of weekdays.

The result is zero-based according to Python datetime convention, i.e. Monday is “0”.

Example#

dates = tff.datetime.dates_from_tuples([(2019, 1, 25), (2020, 3, 2)])
dates.day_of_week()  # [5, 1]

day_of_year

View source

day_of_year()

Calculates the number of days since the beginning of the year.

Returns:#

Tensor of int32 type with elements in range [1, 366]. January 1st yields “1”.

Example#

dt = tff.datetime.dates_from_tuples([(2019, 1, 25), (2020, 3, 2)])
dt.day_of_year()  # [25, 62]

days_until

View source

days_until(
    target_date_tensor
)

Computes the number of days until the target dates.

Args:#

  • target_date_tensor: A DateTensor object broadcastable to the shape of “self”.

Returns:#

An int32 tensor with numbers of days until the target dates.

Example#


dates = tff.datetime.dates_from_tuples([(2020, 1, 25), (2020, 3, 2)])
target = tff.datetime.dates_from_tuples([(2020, 3, 5)])
dates.days_until(target) # [40, 3]

targets = tff.datetime.dates_from_tuples([(2020, 2, 5), (2020, 3, 5)])
dates.days_until(targets)  # [11, 3]

expand_dims

View source

expand_dims(
    axis
)

See tf.expand_dims.

identity

View source

identity()

See tf.identity.

is_end_of_month

View source

is_end_of_month()

Returns a bool Tensor indicating whether dates are at ends of months.

month

View source

month()

Returns an int32 tensor of months.

Example#

dates = tff.datetime.dates_from_tuples([(2019, 1, 25), (2020, 3, 2)])
dates.month()  # [1, 3]

ordinal

View source

ordinal()

Returns an int32 tensor of ordinals.

Ordinal is the number of days since 1st Jan 0001.

Example#

dates = tff.datetime.dates_from_tuples([(2019, 3, 25), (1, 1, 1)])
dates.ordinal()  # [737143, 1]

period_length_in_days

View source

period_length_in_days(
    period_tensor
)

Computes the number of days in each period.

Args:#

  • period_tensor: A PeriodTensor object broadcastable to the shape of “self”.

Returns:#

An int32 tensor with numbers of days each period takes.

Example#

dates = tff.datetime.dates_from_tuples([(2020, 2, 25), (2020, 3, 2)])
dates.period_length_in_days(month())  # [29, 31]

periods = tff.datetime.months([1, 2])
dates.period_length_in_days(periods)  # [29, 61]

reshape

View source

reshape(
    shape
)

See tf.reshape.

squeeze

View source

squeeze(
    axis=None
)

See tf.squeeze.

stack

View source

@classmethod
stack(
    cls, tensor_wrappers, axis=0
)

See tf.stack.

to_end_of_month

View source

to_end_of_month()

Returns a new DateTensor with each date shifted to the end of month.

to_tensor

View source

to_tensor()

Packs the dates into a single Tensor.

The Tensor has shape date_tensor.shape() + (3,), where the last dimension represents years, months and days, in this order.

This can be convenient when the dates are the final result of a computation in the graph mode: a tf.function can return date_tensor.to_tensor(), or, if one uses tf.compat.v1.Session, they can call session.run(date_tensor.to_tensor()).

Returns:#

A Tensor of shape date_tensor.shape() + (3,).

Example#

dates = tff.datetime.dates_from_tuples([(2019, 1, 25), (2020, 3, 2)])
dates.to_tensor()  # tf.Tensor with contents [[2019, 1, 25], [2020, 3, 2]].

transpose

View source

transpose(
    perm=None
)

See tf.transpose.

where

View source

@classmethod
where(
    cls, condition, tensor_wrapper_1, tensor_wrapper_2
)

See tf.where. Only three-argument version is supported here.

year

View source

year()

Returns an int32 tensor of years.

Example#

dates = tff.datetime.dates_from_tuples([(2019, 1, 25), (2020, 3, 2)])
dates.year()  # [2019, 2020]