About processing date data using dt accessor of Python's Pandas library
After converting from the object to date data with the to_datetime function etc. Change to any date type or extract date data of a specific part
First, create time data
import pandas as pd
date_data = pd.DataFrame({'date':
['2020-04-01 01:01:01',
'2021-04-02 02:02:02',
'2022-04-03 03:03:03',
'2023-04-04 04:04:04',
'2024-05-05 05:05:05']})
date_data
date | |
---|---|
0 | 2020-04-01 01:01:01 |
1 | 2021-04-02 02:02:02 |
2 | 2022-04-03 03:03:03 |
3 | 2023-04-04 04:04:04 |
4 | 2024-05-05 05:05:05 |
Let's see the type of data
date_data.dtypes
date object
dtype: object
Currently date_data ['date'] type is object
Let's change this to a data type that can be treated as a date first
date_data['date'] = pd.to_datetime(date_data['date'])
date_data['date']
0 2020-04-01 01:01:01
1 2021-04-02 02:02:02
2 2022-04-03 03:03:03
3 2023-04-04 04:04:04
4 2024-05-05 05:05:05
Name: date, dtype: datetime64[ns]
The dt accessor is as follows
pandas.Series.dt
Series.dt()[source]
Accessor object for datetimelike properties of the Series values.
date_data['date'].dt.year
0 2020
1 2021
2 2022
3 2023
4 2024
Name: date, dtype: int64
date_data['date'].dt.month
0 4
1 4
2 4
3 4
4 5
Name: date, dtype: int64
date_data['date'].dt.day
0 1
1 2
2 3
3 4
4 5
Name: date, dtype: int64
date_data['date'].dt.second
0 1
1 2
2 3
3 4
4 5
Name: date, dtype: int64
There was a function called strftime. "strf" seems to be an abbreviation for "str format"
date_data['date'].dt.strftime("%y/%m")
0 20/04
1 21/04
2 22/04
3 23/04
4 24/05
Name: date, dtype: object
ex:2002/04/01 If you change% y →% Y, it will be 4 digits.
date_data['date'].dt.strftime("%Y/%M/%d")
0 2020/01/01
1 2021/02/02
2 2022/03/03
3 2023/04/04
4 2024/05/05
Name: date, dtype: object
That's all for now.
Recommended Posts