Classify a time series trend by comparing first-half and second-half means

Quick Overview

Write a pandas function that splits a daily time series into two equal halves by position, compares the half means, and labels the change as increasing, decreasing or stable using a 5% band. It tests positional slicing, boundary handling with floating-point ratios and the limits of a split-half trend check.

Classify a time series trend by comparing first-half and second-half means

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `trend_direction(s)` for a `pd.Series` of floats with a daily, ascending `DatetimeIndex` of even length. Split the series by position into two equal halves (the first `len(s) // 2` values and the rest, ignoring the dates), compare the two half means, and classify the move with a 5% band. Requirements: - Return a `pd.Series` with index order `first_half_mean`, `second_half_mean`, `abs_change`, `pct_change`, `direction`. - `abs_change` is `second - first` (a float). `pct_change` is `second / first - 1`, rounded to 4 decimal places. - `direction` is a string: `'increasing'` if `pct_change > 0.05`, `'decreasing'` if `pct_change < -0.05`, else `'stable'`. A `pct_change` of exactly `0.05` or `-0.05` is `'stable'`. - The result has dtype `object` and `name` `None`. Example: ```python s = pd.Series([1.0, 1.0, 2.0, 2.0], index=pd.date_range('2024-01-01', periods=4)) trend_direction(s) # first_half_mean 1.0 # second_half_mean 2.0 # abs_change 1.0 # pct_change 1.0 # direction increasing # dtype: object ``` ```hint Boundary values Run your function on a series whose half means are 1.0 and 1.05 and check which label comes back. Floating-point division does not always land exactly on 0.05. ``` ### Constraints & Assumptions - The index is sorted ascending and the length is even, so no input validation is needed. ### What a Strong Answer Covers - A positional split into two equal halves and the mean of each. - `abs_change` and a rounded `pct_change`, with the label decided so that a move of exactly 5% in either direction reads as stable. - An output Series in the exact index order, with object dtype and no name. - What happens when the first-half mean is zero or negative. - The limits of a split-half comparison as a trend test. ### Follow-up Questions - How would you handle a `first_half_mean` of zero? - What goes wrong with `pct_change` when the first-half mean is negative? - Why is split-half a weak trend test, and what is a better alternative?

Overview: Write a pandas function that splits a daily time series into two equal halves by position, compares the half means, and labels the change as increasing, decreasing or stable using a 5% band. It tests positional slicing, boundary handling with floating-point ratios and the limits of a split-half trend check.

General logo
General
Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Implement trend_direction(s) for a pd.Series of floats with a daily, ascending DatetimeIndex of even length. Split the series by position into two equal halves (the first len(s) // 2 values and the rest, ignoring the dates), compare the two half means, and classify the move with a 5% band.

Requirements:

  • Return a pd.Series with index order first_half_mean , second_half_mean , abs_change , pct_change , direction .
  • abs_change is second - first (a float). pct_change is second / first - 1 , rounded to 4 decimal places.
  • direction is a string: 'increasing' if pct_change > 0.05 , 'decreasing' if pct_change < -0.05 , else 'stable' . A pct_change of exactly 0.05 or -0.05 is 'stable' .
  • The result has dtype object and name None .

Example:

s = pd.Series([1.0, 1.0, 2.0, 2.0],
              index=pd.date_range('2024-01-01', periods=4))
trend_direction(s)
# first_half_mean       1.0
# second_half_mean      2.0
# abs_change            1.0
# pct_change            1.0
# direction      increasing
# dtype: object

Constraints & Assumptions

  • The index is sorted ascending and the length is even, so no input validation is needed.

What a Strong Answer Covers Guidance

  • A positional split into two equal halves and the mean of each.
  • abs_change and a rounded pct_change , with the label decided so that a move of exactly 5% in either direction reads as stable.
  • An output Series in the exact index order, with object dtype and no name.
  • What happens when the first-half mean is zero or negative.
  • The limits of a split-half comparison as a trend test.

Follow-up Questions Guidance

  • How would you handle a first_half_mean of zero?
  • What goes wrong with pct_change when the first-half mean is negative?
  • Why is split-half a weak trend test, and what is a better alternative?