def from_ordinals(ordinals, validate=True): """Creates DateTensor from tensors of ordinals. Args: ordinals: Tensor of type int32. Each value is number of days since 1 Jan 0001. 1 Jan 0001 has `ordinal=1`. validate: Whether to validate the dates. Returns: DateTensor object. ## Example ```python ordinals = tf.constant([ 735703, # 2015-4-12 736693 # 2017-12-30 ], dtype=tf.int32) date_tensor = from_ordinals(ordinals) ``` """ ordinals = tf.convert_to_tensor(ordinals, dtype=tf.int32) control_deps = [] if validate: control_deps.append(tf.debugging.assert_positive(ordinals)) with tf.compat.v1.control_dependencies(control_deps): ordinals = tf.identity(ordinals) with tf.compat.v1.control_dependencies(control_deps): years, months, days = date_utils.ordinal_to_year_month_day(ordinals) return DateTensor(ordinals, years, months, days)
def test_ordinal_to_year_month_day(self): date_tuples = test_data.test_dates ordinals = np.array( [datetime.date(y, m, d).toordinal() for y, m, d in date_tuples], dtype=np.int32) y, m, d = date_utils.ordinal_to_year_month_day(ordinals) result = tf.stack((y, m, d), axis=1) self.assertAllEqual(date_tuples, result)
def _get_month(ordinals): return du.ordinal_to_year_month_day(ordinals)[1]