Python ile Zaman Serisi Verilerini Manipüle Etme
Stefan Jansen
Founder & Lead Data Scientist at Applied Artificial Intelligence
.resample(): .groupby()'a benzer
Yeniden örnekleme döneminde verileri gruplar ve her gruba bir veya birkaç yöntem uygular
Yeni tarih, ofsete göre belirlenir: başlangıç, bitiş vb.
Yukarı örnekleme: mevcutlardan doldurma veya enterpolasyon
Aşağı örnekleme: mevcut verilere toplulaştırma uygula
unrate = pd.read_csv('unrate.csv', parse_dates['Date'], index_col='Date')unrate.info()
DatetimeIndex: 208 entries, 2000-01-01 to 2017-04-01
Data columns (total 1 columns):
UNRATE 208 non-null float64 # frekans bilgisi yok
dtypes: float64(1)
unrate.head()
UNRATE
DATE
2000-01-01 4.0
2000-02-01 4.1
2000-03-01 4.0
2000-04-01 3.8
2000-05-01 4.0
| Frekans | Kısaltma | Örnek Tarih |
|---|---|---|
| Takvim Ay Sonu | M | 2017-04-30 |
| Takvim Ay Başı | MS | 2017-04-01 |
| İş Günü Ay Sonu | BM | 2017-04-28 |
| İş Günü Ay Başı | BMS | 2017-04-03 |


unrate.asfreq('MS').info()
DatetimeIndex: 208 entries, 2000-01-01 to 2017-04-01
Freq: MS
Data columns (total 1 columns):
UNRATE 208 non-null float64
dtypes: float64(1)
unrate.resample('MS') # Resampler nesnesi oluşturur
DatetimeIndexResampler [freq=<MonthBegin>, axis=0, closed=left,
label=left, convention=start, base=0]
unrate.asfreq('MS').equals(unrate.resample('MS').asfreq())
True
.resample(): yalnızca başka bir yöntem çağrıldığında veri döndürürgdp = pd.read_csv('gdp.csv')gdp.info()
DatetimeIndex: 69 entries, 2000-01-01 to 2017-01-01
Data columns (total 1 columns):
gpd 69 non-null float64 # frekans bilgisi yok
dtypes: float64(1)
gdp.head(2)
gpd
DATE
2000-01-01 1.2
2000-04-01 7.8
gdp_1 = gdp.resample('MS').ffill().add_suffix('_ffill')
gpd_ffill
DATE
2000-01-01 1.2
2000-02-01 1.2
2000-03-01 1.2
2000-04-01 7.8
gdp_2 = gdp.resample('MS').interpolate().add_suffix('_inter')
gpd_inter
DATE
2000-01-01 1.200000
2000-02-01 3.400000
2000-03-01 5.600000
2000-04-01 7.800000
.interpolate(): mevcut noktalar arasındaki doğru üzerinde değerleri bulurdf1 = pd.DataFrame([1, 2, 3], columns=['df1'])df2 = pd.DataFrame([4, 5, 6], columns=['df2'])pd.concat([df1, df2])
df1 df2
0 1.0 NaN
1 2.0 NaN
2 3.0 NaN
0 NaN 4.0
1 NaN 5.0
2 NaN 6.0
pd.concat([df1, df2], axis=1)
df1 df2
0 1 4
1 2 5
2 3 6
axis=1: yatay birleştirirpd.concat([gdp_1, gdp_2], axis=1).loc['2015':].plot()

pd.concat([unrate, gdp_inter], axis=1).plot();

Python ile Zaman Serisi Verilerini Manipüle Etme