Last updated: 2023-03-16.
tf_quant_finance.datetime.DateTensor#
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 Jan1 Jan 0001 has
ordinal=1.years,monthsanddaysmust represent the same dates asordinals.
years: Tensor of type int32, of same shape asordinals.months: Tensor of type int32, of same shape asordinalsdays: Tensor of type int32, of same shape asordinals.
Attributes:#
rankshape
Methods#
__add__
__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: APeriodTensorobject 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__
__eq__(
other
)
Compares two DateTensors by “==”, returning a Tensor of bools.
__ge__
__ge__(
other
)
Compares two DateTensors by “>=”, returning a Tensor of bools.
__getitem__
__getitem__(
key
)
Implements indexing.
__gt__
__gt__(
other
)
Compares two DateTensors by “>”, returning a Tensor of bools.
__le__
__le__(
other
)
Compares two DateTensors by “<=”, returning a Tensor of bools.
__lt__
__lt__(
other
)
Compares two DateTensors by “<”, returning a Tensor of bools.
__ne__
__ne__(
other
)
Compares two DateTensors by “!=”, returning a Tensor of bools.
__sub__
__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
boolean_mask(
mask, axis=None
)
See tf.boolean_mask.
broadcast_to
broadcast_to(
shape
)
See tf.broadcast_to.
concat
@classmethod
concat(
cls, tensor_wrappers, axis
)
See tf.concat.
day
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
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
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
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
expand_dims(
axis
)
See tf.expand_dims.
identity
identity()
See tf.identity.
is_end_of_month
is_end_of_month()
Returns a bool Tensor indicating whether dates are at ends of months.
month
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
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
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
reshape(
shape
)
See tf.reshape.
squeeze
squeeze(
axis=None
)
See tf.squeeze.
stack
@classmethod
stack(
cls, tensor_wrappers, axis=0
)
See tf.stack.
to_end_of_month
to_end_of_month()
Returns a new DateTensor with each date shifted to the end of month.
to_tensor
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
transpose(
perm=None
)
See tf.transpose.
where
@classmethod
where(
cls, condition, tensor_wrapper_1, tensor_wrapper_2
)
See tf.where. Only three-argument version is supported here.
year
year()
Returns an int32 tensor of years.
Example#
dates = tff.datetime.dates_from_tuples([(2019, 1, 25), (2020, 3, 2)])
dates.year() # [2019, 2020]