diff --git a/feature_engine/datetime/_datetime_constants.py b/feature_engine/datetime/_datetime_constants.py index f7ef69a20..49f401b59 100644 --- a/feature_engine/datetime/_datetime_constants.py +++ b/feature_engine/datetime/_datetime_constants.py @@ -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, @@ -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, diff --git a/tests/test_datetime/test_datetime_features.py b/tests/test_datetime/test_datetime_features.py index acd9d06e8..c22239492 100644 --- a/tests/test_datetime/test_datetime_features.py +++ b/tests/test_datetime/test_datetime_features.py @@ -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