Skip to content
This repository has been archived by the owner on Sep 4, 2024. It is now read-only.

Commit

Permalink
retired YearMonthDay.. I always knew it was unnecessary but it took m…
Browse files Browse the repository at this point in the history
…e awhile to get over it
  • Loading branch information
seekinginfiniteloop committed Jan 16, 2024
1 parent 104d364 commit 71b3575
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 160 deletions.
4 changes: 2 additions & 2 deletions fedcal/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@
MilitaryPayDay,
)
from .utils import (
YearMonthDay,
dt64_to_date,
dt64_to_dow,
iso_to_ts,
to_datetimeindex,
to_dt64,
to_timestamp,
)
from .status import GovStatus

__all__: list[str] = [
"Dept",
Expand All @@ -57,11 +57,11 @@
"FedIndex",
"FedPayDay",
"FedStamp",
"GovStatus",
"MagicDelegator",
"MilitaryPassDay",
"MilitaryPayDay",
"Month",
"YearMonthDay",
"dt64_to_date",
"dt64_to_dow",
"iso_to_ts",
Expand Down
2 changes: 0 additions & 2 deletions fedcal/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
from pandas import DatetimeIndex, Index, Interval, PeriodIndex, Series, Timestamp

if TYPE_CHECKING:
from fedcal.utils import YearMonthDay
from fedcal.enum import EnumBase

TimestampSeries = "Series[Timestamp]"
Expand All @@ -38,7 +37,6 @@
int64,
datetime64,
float,
"YearMonthDay",
tuple[int, int, int],
tuple[str, str, str],
str,
Expand Down
9 changes: 4 additions & 5 deletions fedcal/fedindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
MilitaryPassDay,
MilitaryPayDay,
)
from fedcal.utils import YearMonthDay


class FedIndex(
Expand Down Expand Up @@ -298,10 +297,7 @@ def _set_default_index() -> DatetimeIndex:
-------
`pd.DatetimeIndex` with default range of FY99 to FY44.
"""
default_range: tuple["YearMonthDay", "YearMonthDay"] = (
utils.YearMonthDay(year=1998, month=10, day=1),
utils.YearMonthDay(year=2045, month=9, day=30),
)
default_range: tuple[Timestamp, Timestamp] = pd.Timestamp(year=1998,month=10,day=1), pd.Timestamp(year=2045,month=9,day=30)
return utils.to_datetimeindex(default_range)

def set_self_date_range(self) -> tuple[Timestamp, Timestamp]:
Expand Down Expand Up @@ -707,6 +703,9 @@ def departments_bool(self) -> DataFrame:

pass

@staticmethod
def get_status_keys() ->


def to_fedindex(*dates: FedIndexConvertibleTypes) -> FedIndex:
"""
Expand Down
22 changes: 1 addition & 21 deletions fedcal/fedstamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
MilitaryPassDay,
MilitaryPayDay,
)
from fedcal.utils import YearMonthDay, to_timestamp, ts_to_posix_day
from fedcal.utils import to_timestamp, ts_to_posix_day


