Pandas basics for beginners ④ Handling of date and time items

What is pandas

A data frame object for handling structured data in Python. You can easily read files and perform subsequent SQL operations, and it is necessary for work such as machine learning to process, calculate, and visualize data. A memo list of commonly used syntaxes for data manipulation. This section is date-time processing. The table of contents for other items is here.

0. Library import

Import pandas with the name pd

python


import pandas as pd

1. Date / time item conversion

Converts the item described in Object type to datetime64 [ns] type (Timestamp type). Suppose you want to convert the item "Day" in the definition of dataframe.

python


dataframe['Day'] = pd.to_datetime(dataframe['Day'])

2. Range specification

"Day" extracts the data after "April 1, 2020".

python


dataframe = dataframe.loc[dataframe['Day'] > pd.to_datetime('20200401')]

3. Extract only date as object type

datetime64 [ns] Converts Day, which is a type, to a date (object type).

python


dataframe['Day'] = pd.to_datetime(dataframe['Day']).dt.date

Recommended Posts

Pandas basics for beginners ④ Handling of date and time items
Basics of pandas for beginners ② Understanding data overview
Pandas basics for beginners ⑧ Digit processing
[Must-see for beginners] Basics of Linux
Pandas basics summary link for beginners
[For beginners] Script within 10 lines (5. Resample of time series data using pandas)
[Pandas] Basics of processing date data using dt
[Python] Create a list of date and time (datetime type) for a certain period
Prepared for date calculation and automation of my bot
Summary of pre-processing practices for Python beginners (Pandas dataframe)
[Linux] Basics of authority setting by chmod for beginners
[For beginners] Basics of Python explained by Java Gold Part 2
[Python] Create a date and time list for a specified period
Date and time ⇔ character string
Overview of Docker (for beginners)
Seaborn basics for beginners ④ pairplot
100 Pandas knocks for Python beginners
[For beginners] Basics of Python explained by Java Gold Part 1
Seaborn basics for beginners ① Aggregate graph of the number of data (Countplot)
[For beginners] Summary of suffering from kaggle's EDA and its struggle
Seaborn basics for beginners ② Histogram (distplot)
LSTM (1) for time series forecasting (for beginners)
Learn the basics of Python ① Beginners
[For beginners] Why are the "weights" and "bias" of neural networks necessary?
Precautions for handling png and jpg images
[Python] Minutes of study meeting for beginners (7/15)
Import of japandas with pandas 1.0 and above
Get date and time in specified format
A little scrutiny of pandas 1.0 and dask
Implementation and explanation using XGBoost for beginners
Time series data anomaly detection for beginners
Pandas of the beginner, by the beginner, for the beginner [Python]
Pytest Current time test (fixed date and time)
For beginners of SageMaker --Collection of material links -
[For beginners of artificial intelligence] Machine learning / Deep Learning Programming Learning path and reference books
A memorandum of method often used when analyzing data with pandas (for beginners)