टाइम सीरीज़ की फ़्रीक्वेंसी बदलना: resampling

Python में Time Series डेटा मैनिपुलेट करना

Stefan Jansen

Founder & Lead Data Scientist at Applied Artificial Intelligence

फ़्रीक्वेंसी बदलना: resampling

  • DateTimeIndex: .asfreq() से फ़्रीक्वेंसी सेट/बदलें
  • लेकिन फ़्रीक्वेंसी बदलने से डेटा प्रभावित होता है
    • Upsampling: missing डेटा को fill या interpolate करें
    • Downsampling: मौजूदा डेटा को aggregate करें
  • pandas API:
    • .asfreq(), .reindex()
    • .resample() + transformation method
Python में Time Series डेटा मैनिपुलेट करना

शुरुआत: quarterly डेटा

dates = pd.date_range(start='2016', periods=4, freq='Q')

data = range(1, 5)
quarterly = pd.Series(data=data, index=dates)
quarterly
2016-03-31    1
2016-06-30    2
2016-09-30    3
2016-12-31    4
Freq: Q-DEC, dtype: int64 # डिफ़ॉल्ट: साल-अंत quarters
Python में Time Series डेटा मैनिपुलेट करना

Upsampling: quarter => month

monthly = quarterly.asfreq('M') # month-end फ़्रीक्वेंसी पर
2016-03-31    1.0
2016-04-30    NaN
2016-05-31    NaN
2016-06-30    2.0
2016-07-31    NaN
2016-08-31    NaN
2016-09-30    3.0
2016-10-31    NaN
2016-11-30    NaN
2016-12-31    4.0
Freq: M, dtype: float64
  • Upsampling से missing values बनती हैं
monthly = monthly.to_frame('baseline') # DataFrame में
Python में Time Series डेटा मैनिपुलेट करना

Upsampling: fill methods

monthly['ffill'] = quarterly.asfreq('M', method='ffill')

monthly['bfill'] = quarterly.asfreq('M', method='bfill')
monthly['value'] = quarterly.asfreq('M', fill_value=0)
Python में Time Series डेटा मैनिपुलेट करना

Upsampling: fill methods

  • bfill: backfill
  • ffill: forward fill
            baseline  ffill  bfill  value
2016-03-31       1.0      1      1      1
2016-04-30       NaN      1      2      0
2016-05-31       NaN      1      2      0
2016-06-30       2.0      2      2      2
2016-07-31       NaN      2      3      0
2016-08-31       NaN      2      3      0
2016-09-30       3.0      3      3      3
2016-10-31       NaN      3      4      0
2016-11-30       NaN      3      4      0
2016-12-31       4.0      4      4      4
Python में Time Series डेटा मैनिपुलेट करना

मिसिंग महीनों को जोड़ें: .reindex()

dates = pd.date_range(start='2016', 
                      periods=12, 
                      freq='M')
DatetimeIndex(['2016-01-31', 
               '2016-02-29', 
               ..., 
               '2016-11-30', 
               '2016-12-31'],
        dtype='datetime64[ns]', freq='M')
  • .reindex():
    • DataFrame को नए index से conform करें
    • .asfreq() जैसा ही filling logic
quarterly.reindex(dates)
2016-01-31    NaN
2016-02-29    NaN
2016-03-31    1.0
2016-04-30    NaN
2016-05-31    NaN
2016-06-30    2.0
2016-07-31    NaN
2016-08-31    NaN
2016-09-30    3.0
2016-10-31    NaN
2016-11-30    NaN
2016-12-31    4.0
Python में Time Series डेटा मैनिपुलेट करना

अभ्यास करते हैं!

Python में Time Series डेटा मैनिपुलेट करना

Preparing Video For Download...