Skip to content

Commit c549e77

Browse files
committed
bp.utils.time: renamed "time_to_datetime" to "time_to_timedelta" (naming was wrong all the time..)
1 parent 17fe679 commit c549e77

File tree

3 files changed

+18
-15
lines changed

3 files changed

+18
-15
lines changed

src/biopsykit/questionnaires/questionnaires.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
)
3333
from biopsykit.utils._datatype_validation_helper import _assert_has_columns, _assert_num_columns, _assert_value_range
3434
from biopsykit.utils.exceptions import ValueRangeError
35-
from biopsykit.utils.time import time_to_datetime
35+
from biopsykit.utils.time import time_to_timedelta
3636

3737

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

8484
# Bedtime Start: Question 1
8585
bed_time_start = data.filter(regex="01").iloc[:, 0]
86-
bed_time_start = time_to_datetime(bed_time_start)
86+
bed_time_start = time_to_timedelta(bed_time_start)
8787

8888
# Bedtime End: Question 3
8989
bed_time_end = data.filter(regex="03").iloc[:, 0]
90-
bed_time_end = time_to_datetime(bed_time_end)
90+
bed_time_end = time_to_timedelta(bed_time_end)
9191

9292
# Compute Hours in Bed (needed for habitual sleep efficiency)
9393
bed_time_diff = bed_time_end - bed_time_start

src/biopsykit/utils/time.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"check_tz_aware",
2020
"extract_time_from_filename",
2121
"get_time_from_date",
22-
"time_to_datetime",
22+
"time_to_timedelta",
2323
"timedelta_to_time",
2424
]
2525

@@ -123,8 +123,8 @@ def get_time_from_date(
123123
return data
124124

125125

126-
def time_to_datetime(data: pd.Series) -> pd.Series:
127-
"""Convert time information in a series into ``datetime.datetime`` data.
126+
def time_to_timedelta(data: pd.Series) -> pd.Series:
127+
"""Convert time information in a series into ``datetime.timedelta`` data.
128128
129129
Parameters
130130
----------
@@ -135,11 +135,14 @@ def time_to_datetime(data: pd.Series) -> pd.Series:
135135
Returns
136136
-------
137137
:class:`~pandas.Series`
138-
series with data converted into :class:`datetime.datetime`
138+
series with data converted into :class:`datetime.timedelta`
139139
140140
"""
141-
col_data = pd.to_datetime(data.astype(str))
142-
return col_data - col_data.dt.normalize()
141+
_assert_is_dtype(data, pd.Series)
142+
if np.issubdtype(data.dtype, np.timedelta64):
143+
# data is already a timedelta
144+
return data
145+
return pd.to_timedelta(data.astype(str))
143146

144147

145148
def timedelta_to_time(data: pd.Series) -> pd.Series:

tests/test_io/test_io_sleep.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def sleep_endpoints_dataframe_correct():
2929
"wake_onset": pd.to_datetime("01.01.2021 08:00"),
3030
"total_sleep_duration": 8 * 60,
3131
},
32-
index=pd.DatetimeIndex(pd.to_datetime(["31.12.2020"]), name="date"),
32+
index=pd.DatetimeIndex(pd.to_datetime(["31.12.2020"], format="%d.%m.%Y"), name="date"),
3333
)
3434

3535

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

4848

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

6969

7070
def sleep_endpoints_dict_correct():
7171
return {
72-
"date": pd.to_datetime("31.12.2020"),
73-
"sleep_onset": pd.to_datetime("01.01.2021 00:00"),
74-
"wake_onset": pd.to_datetime("01.01.2021 08:00"),
72+
"date": pd.to_datetime("31.12.2020", format="%d.%m.%Y"),
73+
"sleep_onset": pd.to_datetime("01.01.2021 00:00", format="%d.%m.%Y %H:%M"),
74+
"wake_onset": pd.to_datetime("01.01.2021 08:00", format="%d.%m.%Y %H:%M"),
7575
"total_sleep_duration": 8 * 60,
7676
}
7777

0 commit comments

Comments
 (0)