Home / Example - Datetime

Example - Datetime

You are not logged in.

If you are a current student, please Log In for full access to this page.

Table of Contents

1) ใช้ตัวอย่างข้อมูล `CrudeOil`

import pandas as pd
df = pd.read_csv("./CrudeOil.csv")
df
Date Close/Last Volume Open High Low
0 07/11/2022 104.09 298389.0 104.79 104.57 100.89
1 07/08/2022 104.79 299690.0 102.22 105.24 101.51
2 07/07/2022 102.73 362930.0 98.22 104.48 96.57
3 07/06/2022 98.53 429044.0 100.36 98.88 95.70
4 07/05/2022 99.50 576315.0 108.80 99.99 99.06
... ... ... ... ... ... ...
2543 07/18/2012 89.87 179443.0 89.13 90.04 88.59
2544 07/17/2012 89.22 241731.0 88.26 89.46 87.41
2545 07/16/2012 88.43 203383.0 87.13 88.48 86.41
2546 07/13/2012 87.10 227965.0 85.86 87.61 85.58
2547 07/12/2012 86.08 220665.0 86.20 86.37 84.21

2548 rows × 6 columns

df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 2548 entries, 0 to 2547
Data columns (total 6 columns):
 #   Column      Non-Null Count  Dtype  
---  ------      --------------  -----  
 0   Date        2548 non-null   object 
 1   Close/Last  2548 non-null   float64
 2   Volume      2515 non-null   float64
 3   Open        2548 non-null   float64
 4   High        2548 non-null   float64
 5   Low         2548 non-null   float64
dtypes: float64(5), object(1)
memory usage: 119.6+ KB

เห็นว่า Date เป็น object หรือข้อความธรรมดา

ซึ่งเราสามารถใช้ฟังก์ชัน pd.to_datetime() ในการแปลงข้อมูลให้เป็นประเภท datetime (วันเวลา) ได้

สามารถดูตัวอย่างเพิ่มเติมจาก documentation ได้ที่ https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.to_datetime.html

df["formatted_date"] = pd.to_datetime(df["Date"])
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 2548 entries, 0 to 2547
Data columns (total 7 columns):
 #   Column          Non-Null Count  Dtype         
---  ------          --------------  -----         
 0   Date            2548 non-null   object        
 1   Close/Last      2548 non-null   float64       
 2   Volume          2515 non-null   float64       
 3   Open            2548 non-null   float64       
 4   High            2548 non-null   float64       
 5   Low             2548 non-null   float64       
 6   formatted_date  2548 non-null   datetime64[ns]
dtypes: datetime64[ns](1), float64(5), object(1)
memory usage: 139.5+ KB

จะเห็นว่าคอลัมน์ formatted_date เป็นประเภท datetime64

df
Date Close/Last Volume Open High Low formatted_date
0 07/11/2022 104.09 298389.0 104.79 104.57 100.89 2022-07-11
1 07/08/2022 104.79 299690.0 102.22 105.24 101.51 2022-07-08
2 07/07/2022 102.73 362930.0 98.22 104.48 96.57 2022-07-07
3 07/06/2022 98.53 429044.0 100.36 98.88 95.70 2022-07-06
4 07/05/2022 99.50 576315.0 108.80 99.99 99.06 2022-07-05
... ... ... ... ... ... ... ...
2543 07/18/2012 89.87 179443.0 89.13 90.04 88.59 2012-07-18
2544 07/17/2012 89.22 241731.0 88.26 89.46 87.41 2012-07-17
2545 07/16/2012 88.43 203383.0 87.13 88.48 86.41 2012-07-16
2546 07/13/2012 87.10 227965.0 85.86 87.61 85.58 2012-07-13
2547 07/12/2012 86.08 220665.0 86.20 86.37 84.21 2012-07-12

2548 rows × 7 columns

1.1) เรียงข้อมูลตามวันที่

เรียงข้อมูลตามคอลัมน์ใหม่ formatted_date ที่เพิ่งสร้างขึ้นมา

