Best and worst day per asset

window functions

Best and worst day per asset

Robinhood Pandas Interview Question

Robinhood writes a short note on each asset it lists, and the two dates readers actually want are the best day and the worst day: the sharpest one day gain and the sharpest one day fall. Both are measured against the asset's own previous close.

Using the prices DataFrame, find each symbol, the date of its sharpest one day gain as best_day, that gain as best_pct, the date of its sharpest one day fall as worst_day, and that fall as worst_pct. Give both percentages rounded to 2 decimal places. Assign the answer to result.

Asked of

  • Data Analyst
  • Business Analyst
  • BI Analyst
  • Product Analyst
  • Analytics Engineer
  • Data Scientist

pricesDataFrame24 rows

Column NameType
symbolstr
priced_onstr
close_usdfloat64

pricesExample Input

symbolpriced_onclose_usd
BTC2024-05-0158000
BTC2024-05-0259200
BTC2024-05-0360500
BTC2024-05-0461100
BTC2024-05-0559800
BTC2024-05-0658400
ETH2024-05-012900
ETH2024-05-022860
ETH2024-05-032945
ETH2024-05-043010
ETH2024-05-053080
ETH2024-05-062990

Example Output

symbolbest_daybest_pctworst_dayworst_pct
BTC2024-05-032.22024-05-06-2.34
ETH2024-05-032.972024-05-06-2.92

Explanation

BTC rose 2.2 percent on May 3, its sharpest gain in the example, and fell 2.34 percent on May 6. The first day of each asset has nothing before it to measure against, so it can be neither the best nor the worst.

The example above is a small slice of the data. Your code runs against the full DataFrames.