qis.TimePeriod

class qis.TimePeriod(start=None, end=None, tz=None)[source]

Bases: object

a start and end date, with the conversions and slicing every qis function expects.

The canonical way to express a window in this stack. Pass a TimePeriod rather than masking an index by hand: time_period.locate(df) applies the same inclusive convention everywhere, and the tz is carried with the period so a tz-aware frame is not silently compared against naive timestamps.

Either bound may be None, meaning open-ended on that side. start and end accept pd.Timestamp, datetime, str, int, or an Enum whose value is one of those:

  • '12/31/2019' — month/day/year

  • '31Dec2019'

  • 20191231 as a string — year/month/day

  • 2019 as an int — 1 January of that year

Parameters:
  • start (SUPPORTED_TYPES) – start of the window, or None for open-ended

  • end (SUPPORTED_TYPES) – end of the window, or None for open-ended

  • tz (str) – timezone applied to both bounds

Raises:

TypeError – if a bound is of a type not listed above

__init__(start=None, end=None, tz=None)[source]
Parameters:

Methods

__init__([start, end, tz])

copy()

end_to_str([format])

fill_outside(df[, fill_value])

fill given value outside of timeperiod

get_time_period_an()

locate(df)

truncate timeseries data to given timeperiod

print()

shift_end_date_by_days([backward, num_days])

shift_start_date_by_days([backward, num_days])

start_to_str([format])

to_pd_datetime_index([freq, hour_offset, ...])

generate pd dt_time index

to_period_dates_str([freq, tz, date_format, ...])

nb.

to_str([date_separator, ...])

tz_localize([tz])

Attributes

SUPPORTED_TYPES

alias of Timestamp | str | datetime | Enum | int

print()[source]
Return type:

None

copy()[source]
Return type:

TimePeriod

tz_localize(tz='UTC')[source]
Parameters:

tz (str)

Return type:

TimePeriod

to_str(date_separator=' - ', is_increase_by_one_day=False, date_format='%d%b%Y')[source]
Parameters:
  • date_separator (str)

  • is_increase_by_one_day (bool)

  • date_format (str | None)

Return type:

str

start_to_str(format='%d%b%Y')[source]
Parameters:

format (str | None)

Return type:

str

end_to_str(format='%d%b%Y')[source]
Parameters:

format (str | None)

Return type:

str

locate(df)[source]

truncate timeseries data to given timeperiod

Parameters:

df (DataFrame | Series)

Return type:

DataFrame | Series

fill_outside(df, fill_value=nan)[source]

fill given value outside of timeperiod

Parameters:
Return type:

DataFrame | Series

to_pd_datetime_index(freq='B', hour_offset=None, tz=None, include_start_date=False, include_end_date=False, is_business_dates=True, days_shift=None)[source]

generate pd dt_time index

Parameters:
  • freq (str)

  • hour_offset (int | None)

  • tz (str | None)

  • include_start_date (bool)

  • include_end_date (bool)

  • is_business_dates (bool)

  • days_shift (int | None)

Return type:

DatetimeIndex

to_period_dates_str(freq='B', tz=None, date_format='%Y%m%d', holidays=None)[source]

nb. can pass calendar pd.date_range(holidays=holidays)

Parameters:
Return type:

List[str]

shift_end_date_by_days(backward=True, num_days=1)[source]
Parameters:
Return type:

TimePeriod

shift_start_date_by_days(backward=False, num_days=1)[source]
Parameters:
Return type:

TimePeriod

get_time_period_an()[source]
Return type:

float