otp.dt (otp.datetime)#

class datetime(first_arg, month=None, day=None, hour=None, minute=None, second=None, microsecond=None, nanosecond=None, *, tzinfo=None, tz=None)#

Bases: onetick.py.types.AbstractTime

Class otp.datetime is used for representing date with time in onetick-py. It can be used both when specifying start and end time for queries and in column operations with onetick.py.Source. Datetime offset objects (e.g. otp.Nano, otp.Day) can be added to or subtracted from otp.datetime object.

Parameters
  • first_arg (int, str, otp.datetime, otp.date pandas.Timestamp, datetime.datetime) – If month, day and other parts of date are specified, first argument will be considered as year. Otherwise, first argument will be converted to otp.datetime.

  • month (int) – Number between 1 and 12.

  • day (int) – Number between 1 and 31.

  • hour (int, default=0) – Number between 0 and 23.

  • minute (int, default=0) – Number between 0 and 59.

  • second (int, default=0) – Number between 0 and 59.

  • microsecond (int, default=0) – Number between 0 and 999999.

  • nanosecond (int, default=0) – Number between 0 and 999.

  • tzinfo (datetime.tzinfo) – Timezone object.

  • tz (str) – Timezone name.

Note

Class otp.datetime share many methods that classes pandas.Timestamp and datetime.datetime have, but these objects are not fully interchangeable. Class otp.datetime should work in all onetick-py methods and classes, other classes should work too if documented, and may even work when not documented, but the users should not count on it.

Examples

Initialization by datetime.datetime class from standard library:

>>> otp.datetime(datetime(2019, 1, 1, 1))
2019-01-01 01:00:00

Initialization by pandas Timestamp class:

>>> otp.datetime(pd.Timestamp(2019, 1, 1, 1))
2019-01-01 01:00:00

Initialization by int timestamp:

>>> otp.datetime(1234567890)
1970-01-01 00:00:01.234567890

Initialization by params with nanoseconds:

>>> otp.datetime(2019, 1, 1, 1, 2, 3, 4, 5)
2019-01-01 01:02:03.000004005

Initialization by string:

>>> otp.datetime('2019/01/01 1:02')
2019-01-01 01:02:00

otp.dt is the alias for otp.datetime:

>>> otp.dt(2019, 1, 1)
2019-01-01 00:00:00
replace(**kwargs)#

Replace parts of otp.datetime object.

Parameters
  • year (int, optional) –

  • month (int, optional) –

  • day (int, optional) –

  • hour (int, optional) –

  • minute (int, optional) –

  • second (int, optional) –

  • microsecond (int, optional) –

  • nanosecond (int, optional) –

  • tzinfo (tz-convertible, optional) –

Returns

result – Timestamp with fields replaced.

Return type

otp.datetime

Examples

>>> ts = otp.datetime(2022, 2, 24, 3, 15, 54, 999, 1)
>>> ts
2022-02-24 03:15:54.000999001
>>> ts.replace(year=2000, month=2, day=2, hour=2, minute=2, second=2, microsecond=2, nanosecond=2)
2000-02-02 02:02:02.000002002
static now(tz=None)#

Will return otp.datetime object with timestamp at the moment of calling this function. Not to be confused with function otp.now which can only add column with current timestamp to the otp.Source when running the query.

Parameters

tz (str or timezone object, default None) – Timezone to localize to.

__add__(other)#

Add datetime offset to otp.datetime.

Parameters

other (datetime offset, otp.datetime) – object to add

Returns

result – return otp.datetime if otp.Nano or another datetime offset object was passed as an argument, or pandas.Timedelta object if otp.datetime was passed as an argument.

Return type

otp.datetime, pandas.Timedelta

Examples

>>> otp.datetime(2022, 2, 24) + otp.Nano(1)
2022-02-24 00:00:00.000000001
__sub__(other)#

Subtract datetime offset from otp.datetime.

Parameters

other (datetime offset, otp.datetime) – object to subtract

Returns

result – return datetime if otp.Nano or another datetime offset object was passed as an argument, or pandas.Timedelta object if otp.datetime was passed as an argument.

Return type

otp.datetime, pandas.Timedelta

Examples

>>> otp.datetime(2022, 2, 24) - otp.Nano(1)
2022-02-23 23:59:59.999999999
to_operation(timezone=None)#

Convert otp.datetime object to otp.Operation

Parameters

timezone (Operation) – Can be used to specify timezone as an Operation.

Examples

>>> t = otp.Ticks(TZ=['EST5EDT', 'GMT'])
>>> t['DT'] = otp.dt(2022, 1, 1).to_operation(timezone=t['TZ'])
>>> otp.run(t, timezone='GMT')[['TZ', 'DT']]
        TZ                  DT
0  EST5EDT 2022-01-01 05:00:00
1      GMT 2022-01-01 00:00:00