class FedStamp(metaclass=MagicDelegator, delegate_to="ts", delegate_class=pd.Timestamp):
Expand Down Expand Up @@ -66,9 +66,6 @@ class FedStamp(metaclass=MagicDelegator, delegate_to="ts", delegate_class=pd.Tim
_fiscalcal: A *private* lazy attribute that caches our FiscalCalendar
instance once called.
year_month_day
returns the FedStamp as a YearMonthDay object.
posix_day
Returns the POSIX-day timestamp normalized to midnight.
Expand Down Expand Up @@ -311,23 +308,6 @@ def __getattribute__(self, name: str) -> Any:
# static utility methods

# utility properties
@property
def year_month_day(self) -> "YearMonthDay":
"""
Returns a YearMonthDay object for the date.
Returns
-------
A YearMonthDay object representing the year, month, and day of the
ts.
"""
return YearMonthDay(
year=self.ts.year,
month=self.ts.month,
day=self.ts.day,
)

@property
def posix_day(self) -> int:
"""
Expand Down
131 changes: 1 addition & 130 deletions fedcal/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@
dow using numpy vectorized operations. Primarily used for custom DateOffset
object calculations.
- `YearMonthDay` a class for handling date conversions from year, month, day.
It's there because I wanted something cleaner than datetime. I like it. It's
gonna stay.
- `to_timestamp` and `to_datetimeindex` are singledispatch converter functions
for converting a wide range of possible time inputs to `pd.Timestamp` and
`pd.DatetimeIndex` respectively. As they can look complex for the uninitiated
Expand Down Expand Up @@ -355,124 +351,6 @@ def dt64_to_dow(dtarr: NDArray[datetime64]) -> NDArray[datetime64 | int64]:
return out


@dataclass(order=True, slots=True)
class YearMonthDay:

"""
A class to handle conversion of year,month,day integer input to other date
types needed by the calendar.
Do we *need* YearMonthDay? No, but it does provide clear typing and ensure
smooth functioning for the most common form of programmatic date input
(i.e. year, month, day). We need it in the same sense that an
average person needs a remote controlled drone... they don't, but it beats
climbing on a roof. Doesn't YearMonthDay look so much nicer in a type
hint than tuple[int, int, int]? I think so. Could we use Python date
instead? Also yes.
Attributes
----------
year : Four digit year as an integer
month : integer month
day : integer day
Methods
-------
from_timestamp(date: Timestamp) -> YearMonthDay
Convert a pandas pd.Timestamp object into a YearMonthDay
object.
to_posix_timestamp(self) -> int
Converts a YearMonthDay object to a POSIX-day integer timestamp.
to_ts(self) -> Timestamp
Converts YearMonthDay to pandas pd.Timestamp.
to_pydate(self) -> date
Converts YearMonthDay to Python date object (datetime.date)
timetuple(self) -> tuple[int, int, int]
Returns a tuple of YearMonthDay attributes.
"""

year: int = field(converter=int)
month: int = field(converter=int)
day: int = field(converter=int)

@staticmethod
def from_timestamp(date: Timestamp) -> Self:
"""
Convert a pandas pd.Timestamp object into a
YearMonthDay object.
Parameters
----------
date : Date to convert
Returns
-------
YearMonthDay object
"""
return YearMonthDay(year=date.year, month=date.month, day=date.day)

def to_timestamp(self) -> int:
"""
Converts a YearMonthDay object to a POSIX-day integer timestamp.
Returns
-------
A POSIX timestamp as an integer (seconds since the Unix Epoch).
"""
return int(self.to_ts().timestamp())

def to_timestamp_day(self) -> int:
"""
Converts a YearMonthDay object to a POSIX-day integer timestamp.
Returns
-------
A POSIX-day timestamp as an integer (whole days since the Unix Epoch).
"""
return ts_to_posix_day(timestamp=self.to_ts())

def to_ts(self) -> Timestamp:
"""
Converts YearMonthDay to pandas pd.Timestamp.
Returns
-------
A pandas pd.Timestamp object.
"""
return pd.Timestamp(year=self.year, month=self.month, day=self.day)

def to_pydate(self) -> datetime.date:
"""
Converts YearMonthDay to Python datetime.date.
Returns
-------
A Python datetime.date object.
"""
return datetime.date(year=self.year, month=self.month, day=self.day)

@property
def timetuple(self) -> tuple[int, int, int]:
"""
Returns a tuple of YearMonthDay attributes.
Returns
-------
A tuple of YearMonthDay attributes.
"""
return self.year, self.month, self.day


@singledispatch
def to_timestamp(date_input: FedStampConvertibleTypes) -> Timestamp | None:
"""
Expand Down Expand Up @@ -576,12 +454,6 @@ def _date_to_timestamp(
return _normalize_timestamp(date_input)


@to_timestamp.register(cls=YearMonthDay)
def _yearmonthday_to_timestamp(date_input: YearMonthDay) -> Timestamp:
"""Conversion for YearMonthDay objects."""
return _normalize_timestamp(date_input.to_ts())


@to_timestamp.register(cls=tuple)
def _timetuple_to_timestamp(date_input: tuple) -> Timestamp:
if len(date_input) != 3:
Expand All @@ -600,7 +472,7 @@ def _timetuple_to_timestamp(date_input: tuple) -> Timestamp:
if not 1970 <= year <= 2200:
raise ValueError("Year must be a four-digit number between 1970 and 2199")

return _normalize_timestamp(YearMonthDay(year=year, month=month, day=day).to_ts())
return _normalize_timestamp(pd.Timestamp(year=year, month=month, day=day))


def _check_year(dates: Timestamp | DatetimeIndex) -> Timestamp | DatetimeIndex:
Expand Down Expand Up @@ -852,7 +724,6 @@ def _normalize_datetimeindex(datetimeindex: DatetimeIndex) -> DatetimeIndex:


__all__: list[str] = [
"YearMonthDay",
"check_timestamp",
"datetime_keys",
"dt64_to_date",
Expand Down

0 comments on commit 71b3575

Please sign in to comment.