Pandas Assignment– 6

Time Series & Date Handling

Basic Questions

  1. Create a Series of date strings [‘2024-01-01′,’2024-02-15′,’2024-03-20’]. Convert it to datetime using pd.to_datetime().
  2. From a datetime Series, extract only the .dt.year values.
  3. From the same Series, extract the .dt.month and .dt.day.
  4. Create a Series of timestamps with hours (‘2024-01-01 10:00:00’, ‘2024-01-01 15:00:00’). Extract .dt.hour.
  5. Create a date range of 7 consecutive days starting from ‘2024-04-01’ using pd.date_range().
  6. Create a date range of 6 months starting from ‘2024-01-01’ with frequency ‘M’.
  7. Create a DataFrame with a datetime index of daily frequency for 10 days and a column ‘sales’ of random numbers. Print the DataFrame.
  8. From the DataFrame, select only rows where the date is in April 2024 using .loc[].
  9. Resample the daily ‘sales’ DataFrame to weekly frequency using .resample(‘W’).sum().
  10. Resample the same daily data to monthly average sales using .resample(‘M’).mean().
  11. Create a Series with 10 sequential days. Apply .shift(1) to shift all values down by 1 day.
  12. Apply .shift(-1) to shift values upward by 1 day.
  13. Use .shift(2) with fill_value=0 to see how missing values are filled.
  14. Create a Series with datetime index and apply .tshift(1, freq=’D’) to shift the index by 1 day forward.
  15. Create a daily time series of 30 random values. Apply .rolling(window=3).mean() and print the first 10 results.
  16. On the same time series, compute a rolling window sum with .rolling(5).sum().
  17. Apply .expanding().sum() to the time series and print the first 10 results.
  18. Apply .expanding().mean() to calculate cumulative average.
  19. Create a Series of 12 months data and resample quarterly using .resample(‘Q’).mean().
  20. Show how .rolling(window=7).mean() smooths weekly variations in a noisy daily dataset.

Intermediate Questions

  1. Convert a column of mixed date strings (‘2024/01/01′,’01-02-2024′,’March 3, 2024’) into datetime using to_datetime().
  2. Extract .dt.weekday from a datetime Series and print day of week (0=Monday).
  3. From a Series of timestamps, extract .dt.quarter values.
  4. Create a date range with hourly frequency for ‘2024-01-01’. Print the first 10 timestamps.
  5. Create a business day range of 10 days using pd.date_range(freq=’B’).
  6. Create a DataFrame with a datetime index (1 month daily) and column ‘temperature’. Resample to weekly max.
  7. Resample the same DataFrame to 2-day frequency with average values.
  8. Compare .resample(‘W’).sum() with .resample(‘7D’).sum() on the same dataset.
  9. Create a time series of stock prices. Use .shift(1) to create a lag-1 column (yesterday’s price).
  10. Compute daily returns as (today – yesterday)/yesterday using shifted values.
  11. Use .shift(-1) to create a lead column (tomorrow’s price).
  12. Demonstrate .tshift(freq=’M’) to shift index to next month.
  13. Create a Series of 100 daily random sales. Use .rolling(7).mean() to calculate 7-day moving average.
  14. Plot original vs rolling mean to show smoothing effect.
  15. Use .rolling(7, min_periods=1).mean() to handle small windows at the start.
  16. Compute .rolling(7).std() to show rolling standard deviation.
  17. Demonstrate .expanding().max() on a time series.
  18. Compare .expanding().sum() with .cumsum() on the same data.
  19. Create a time series with missing dates. Use .resample(‘D’).asfreq() to reindex daily, filling NaN.
  20. Fill missing values after resampling using .ffill() and .bfill().

Advanced Questions

  1. Create a DataFrame of stock prices with datetime index for 2 years of daily data. Resample monthly to compute open, high, low, and close (OHLC).
  2. On the same stock dataset, compute rolling 30-day volatility using .rolling(30).std().
  3. Use .expanding().mean() to compute cumulative average stock price over the 2 years.
  4. Build a time series of daily rainfall. Resample to annual total rainfall and identify driest and wettest year using .idxmin() and .idxmax().
  5. Create a DataFrame of hourly energy consumption. Resample to daily sums and plot weekday vs weekend comparison using .dt.weekday.
  6. Compute a 7-day rolling correlation between two time series (temperature vs sales).
  7. Use .shift(1) and .shift(7) to create lag features for a time series and compare their correlation with the original.
  8. Demonstrate a pipeline: create noisy daily sales data → compute weekly averages → calculate expanding mean → plot trend.
  9. Create quarterly GDP data and resample to monthly using .resample(‘M’).ffill().
  10. Create a multi-year daily time series, compute 30-day rolling mean, and compare with .resample(‘M’).mean() to show difference between rolling vs resampling.