Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions feature_engine/datetime/_datetime_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"quarter": lambda x: x.dt.quarter,
"semester": lambda x: np.where(x.dt.month <= 6, 1, 2).astype(np.int64),
"year": lambda x: x.dt.year,
"week": lambda x: x.dt.isocalendar().week.astype(np.int64),
"week": lambda x: x.dt.isocalendar().week.astype("float64"),
"day_of_week": lambda x: x.dt.dayofweek,
"day_of_month": lambda x: x.dt.day,
"day_of_year": lambda x: x.dt.dayofyear,
Expand All @@ -73,7 +73,7 @@
"year_start": lambda x: x.dt.is_year_start.astype(np.int64),
"year_end": lambda x: x.dt.is_year_end.astype(np.int64),
"leap_year": lambda x: x.dt.is_leap_year.astype(np.int64),
"days_in_month": lambda x: x.dt.days_in_month.astype(np.int64),
"days_in_month": lambda x: x.dt.days_in_month.astype("float64"),
"hour": lambda x: x.dt.hour,
"minute": lambda x: x.dt.minute,
"second": lambda x: x.dt.second,
Expand Down
17 changes: 17 additions & 0 deletions tests/test_datetime/test_datetime_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,23 @@ def test_extract_features_from_variables_containing_nans():
)


def test_ignore_nan_for_week_and_days_in_month():
# week and days_in_month must propagate NaN like the other features when
# missing_values="ignore", instead of raising on the int cast of a NaT.
X = DatetimeFeatures(
features_to_extract=["week", "days_in_month"], missing_values="ignore"
).fit_transform(dates_nan)
pd.testing.assert_frame_equal(
X,
pd.DataFrame(
{
"dates_na_week": [5.0, np.nan, 22.0, np.nan],
"dates_na_days_in_month": [28.0, np.nan, 30.0, np.nan],
}
),
)


def test_extract_features_with_different_datetime_parsing_options(df_datetime):
X = DatetimeFeatures(
features_to_extract=["day_of_month"], dayfirst=True
Expand Down