manager.exo.calendar.get_day_night_features
manager.exo.calendar.get_day_night_features(
start,
cov_end,
location,
freq='h',
timezone='UTC',
)Create day/night features using astronomical sunrise and sunset times.
Sunrise and sunset times are computed once per unique calendar date (using :func:astral.sun.sun) and then broadcast to all timestamps in the requested hourly grid, which avoids redundant computation for large date ranges.
The returned DataFrame contains four columns:
sunrise_hour— rounded sunrise hour (0–23).sunset_hour— rounded sunset hour (0–23).daylight_hours—sunset_hour - sunrise_hour.is_daylight—1if the timestamp is between sunrise and sunset, else0.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| start | Union[str, pd.Timestamp] | Start of the time range. String values are parsed with utc=True. |
required |
| cov_end | Union[str, pd.Timestamp] | Inclusive end of the time range. String values are parsed with utc=True. |
required |
| location | LocationInfo |
:class:~astral.LocationInfo instance describing the geographic location (latitude, longitude, timezone). |
required |
| freq | str | Pandas-compatible frequency string for the output index. Defaults to "h" (hourly). |
'h' |
| timezone | str | Timezone label applied to the generated index. Defaults to "UTC". |
'UTC' |
Returns
| Name | Type | Description |
|---|---|---|
| pd.DataFrame | pd.DataFrame: DataFrame with columns sunrise_hour, |
|
| pd.DataFrame | sunset_hour, daylight_hours, is_daylight. The index |
|
| pd.DataFrame | is a tz-aware :class:~pandas.DatetimeIndex with the requested |
|
| pd.DataFrame | freq. |
Examples:
::: {#e2932735 .cell execution_count=1}
``` {.python .cell-code}
import pandas as pd
from astral import LocationInfo
from spotforecast2_safe.manager.exo.calendar import get_day_night_features
start = pd.Timestamp("2024-06-01", tz="UTC")
cov_end = pd.Timestamp("2024-06-07 23:00", tz="UTC")
location = LocationInfo(
latitude=51.5136,
longitude=7.4653,
timezone="UTC",
)
features = get_day_night_features(
start=start,
cov_end=cov_end,
location=location,
freq="h",
timezone="UTC",
)
print("shape:", features.shape)
print("columns:", features.columns.tolist())
print(features.head(3))
```
::: {.cell-output .cell-output-stdout}
```
shape: (168, 4)
columns: ['sunrise_hour', 'sunset_hour', 'daylight_hours', 'is_daylight']
sunrise_hour sunset_hour daylight_hours \
2024-06-01 00:00:00+00:00 3 20 17
2024-06-01 01:00:00+00:00 3 20 17
2024-06-01 02:00:00+00:00 3 20 17
is_daylight
2024-06-01 00:00:00+00:00 0
2024-06-01 01:00:00+00:00 0
2024-06-01 02:00:00+00:00 0
```
:::
:::