df.sort_values("formatted_date")
Date Close/Last Volume Open High Low formatted_date
2547 07/12/2012 86.08 220665.0 86.20 86.37 84.21 2012-07-12
2546 07/13/2012 87.10 227965.0 85.86 87.61 85.58 2012-07-13
2545 07/16/2012 88.43 203383.0 87.13 88.48 86.41 2012-07-16
2544 07/17/2012 89.22 241731.0 88.26 89.46 87.41 2012-07-17
2543 07/18/2012 89.87 179443.0 89.13 90.04 88.59 2012-07-18
... ... ... ... ... ... ... ...
4 07/05/2022 99.50 576315.0 108.80 99.99 99.06 2022-07-05
3 07/06/2022 98.53 429044.0 100.36 98.88 95.70 2022-07-06
2 07/07/2022 102.73 362930.0 98.22 104.48 96.57 2022-07-07
1 07/08/2022 104.79 299690.0 102.22 105.24 101.51 2022-07-08
0 07/11/2022 104.09 298389.0 104.79 104.57 100.89 2022-07-11

2548 rows × 7 columns

2) ใช้ตัวอย่างข้อมูล `Bitcoin_History`

df2 = pd.read_csv("./Bitcoin_Historical_Data_1.csv")
df2
Date Price Open High Low Vol. Change %
0 Dec 31, 2021 46,219.5 47,123.3 48,553.9 45,693.6 58.18K -1.92%
1 Dec 30, 2021 47,123.3 46,470.7 47,901.4 46,003.0 60.96K 1.42%
2 Dec 29, 2021 46,461.7 47,548.4 48,121.7 46,127.8 63.92K -2.28%
3 Dec 28, 2021 47,545.2 50,703.4 50,703.8 47,345.7 74.39K -6.18%
4 Dec 27, 2021 50,678.2 50,783.6 52,016.3 50,459.0 43.90K -0.20%
... ... ... ... ... ... ... ...
2187 Jan 05, 2016 431.2 433.3 435.3 428.9 45.03K -0.49%
2188 Jan 04, 2016 433.3 430.7 435.3 428.6 53.01K 0.61%
2189 Jan 03, 2016 430.7 433.7 434.1 423.1 54.83K -0.70%
2190 Jan 02, 2016 433.7 434.0 437.4 430.7 33.57K -0.06%
2191 Jan 01, 2016 434.0 430.0 438.0 425.9 46.97K 0.94%

2192 rows × 7 columns

df2["formatted_date"] = pd.to_datetime(df2["Date"])
df2
Date Price Open High Low Vol. Change % formatted_date
0 Dec 31, 2021 46,219.5 47,123.3 48,553.9 45,693.6 58.18K -1.92% 2021-12-31
1 Dec 30, 2021 47,123.3 46,470.7 47,901.4 46,003.0 60.96K 1.42% 2021-12-30
2 Dec 29, 2021 46,461.7 47,548.4 48,121.7 46,127.8 63.92K -2.28% 2021-12-29
3 Dec 28, 2021 47,545.2 50,703.4 50,703.8 47,345.7 74.39K -6.18% 2021-12-28
4 Dec 27, 2021 50,678.2 50,783.6 52,016.3 50,459.0 43.90K -0.20% 2021-12-27
... ... ... ... ... ... ... ... ...
2187 Jan 05, 2016 431.2 433.3 435.3 428.9 45.03K -0.49% 2016-01-05
2188 Jan 04, 2016 433.3 430.7 435.3 428.6 53.01K 0.61% 2016-01-04
2189 Jan 03, 2016 430.7 433.7 434.1 423.1 54.83K -0.70% 2016-01-03
2190 Jan 02, 2016 433.7 434.0 437.4 430.7 33.57K -0.06% 2016-01-02
2191 Jan 01, 2016 434.0 430.0 438.0 425.9 46.97K 0.94% 2016-01-01

2192 rows × 8 columns