การ Indexing และ Resampling Time Series

การจัดการข้อมูล Time Series ใน Python

Stefan Jansen

Founder & Lead Data Scientist at Applied Artificial Intelligence

การแปลง time series

การแปลง time series เบื้องต้น ได้แก่:

  • แปลง string วันที่ให้เป็น datetime64

  • เลือกและ slice ข้อมูลช่วงเวลาที่ต้องการ

  • กำหนดและเปลี่ยนความถี่ของ DateTimeIndex

    • Upsampling vs Downsampling
การจัดการข้อมูล Time Series ใน Python

ดึงราคาหุ้น GOOG

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
การจัดการข้อมูล Time Series ใน Python

แปลง string วันที่เป็น datetime64

  • pd.to_datetime():
    • แปลง string วันที่
    • แปลงเป็น datetime64
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)
การจัดการข้อมูล Time Series ใน Python

แปลง string วันที่เป็น datetime64

  • .set_index():
    • ตั้งวันที่เป็น index
    • inplace:
      • ไม่สร้างสำเนาใหม่
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)
การจัดการข้อมูล Time Series ใน Python

พล็อตกราฟ time series ราคาหุ้น Google

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

plt.tight_layout(); plt.show()

ch1_2_v2 - Indexing & Resampling Time Series.013.png

การจัดการข้อมูล Time Series ใน Python

Partial string indexing

  • การ index โดยใช้ string ที่แปลงเป็นวันที่ได้
google['2015'].info() # Pass string for part of date
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() # Slice includes last month
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
การจัดการข้อมูล Time Series ใน Python

Partial string indexing

google.loc['2016-6-1', 'price'] # Use full date with .loc[]
734.15
การจัดการข้อมูล Time Series ใน Python

.asfreq(): กำหนดความถี่

  • .asfreq('D'):
    • แปลง DateTimeIndex เป็นความถี่รายวัน (ตามปฏิทิน)
google.asfreq('D').info() # set calendar day frequency
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)
การจัดการข้อมูล Time Series ใน Python

.asfreq(): กำหนดความถี่

  • Upsampling:
    • ความถี่สูงขึ้น => มีวันที่ใหม่ => ข้อมูลขาดหาย
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
การจัดการข้อมูล Time Series ใน Python

.asfreq(): เปลี่ยนความถี่

  • .asfreq('B'):
    • แปลง DateTimeIndex เป็นความถี่วันทำการ
google = google.asfreq('B') # Change to calendar day frequency

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)
การจัดการข้อมูล Time Series ใน Python

.asfreq(): เปลี่ยนความถี่

google[google.price.isnull()] # Select missing 'price' values
            price
date             
2015-01-19    NaN
2015-02-16    NaN
...
2016-11-24    NaN
2016-12-26    NaN
  • วันทำการที่ไม่ใช่วันซื้อขาย
การจัดการข้อมูล Time Series ใน Python

มาฝึกกันเถอะ!

การจัดการข้อมูล Time Series ใน Python

Preparing Video For Download...