AnalystPath

Roll Up Sunny and Rainy Streaks

PandasHardSenior level~10 min

Problem

You are given two DataFrames.

`rainydays` has a single column `rain_day` (a date); each row is a calendar day on which it rained.

`sunnydays` has a single column `sun_day` (a date); each row is a calendar day that was sunny.

Across the year **2023**, every day in these tables is either rainy or sunny (never both). Walking the days in date order, collapse each maximal run of same-weather days into one streak. For each run, output one row with the weather and the run's first and last day.

Return `weather` (`'rainy'` or `'sunny'`), `streak_start`, `streak_end`, ordered by `streak_start`. Only consider days in 2023 (from `2023-01-01` to `2023-12-31` inclusive).

Input data

Example rows — the live problem includes the full dataset.

rainydays
rain_day
2023-01-04
2023-01-05
sunnydays
sun_day
2023-01-01
2023-01-02
2023-01-03
2023-01-06

Expected output

Your answer should return 3 rows with the columns weather, streak_start, streak_end.

Starter code (Pandas (Python))

import pandas as pd

def roll_up_weather_streaks(rainydays, sunnydays) -> pd.DataFrame:
    # Your code here
    return rainydays

Solve this Pandas question free

Write Pandas (Python) and run it instantly in your browser — even on your phone. No signup needed to try.

Solution & explanation

Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.

Sign up free to unlock

Related Pandas questions