otp.Operation.dt.date_trunc#
- date_trunc(timestamp)#
Truncates to the specified precision.
- Parameters
date_part (str | Operation | Column) – Precision to truncate datetime to. Possible values are ‘year’, ‘quarter’, ‘month’, ‘week’, ‘day’, ‘hour’, ‘minute’, ‘second’, ‘millisecond’ and ‘nanosecond’. Notice that beginning of week is considered to be Sunday.
timestamp (str | Operation | Column) – Name of the timezone, an operation or a column with it. By default, the timezone of the query will be used.
Examples
>>> data = otp.Ticks(X=[otp.dt(2020, 11, 11, 5, 4, 13, 101737, 879)] * 10, ... DATE_PART=['year', 'quarter', 'month', 'week', 'day', 'hour', ... 'minute', 'second', 'millisecond', 'nanosecond']) >>> data['TRUNCATED_X'] = data['X'].dt.date_trunc(data['DATE_PART']) >>> otp.run(data)[['X', 'TRUNCATED_X', 'DATE_PART']] X TRUNCATED_X DATE_PART 0 2020-11-11 05:04:13.101737879 2020-01-01 00:00:00.000000000 year 1 2020-11-11 05:04:13.101737879 2020-10-01 01:00:00.000000000 quarter 2 2020-11-11 05:04:13.101737879 2020-11-01 01:00:00.000000000 month 3 2020-11-11 05:04:13.101737879 2020-11-08 00:00:00.000000000 week 4 2020-11-11 05:04:13.101737879 2020-11-11 00:00:00.000000000 day 5 2020-11-11 05:04:13.101737879 2020-11-11 05:00:00.000000000 hour 6 2020-11-11 05:04:13.101737879 2020-11-11 05:04:00.000000000 minute 7 2020-11-11 05:04:13.101737879 2020-11-11 05:04:13.000000000 second 8 2020-11-11 05:04:13.101737879 2020-11-11 05:04:13.101000000 millisecond 9 2020-11-11 05:04:13.101737879 2020-11-11 05:04:13.101737879 nanosecond
Notice that there is bug when 1 hour before new year is returned for case
date_part='year'
if there was daylight saving time:>>> data = otp.Tick(X=otp.dt(2020, 5, 11, 5, 4, 13, 101737, 879)) >>> data['TRUNCATED_X'] = data['X'].dt.date_trunc('year') >>> otp.run(data)[['X', 'TRUNCATED_X']] X TRUNCATED_X 0 2020-05-11 05:04:13.101737879 2019-12-31 23:00:00