Skip to content

Commit

Permalink
fix: drop first/last days instead of setting to na; rename to drop_ a…
Browse files Browse the repository at this point in the history
…nd flag_ for clarity
  • Loading branch information
chanshing committed Nov 6, 2024
1 parent aafc415 commit f6a0f82
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/stepcount/stepcount.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ def main():

# Exclusion: first/last days
if args.exclude_first_last is not None:
data = utils.exclude_first_last_days(data, args.exclude_first_last)
data = utils.drop_first_last_days(data, args.exclude_first_last)

# Exclusion: days with wear time below threshold
if args.exclude_wear_below is not None:
data = utils.exclude_wear_below_days(data, args.exclude_wear_below)
data = utils.flag_wear_below_days(data, args.exclude_wear_below)

# Update wear time stats after exclusions
info.update(utils.calculate_wear_stats(data))
Expand Down
22 changes: 11 additions & 11 deletions src/stepcount/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def calculate_wear_stats(data: pd.DataFrame):
}


def exclude_wear_below_days(
def flag_wear_below_days(
x: Union[pd.Series, pd.DataFrame],
min_wear: str = '12H'
):
Expand Down Expand Up @@ -217,34 +217,34 @@ def exclude_wear_below_days(
return x


def exclude_first_last_days(
def drop_first_last_days(
x: Union[pd.Series, pd.DataFrame],
first_or_last='both'
):
"""
Set the values of the first day, last day, or both to NaN in a time series.
Drop the first day, last day, or both from a time series.
Parameters:
- x (pd.Series or pd.DataFrame): A pandas Series or DataFrame with a DatetimeIndex representing time series data.
- first_or_last (str, optional): A string indicating which days to exclude. Options are 'first', 'last', or 'both'. Default is 'both'.
- first_or_last (str, optional): A string indicating which days to drop. Options are 'first', 'last', or 'both'. Default is 'both'.
Returns:
- pd.Series or pd.DataFrame: A pandas Series or DataFrame with the values of the specified days set to NaN.
- pd.Series or pd.DataFrame: A pandas Series or DataFrame with the values of the specified days dropped.
Example:
# Exclude the first day from the series
series = exclude_first_last_days(series, first_or_last='first')
# Drop the first day from the series
series = drop_first_last_days(series, first_or_last='first')
"""
if len(x) == 0:
print("No data to exclude")
print("No data to drop")
return x

if first_or_last == 'first':
x[x.index.date == x.index.date[0]] = np.nan
x = x[x.index.date != x.index.date[0]]
elif first_or_last == 'last':
x[x.index.date == x.index.date[-1]] = np.nan
x = x[x.index.date != x.index.date[-1]]
elif first_or_last == 'both':
x[(x.index.date == x.index.date[0]) | (x.index.date == x.index.date[-1])] = np.nan
x = x[(x.index.date != x.index.date[0]) & (x.index.date != x.index.date[-1])]
return x


Expand Down

0 comments on commit f6a0f82

Please sign in to comment.