Skip to content

bgc_data_processing.utils.dateranges

Date range generating objects.

DateRangeGenerator(start, end, interval, interval_length=1)

Generate date ranges.

Parameters:

Name Type Description Default
start datetime

Starting date of the range.

required
end datetime

Ending date of the range.

required
interval str

Type of interval, 'day', 'week', 'month', 'year' or 'custom'.

required
interval_length int

Length of custom interval, in days., by default 1

1

Generate date ranges.

Parameters:

Name Type Description Default
start datetime

Starting date of the range.

required
end datetime

Ending date of the range.

required
interval str

Type of interval, 'day', 'week', 'month', 'year' or 'custom'.

required
interval_length int

Length of custom interval, in days., by default 1

1
Source code in src/bgc_data_processing/utils/dateranges.py
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
def __init__(
    self,
    start: dt.datetime,
    end: dt.datetime,
    interval: str,
    interval_length: int = 1,
) -> None:
    """Generate date ranges.

    Parameters
    ----------
    start : dt.datetime
        Starting date of the range.
    end : dt.datetime
        Ending date of the range.
    interval : str
        Type of interval, 'day', 'week', 'month', 'year' or 'custom'.
    interval_length : int, optional
        Length of custom interval, in days., by default 1
    """
    self.start = pd.to_datetime(start).normalize()
    self.end = pd.to_datetime(end).normalize()
    self.interval = interval
    self.interval_length = interval_length

freqs: dict[str, str] = {'day': 'D', 'week': 'W', 'month': 'M', 'year': 'Y'} class-attribute

start_column_name: str = 'start_date' class-attribute instance-attribute

end_column_name: str = 'end_date' class-attribute instance-attribute

start = pd.to_datetime(start).normalize() instance-attribute

end = pd.to_datetime(end).normalize() instance-attribute

interval = interval instance-attribute

interval_length = interval_length instance-attribute

__call__()

Load the date ranges.

Returns:

Type Description
DataFrame

Range DataFrame with self.start_column_name and self.end_column_name columns. Both start and end dates are supposed to be included in the final range.

Source code in src/bgc_data_processing/utils/dateranges.py
59
60
61
62
63
64
65
66
67
68
69
70
71
def __call__(self) -> "DateRange":
    """Load the date ranges.

    Returns
    -------
    pd.DataFrame
        Range DataFrame with self.start_column_name and \
        self.end_column_name columns. Both start and end dates \
        are supposed to be included in the final range.
    """
    if self.interval == "custom":
        return self._make_custom_range()
    return self._make_range()

DateRange(data, start_col, end_col)

Class to hold dateranges dataframes.

Parameters:

Name Type Description Default
data DataFrame

The actual daterange dataframe.

required
start_col str

Name of the column with starting dates.

required
end_col str

Name of the column with ending dates.

required
Source code in src/bgc_data_processing/utils/dateranges.py
163
164
165
166
def __init__(self, data: pd.DataFrame, start_col: str, end_col: str) -> None:
    self._data = data
    self._start = start_col
    self._end = end_col

start_field: str property

Satrting date column name.

end_field: str property

Ending date column name.

start_dates: pd.Series property

Starting date column.

end_dates: pd.Series property

Ending date column.

as_dataframe()

Return the dateranges as a DataFrame, ie self._data.

Source code in src/bgc_data_processing/utils/dateranges.py
188
189
190
def as_dataframe(self) -> pd.DataFrame:
    """Return the dateranges as a DataFrame, ie self._data."""
    return self._data

as_str()

Return a Series of dateranges as strings with format: 'YYYYMMDD-YYYYMMDD'.

Source code in src/bgc_data_processing/utils/dateranges.py
192
193
194
195
196
def as_str(self) -> pd.Series:
    """Return a Series of dateranges as strings with format: 'YYYYMMDD-YYYYMMDD'."""
    str_start = pd.to_datetime(self.start_dates).dt.strftime("%Y%m%d")
    str_end = pd.to_datetime(self.end_dates).dt.strftime("%Y%m%d")
    return str_start + "-" + str_end