-
Notifications
You must be signed in to change notification settings - Fork 80
Add option day_number_in_year
to DateFlagsTransform
#552
Merged
Merged
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d140582
Add day_number_in_year option in DateFlagsTransform and tests for it
bd798bb
Merge remote-tracking branch 'origin/master' into issue-546
823aa10
Update changelog
1b9ad45
Fix parameter description
3d917cc
Fix generation of regressor prefix
dc912b3
Temp fix for binder
8eb82ce
Merge remote-tracking branch 'origin/master' into issue-546
0bc21b3
Fix binder back, add notebook with fixed DateFlag representation
06b7e35
Merge branch 'master' into issue-546
alex-hse-repository File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -17,6 +17,7 @@ def __init__( | |||||
self, | ||||||
day_number_in_week: Optional[bool] = True, | ||||||
day_number_in_month: Optional[bool] = True, | ||||||
day_number_in_year: Optional[bool] = False, | ||||||
week_number_in_month: Optional[bool] = False, | ||||||
week_number_in_year: Optional[bool] = False, | ||||||
month_number_in_year: Optional[bool] = False, | ||||||
|
@@ -34,6 +35,8 @@ def __init__( | |||||
if True, add column with weekday info to feature dataframe in transform | ||||||
day_number_in_month: | ||||||
if True, add column with day info to feature dataframe in transform | ||||||
day_number_in_year: | ||||||
if True, add column with number of day in a year, leap year numeration, starts with 1 | ||||||
week_number_in_month: | ||||||
if True, add column with week number (in month context) to feature dataframe in transform | ||||||
week_number_in_year: | ||||||
|
@@ -76,6 +79,7 @@ def __init__( | |||||
[ | ||||||
day_number_in_week, | ||||||
day_number_in_month, | ||||||
day_number_in_year, | ||||||
week_number_in_month, | ||||||
week_number_in_year, | ||||||
month_number_in_year, | ||||||
|
@@ -87,13 +91,14 @@ def __init__( | |||||
): | ||||||
raise ValueError( | ||||||
f"{type(self).__name__} feature does nothing with given init args configuration, " | ||||||
f"at least one of day_number_in_week, day_number_in_month, week_number_in_month, " | ||||||
f"at least one of day_number_in_week, day_number_in_month, day_number_in_year, week_number_in_month, " | ||||||
f"week_number_in_year, month_number_in_year, year_number, is_weekend should be True or any of " | ||||||
f"special_days_in_week, special_days_in_month should be not empty." | ||||||
) | ||||||
|
||||||
self.day_number_in_week = day_number_in_week | ||||||
self.day_number_in_month = day_number_in_month | ||||||
self.day_number_in_year = day_number_in_year | ||||||
self.week_number_in_month = week_number_in_month | ||||||
self.week_number_in_year = week_number_in_year | ||||||
self.month_number_in_year = month_number_in_year | ||||||
|
@@ -109,6 +114,7 @@ def __init__( | |||||
self._empty_parameters = dict( | ||||||
day_number_in_week=False, | ||||||
day_number_in_month=False, | ||||||
day_number_in_year=False, | ||||||
week_number_in_month=False, | ||||||
week_number_in_year=False, | ||||||
month_number_in_year=False, | ||||||
|
@@ -156,6 +162,11 @@ def transform(self, df: pd.DataFrame) -> pd.DataFrame: | |||||
timestamp_series=timestamp_series | ||||||
) | ||||||
|
||||||
if self.day_number_in_year: | ||||||
features[self._get_column_name("day_number_in_year")] = self._get_day_number_in_year( | ||||||
timestamp_series=timestamp_series | ||||||
) | ||||||
|
||||||
if self.week_number_in_month: | ||||||
features[self._get_column_name("week_number_in_month")] = self._get_week_number_in_month( | ||||||
timestamp_series=timestamp_series | ||||||
|
@@ -228,11 +239,25 @@ def _get_day_number_in_month(timestamp_series: pd.Series) -> np.ndarray: | |||||
"""Generate an array with the number of the day in the month.""" | ||||||
return timestamp_series.apply(lambda x: x.day).values | ||||||
|
||||||
@staticmethod | ||||||
def _get_day_number_in_year(timestamp_series: pd.Series) -> np.ndarray: | ||||||
"""Generate an array with the number of the day in a year, lear year numeration, starts with 1.""" | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Get the right variant from the constructor docstring |
||||||
|
||||||
def leap_year_number(dt: pd.Timestamp) -> int: | ||||||
"""Return day number with leap year numeration.""" | ||||||
day_of_year = dt.dayofyear | ||||||
if not dt.is_leap_year and dt.month >= 3: | ||||||
return day_of_year + 1 | ||||||
else: | ||||||
return day_of_year | ||||||
|
||||||
return timestamp_series.apply(leap_year_number).values | ||||||
|
||||||
@staticmethod | ||||||
def _get_week_number_in_month(timestamp_series: pd.Series) -> np.ndarray: | ||||||
"""Generate an array with the week number in the month.""" | ||||||
|
||||||
def week_of_month(dt: pd.Timestamp) -> float: | ||||||
def week_of_month(dt: pd.Timestamp) -> int: | ||||||
"""Return week of month number. | ||||||
|
||||||
How it works: | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
May you rewrite it to make it clear