Zaman serilerinde indeksleme ve yeniden örnekleme

Python ile Zaman Serisi Verilerini Manipüle Etme

Stefan Jansen

Founder & Lead Data Scientist at Applied Artificial Intelligence

Zaman serisi dönüşümü

Temel zaman serisi dönüşümleri:

  • Dize tarihleri ayrıştırma ve datetime64'e çevirme

  • Belirli alt dönemler için seçme ve dilimleme

  • DateTimeIndex frekansını ayarlama ve değiştirme

    • Üst örnekleme vs Alt örnekleme
Python ile Zaman Serisi Verilerini Manipüle Etme

GOOG hisse fiyatlarını alma

google = pd.read_csv('google.csv')  # import pandas as pd

google.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 504 entries, 0 to 503
Data columns (total 2 columns):
date     504 non-null object
price    504 non-null float64
dtypes: float64(1), object(1)
google.head()
         date   price
0  2015-01-02  524.81
1  2015-01-05  513.87
2  2015-01-06  501.96
3  2015-01-07  501.10
4  2015-01-08  502.68
Python ile Zaman Serisi Verilerini Manipüle Etme

Dize tarihlerini datetime64'e dönüştürme

  • pd.to_datetime():
    • Tarih dizgesini ayrıştırır
    • datetime64'e çevirir
google.date = pd.to_datetime(google.date)

google.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 504 entries, 0 to 503
Data columns (total 2 columns):
date     504 non-null datetime64[ns]
price    504 non-null float64
dtypes: datetime64[ns](1), float64(1)
Python ile Zaman Serisi Verilerini Manipüle Etme

Dize tarihlerini datetime64'e dönüştürme

  • .set_index():
    • Tarihi indekse alır
    • inplace:
      • kopya oluşturmaz
google.set_index('date', inplace=True)

google.info()
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 504 entries, 2015-01-02 to 2016-12-30
Data columns (total 1 columns):
price    504 non-null float64
dtypes: float64(1)
Python ile Zaman Serisi Verilerini Manipüle Etme

Google hisse senedi zaman serisini çizme

google.price.plot(title='Google Stock Price')

plt.tight_layout(); plt.show()

Google hisse senedi zaman serisini çizme

Python ile Zaman Serisi Verilerini Manipüle Etme

Kısmi dize ile indeksleme

  • Tarihe ayrıştırılan dizelerle seçme/indeksleme
google['2015'].info() # Tarihin bir bölümünü dize olarak geçin
DatetimeIndex: 252 entries, 2015-01-02 to 2015-12-31
Data columns (total 1 columns):
price    252 non-null float64
dtypes: float64(1)
google['2015-3': '2016-2'].info() # Dilim son ayı içerir
DatetimeIndex: 252 entries, 2015-03-02 to 2016-02-29
Data columns (total 1 columns):
price    252 non-null float64
dtypes: float64(1)
memory usage: 3.9 KB
Python ile Zaman Serisi Verilerini Manipüle Etme

Kısmi dize ile indeksleme

google.loc['2016-6-1', 'price'] # Tam tarihi .loc[] ile kullanın
734.15
Python ile Zaman Serisi Verilerini Manipüle Etme

.asfreq(): frekans ayarla

  • .asfreq('D'):
    • DateTimeIndex'i takvim günü frekansına çevirir
google.asfreq('D').info() # takvim günü frekansını ayarla
DatetimeIndex: 729 entries, 2015-01-02 to 2016-12-30
Freq: D
Data columns (total 1 columns):
price    504 non-null float64
dtypes: float64(1)
Python ile Zaman Serisi Verilerini Manipüle Etme

.asfreq(): frekans ayarla

  • Üst örnekleme:
    • Daha yüksek frekans yeni tarihler demektir => eksik veri
google.asfreq('D').head()
             price
date              
2015-01-02  524.81
2015-01-03     NaN
2015-01-04     NaN
2015-01-05  513.87
2015-01-06  501.96
Python ile Zaman Serisi Verilerini Manipüle Etme

.asfreq(): frekansı sıfırla

  • .asfreq('B'):
    • DateTimeIndex'i iş günü frekansına çevirir
google = google.asfreq('B') # Takvim günü frekansına değiştir

google.info()
DatetimeIndex: 521 entries, 2015-01-02 to 2016-12-30
Freq: B
Data columns (total 1 columns):
price    504 non-null float64
dtypes: float64(1)
Python ile Zaman Serisi Verilerini Manipüle Etme

.asfreq(): frekansı sıfırla

google[google.price.isnull()] # Eksik 'price' değerlerini seçin
            price
date             
2015-01-19    NaN
2015-02-16    NaN
...
2016-11-24    NaN
2016-12-26    NaN
  • İş günü olup işlem yapılmayan günler
Python ile Zaman Serisi Verilerini Manipüle Etme

Ayo berlatih!

Python ile Zaman Serisi Verilerini Manipüle Etme

Preparing Video For Download...