[Python] Split the date

When analyzing time series data, use columns such as timestamps 「2016-12-17 09:59:17」 It may be saved in a format like this. From this format, so that it can be handled by machine learning ** How to split a date into year, month, day, hour, and day of the week **

This time, we will use the following dummy data.

time.py


import pandas as pd
df = pd.read_csv('df.csv')
df.head()

#output
    patient	Last UpDdated
0	5.0	    2020-03-22 10:00:00
1	4.0	    2020-03-22 11:00:00
2	6.0	    2020-03-22 12:00:00
3	10.0	2020-03-23 10:00:00
4	3.0	    2020-03-23 11:00:00

df.info()
#output
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 21 entries, 0 to 20
Data columns (total 3 columns):
patient          21 non-null float64
Last UpDdated    21 non-null object
dtypes: float64(2), object(1)
memory usage: 800.0+ bytes

Splits the date in the Last Up Dated column. ■ Order ** ① Convert from object type to datetime64 [ns] type **   pd.to_datetime(df['Last UpDdated']) ** ②. dt. Get year, month, day, time, day of the week with ~ **   df['Last UpDdated'].dt.month

time.py


df['Last UpDdated'] = pd.to_datetime(df['Last UpDdated']) #Convert type
df.dtypes

#output
patient                 float64
Last UpDdated    datetime64[ns]
dtype: object

#Added column "manth"
df['month'] = df['Last UpDdated'].dt.month
#Added column "day"
df['day'] = df['Last UpDdated'].dt.day
#Added column "hour"
df['hour'] = df['Last UpDdated'].dt.hour
#Added column "week"
df['week'] = df['Last UpDdated'].dt.dayofweek
#Removed Last Up Ddated
df = df.drop(['Last UpDdated'],axis=1)

df.head()

#output
	patient	month  day	hour  week
0	5.0	    3	   22	10	  6
1	4.0	    3	   22	11	  6
2	6.0	    3	   22	12	  6
3	10.0	3	   23	10	  0
4	3.0	    3	   23	11	  0

Month, day, hour, and day of the week columns have been added based on the values in the Last UpDated column! The day of the week is an int type from 0 to 6.

Recommended Posts

[Python] Split the date
Python date arithmetic
Find the maximum Python
[Python] How to change the date format (display format)
ffmpeg-Build a python environment and split the video
the zen of Python
Check the date of the flag duty with Python
[Python] Get the last updated date of the website
Python and Ruby split
[Python2] Date string-> UnixTime-> Date string
[Python3] Get date diff
Get date in Python
Get date with python
Date calculation in Python
Get the update date of the Python memo file.
python3 Measure the processing speed.
[python] Convert date to string
Towards the retirement of Python2
Download the file in Python
Find the difference in Python
Compare the Python array-like guys
About the Python module venv
About the ease of Python
About the enumerate function (python)
[Python] Adjusting the color bar
[Python] Get the previous month
[Python 2/3] Parse the format string
Call the API with python3.
String date manipulation in Python
About the features of Python
[Python] Check the installed libraries
Sort by date in python
I downloaded the python source
The Power of Pandas: Python
Find the maximum python (improved)
Get the current date and time in Python, considering the time difference
Difference between Ruby and Python split
Leave the troublesome processing to Python
[Python] Check the current directory, move the directory
Extract the xz file with python
The story of Python and the story of NaN
Getting the arXiv API in Python
Check the behavior when assigning Python
[Python] Find the second smallest value.
[Python] The stumbling block of import
Python
First Python 3 ~ The beginning of repetition ~
Python in the browser: Brython's recommendation
Save the binary file in Python
AtCoder: Python: Daddy the sample test.
[Introduction to Python] How to split a character string with the split function
Hit the Sesami API in Python
Try the Python LINE Pay SDK
Specifying the date with the Twitter API
Get the site update date seriously
"The easiest Python introductory class" modified
[Python] What is @? (About the decorator)
Existence from the viewpoint of Python
Get the weather with Python requests 2
pyenv-change the python version of virtualenv
In the python command python points to python3.8