Skip to content

Commit

Permalink
bp.utils.time: renamed "time_to_datetime" to "time_to_timedelta" (nam…
Browse files Browse the repository at this point in the history
…ing was wrong all the time..)
  • Loading branch information
richrobe committed Apr 22, 2022
1 parent 17fe679 commit c549e77
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
6 changes: 3 additions & 3 deletions src/biopsykit/questionnaires/questionnaires.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
)
from biopsykit.utils._datatype_validation_helper import _assert_has_columns, _assert_num_columns, _assert_value_range
from biopsykit.utils.exceptions import ValueRangeError
from biopsykit.utils.time import time_to_datetime
from biopsykit.utils.time import time_to_timedelta


def psqi(data: pd.DataFrame, columns: Optional[Union[Sequence[str], pd.Index]] = None) -> pd.DataFrame:
Expand Down Expand Up @@ -83,11 +83,11 @@ def psqi(data: pd.DataFrame, columns: Optional[Union[Sequence[str], pd.Index]] =

# Bedtime Start: Question 1
bed_time_start = data.filter(regex="01").iloc[:, 0]
bed_time_start = time_to_datetime(bed_time_start)
bed_time_start = time_to_timedelta(bed_time_start)

# Bedtime End: Question 3
bed_time_end = data.filter(regex="03").iloc[:, 0]
bed_time_end = time_to_datetime(bed_time_end)
bed_time_end = time_to_timedelta(bed_time_end)

# Compute Hours in Bed (needed for habitual sleep efficiency)
bed_time_diff = bed_time_end - bed_time_start
Expand Down
15 changes: 9 additions & 6 deletions src/biopsykit/utils/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"check_tz_aware",
"extract_time_from_filename",
"get_time_from_date",
"time_to_datetime",
"time_to_timedelta",
"timedelta_to_time",
]

Expand Down Expand Up @@ -123,8 +123,8 @@ def get_time_from_date(
return data


def time_to_datetime(data: pd.Series) -> pd.Series:
"""Convert time information in a series into ``datetime.datetime`` data.
def time_to_timedelta(data: pd.Series) -> pd.Series:
"""Convert time information in a series into ``datetime.timedelta`` data.
Parameters
----------
Expand All @@ -135,11 +135,14 @@ def time_to_datetime(data: pd.Series) -> pd.Series:
Returns
-------
:class:`~pandas.Series`
series with data converted into :class:`datetime.datetime`
series with data converted into :class:`datetime.timedelta`
"""
col_data = pd.to_datetime(data.astype(str))
return col_data - col_data.dt.normalize()
_assert_is_dtype(data, pd.Series)
if np.issubdtype(data.dtype, np.timedelta64):
# data is already a timedelta
return data
return pd.to_timedelta(data.astype(str))


def timedelta_to_time(data: pd.Series) -> pd.Series:
Expand Down
12 changes: 6 additions & 6 deletions tests/test_io/test_io_sleep.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def sleep_endpoints_dataframe_correct():
"wake_onset": pd.to_datetime("01.01.2021 08:00"),
"total_sleep_duration": 8 * 60,
},
index=pd.DatetimeIndex(pd.to_datetime(["31.12.2020"]), name="date"),
index=pd.DatetimeIndex(pd.to_datetime(["31.12.2020"], format="%d.%m.%Y"), name="date"),
)


Expand All @@ -42,7 +42,7 @@ def sleep_endpoints_dataframe_additional_cols():
"major_rest_period_start": pd.to_datetime("31.12.2020 23:00"),
"major_rest_period_end": pd.to_datetime("01.01.2021 08:30"),
},
index=pd.DatetimeIndex(pd.to_datetime(["31.12.2020"]), name="date"),
index=pd.DatetimeIndex(pd.to_datetime(["31.12.2020"], format="%d.%m.%Y"), name="date"),
)


Expand All @@ -63,15 +63,15 @@ def sleep_endpoints_dataframe_missing_cols():
"sleep_onset": pd.to_datetime("01.01.2021 00:00"),
"wake_onset": pd.to_datetime("01.01.2021 08:00"),
},
index=pd.DatetimeIndex(pd.to_datetime(["31.12.2020"]), name="date"),
index=pd.DatetimeIndex(pd.to_datetime(["31.12.2020"], format="%d.%m.%Y"), name="date"),
)


def sleep_endpoints_dict_correct():
return {
"date": pd.to_datetime("31.12.2020"),
"sleep_onset": pd.to_datetime("01.01.2021 00:00"),
"wake_onset": pd.to_datetime("01.01.2021 08:00"),
"date": pd.to_datetime("31.12.2020", format="%d.%m.%Y"),
"sleep_onset": pd.to_datetime("01.01.2021 00:00", format="%d.%m.%Y %H:%M"),
"wake_onset": pd.to_datetime("01.01.2021 08:00", format="%d.%m.%Y %H:%M"),
"total_sleep_duration": 8 * 60,
}

Expand Down

0 comments on commit c549e77

Please sign in to comment.