Pandas Assignment– 6
Time Series & Date Handling
Basic Questions
- Create a Series of date strings [‘2024-01-01′,’2024-02-15′,’2024-03-20’]. Convert it to datetime using pd.to_datetime().
- From a datetime Series, extract only the .dt.year values.
- From the same Series, extract the .dt.month and .dt.day.
- Create a Series of timestamps with hours (‘2024-01-01 10:00:00’, ‘2024-01-01 15:00:00’). Extract .dt.hour.
- Create a date range of 7 consecutive days starting from ‘2024-04-01’ using pd.date_range().
- Create a date range of 6 months starting from ‘2024-01-01’ with frequency ‘M’.
- Create a DataFrame with a datetime index of daily frequency for 10 days and a column ‘sales’ of random numbers. Print the DataFrame.
- From the DataFrame, select only rows where the date is in April 2024 using .loc[].
- Resample the daily ‘sales’ DataFrame to weekly frequency using .resample(‘W’).sum().
- Resample the same daily data to monthly average sales using .resample(‘M’).mean().
- Create a Series with 10 sequential days. Apply .shift(1) to shift all values down by 1 day.
- Apply .shift(-1) to shift values upward by 1 day.
- Use .shift(2) with fill_value=0 to see how missing values are filled.
- Create a Series with datetime index and apply .tshift(1, freq=’D’) to shift the index by 1 day forward.
- Create a daily time series of 30 random values. Apply .rolling(window=3).mean() and print the first 10 results.
- On the same time series, compute a rolling window sum with .rolling(5).sum().
- Apply .expanding().sum() to the time series and print the first 10 results.
- Apply .expanding().mean() to calculate cumulative average.
- Create a Series of 12 months data and resample quarterly using .resample(‘Q’).mean().
- Show how .rolling(window=7).mean() smooths weekly variations in a noisy daily dataset.
Intermediate Questions
- Convert a column of mixed date strings (‘2024/01/01′,’01-02-2024′,’March 3, 2024’) into datetime using to_datetime().
- Extract .dt.weekday from a datetime Series and print day of week (0=Monday).
- From a Series of timestamps, extract .dt.quarter values.
- Create a date range with hourly frequency for ‘2024-01-01’. Print the first 10 timestamps.
- Create a business day range of 10 days using pd.date_range(freq=’B’).
- Create a DataFrame with a datetime index (1 month daily) and column ‘temperature’. Resample to weekly max.
- Resample the same DataFrame to 2-day frequency with average values.
- Compare .resample(‘W’).sum() with .resample(‘7D’).sum() on the same dataset.
- Create a time series of stock prices. Use .shift(1) to create a lag-1 column (yesterday’s price).
- Compute daily returns as (today – yesterday)/yesterday using shifted values.
- Use .shift(-1) to create a lead column (tomorrow’s price).
- Demonstrate .tshift(freq=’M’) to shift index to next month.
- Create a Series of 100 daily random sales. Use .rolling(7).mean() to calculate 7-day moving average.
- Plot original vs rolling mean to show smoothing effect.
- Use .rolling(7, min_periods=1).mean() to handle small windows at the start.
- Compute .rolling(7).std() to show rolling standard deviation.
- Demonstrate .expanding().max() on a time series.
- Compare .expanding().sum() with .cumsum() on the same data.
- Create a time series with missing dates. Use .resample(‘D’).asfreq() to reindex daily, filling NaN.
- Fill missing values after resampling using .ffill() and .bfill().
Advanced Questions
- 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).
- On the same stock dataset, compute rolling 30-day volatility using .rolling(30).std().
- Use .expanding().mean() to compute cumulative average stock price over the 2 years.
- Build a time series of daily rainfall. Resample to annual total rainfall and identify driest and wettest year using .idxmin() and .idxmax().
- Create a DataFrame of hourly energy consumption. Resample to daily sums and plot weekday vs weekend comparison using .dt.weekday.
- Compute a 7-day rolling correlation between two time series (temperature vs sales).
- Use .shift(1) and .shift(7) to create lag features for a time series and compare their correlation with the original.
- Demonstrate a pipeline: create noisy daily sales data → compute weekly averages → calculate expanding mean → plot trend.
- Create quarterly GDP data and resample to monthly using .resample(‘M’).ffill().
- 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.
