[PYTHON] [Pandas] Basics of processing date data using dt

About processing date data using dt accessor of Python's Pandas library

Case

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

data

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]

Date data processing

I want to take only the year, month, day, and seconds

The dt accessor is as follows

Series.dt

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

If you want a specific type such as year / month (ex: 17/01)

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

[Pandas] Basics of processing date data using dt
Example of efficient data processing with PANDAS
100 language processing knock-20 (using pandas): reading JSON data
100 Language Processing Knock-32 (using pandas): Prototype of verb
Basics of pandas for beginners ② Understanding data overview
100 Language Processing Knock-36 (using pandas): Frequency of word occurrence
100 Language Processing Knock: Chapter 2 UNIX Command Basics (using pandas)
Process csv data with python (count processing using pandas)
python: Basics of using scikit-learn ①
Data analysis using python pandas
Data processing tips with Pandas
Pandas basics for beginners ④ Handling of date and time items
Pandas basics for beginners ① Reading & processing
Pandas basics for beginners ⑧ Digit processing
Data visualization method using matplotlib (+ pandas) (3)
100 Language Processing Knock-31 (using pandas): Verb
Recommendation of data analysis using MessagePack
Data visualization method using matplotlib (+ pandas) (4)
100 Language Processing Knock-38 (using pandas): Histogram
[Introduction to Data Scientists] Basics of scientific calculation, data processing, and how to use graph drawing library ♬ Basics of Pandas
Basics of binarized image processing with Python
Python: Basics of image recognition using CNN
Basics of Quantum Information Theory: Data Compression (1)
[For beginners] Script within 10 lines (5. Resample of time series data using pandas)
100 Language Processing Knock-93 (using pandas): Calculate the accuracy rate of analogy tasks
100 Language Processing Knock-33 (using pandas): Sahen noun
100 Language Processing Knock-91: Preparation of Analogy Data
Pandas basics
100 Language Processing Knock-35 (using pandas): Noun concatenation
[Python] Various data processing using Numpy arrays
Basics of Tableau Basics (Visualization Using Geographic Information)
Pandas basics
100 Language Processing Knock-39 (using pandas): Zipf's Law
Basics of Quantum Information Theory: Data Compression (2)
100 Language Processing Knock-34 (using pandas): "A B"
[Introduction to Data Scientists] Basics of Python ♬
Awareness of using Aurora Severless Data API
Use decorators to prevent re-execution of data processing
100 language processing knock-98 (using pandas): Ward's method clustering
100 language processing knock-75 (using scikit-learn): weight of features
100 language processing knock-99 (using pandas): visualization by t-SNE
Python introductory study-output of sales data using tuples-
100 language processing knock-95 (using pandas): Rating with WordSimilarity-353
Check the status of your data using pandas_profiling
Scraping the winning data of Numbers using Docker
Read pandas data
Basics of Python ①
Basics of python ①
Image processing by matrix Basics & Table of Contents-Reinventor of Python image processing-
100 language processing knock-92 (using Gensim): application to analogy data
Analysis of financial data by pandas and its visualization (2)
Full-width and half-width processing of CSV data in Python
Basics of I / O screen using tkinter in python3
[Python] [Word] [python-docx] Simple analysis of diff data using python
Get Amazon RDS (PostgreSQL) data using SQL with pandas
Analysis of financial data by pandas and its visualization (1)
How to scrape horse racing data using pandas read_html
Drawing on Jupyter using the plot function of pandas
Performance verification of data preprocessing in natural language processing
Summary of Pandas methods used when extracting data [Python]
100 Language Processing Knock-83 (using pandas): Measuring word / context frequency