当前位置:网站首页>Datetime data type ---now() gets the current time, datetime() creation date, performs mathematical operations, and to_ Datetime() converts to date type and extracts various parts of date
Datetime data type ---now() gets the current time, datetime() creation date, performs mathematical operations, and to_ Datetime() converts to date type and extracts various parts of date
2022-06-26 04:45:00 【I am a little monster】
Catalog
Get the current date and time
>>> from datetime import datetime as dt
>>> print(dt.now())
2022-01-11 11:22:23.612976
Date of creation
>>> t1=dt(1999,5,23)
>>> print(t1)
1999-05-23 00:00:00
Yes datetime Do math
>>> t1=dt(1999,5,23)
>>> t2=dt.now()
>>> diff=t2-t1
>>> print(diff)
8269 days, 11:26:14.647381
>>> print(type(diff))
<class 'datetime.timedelta'>
Convert to datetime data type
>>> import pandas as pd
>>> ebola=pd.read_csv(r'D:\pandas Flexible use \pandas_for_everyone-master\data/country_timeseries.csv')
>>> print(ebola.iloc[:5,:5])
Date Day Cases_Guinea Cases_Liberia Cases_SierraLeone
0 1/5/2015 289 2776.0 NaN 10030.0
1 1/4/2015 288 2775.0 NaN 9780.0
2 1/3/2015 287 2769.0 8166.0 9722.0
3 1/2/2015 286 NaN 8157.0 NaN
4 12/31/2014 284 2730.0 8115.0 9633.0
>>> print(ebola.info())
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 122 entries, 0 to 121
Data columns (total 18 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Date 122 non-null object
1 Day 122 non-null int64
2 Cases_Guinea 93 non-null float64
3 Cases_Liberia 83 non-null float64
4 Cases_SierraLeone 87 non-null float64
5 Cases_Nigeria 38 non-null float64
6 Cases_Senegal 25 non-null float64
7 Cases_UnitedStates 18 non-null float64
8 Cases_Spain 16 non-null float64
9 Cases_Mali 12 non-null float64
10 Deaths_Guinea 92 non-null float64
11 Deaths_Liberia 81 non-null float64
12 Deaths_SierraLeone 87 non-null float64
13 Deaths_Nigeria 38 non-null float64
14 Deaths_Senegal 22 non-null float64
15 Deaths_UnitedStates 18 non-null float64
16 Deaths_Spain 16 non-null float64
17 Deaths_Mali 12 non-null float64
dtypes: float64(16), int64(1), object(1)
memory usage: 17.3+ KB
None
>>> ebola['date_dt']=pd.to_datetime(ebola['Date'])
>>> print(ebola.info())
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 122 entries, 0 to 121
Data columns (total 19 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Date 122 non-null object
1 Day 122 non-null int64
2 Cases_Guinea 93 non-null float64
3 Cases_Liberia 83 non-null float64
4 Cases_SierraLeone 87 non-null float64
5 Cases_Nigeria 38 non-null float64
6 Cases_Senegal 25 non-null float64
7 Cases_UnitedStates 18 non-null float64
8 Cases_Spain 16 non-null float64
9 Cases_Mali 12 non-null float64
10 Deaths_Guinea 92 non-null float64
11 Deaths_Liberia 81 non-null float64
12 Deaths_SierraLeone 87 non-null float64
13 Deaths_Nigeria 38 non-null float64
14 Deaths_Senegal 22 non-null float64
15 Deaths_UnitedStates 18 non-null float64
16 Deaths_Spain 16 non-null float64
17 Deaths_Mali 12 non-null float64
18 date_dt 122 non-null datetime64[ns]
dtypes: datetime64[ns](1), float64(16), int64(1), object(1)
memory usage: 18.2+ KB
None
# Convert to date format or specify date format , Be careful format The specified date format is the acquired date format
>>> ebola['date_dt']=pd.to_datetime(ebola['Date'],format='%m/%d/%Y')
>>> print(ebola.iloc[:5,:5])
Date Day Cases_Guinea Cases_Liberia Cases_SierraLeone
0 1/5/2015 289 2776.0 NaN 10030.0
1 1/4/2015 288 2775.0 NaN 9780.0
2 1/3/2015 287 2769.0 8166.0 9722.0
3 1/2/2015 286 NaN 8157.0 NaN
4 12/31/2014 284 2730.0 8115.0 9633.0
>>> ebola['date_dt']=pd.to_datetime(ebola['Date'],format='%d/%m/%Y')
# Due to the output above ebola The data shows that , The fifth row of data has 31, It cannot be used as a month , Therefore, the following error occurs
ValueError: time data '12/31/2014' does not match format '%d/%m/%Y' (match)
For the format of how to represent the date, please refer to Python strftime Behavior _ I am a little monster blog -CSDN Blog
Load data containing dates
When reading the file, we can go through read_csv Parameters in parse_dates To specify the column that you want to resolve to a date
>>> import pandas as pd
>>> ebola=pd.read_csv(r'D:\pandas Flexible use \pandas_for_everyone-master\data/country_timeseries.csv',parse_dates=[0])
>>> print(ebola.info())
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 122 entries, 0 to 121
Data columns (total 18 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Date 122 non-null datetime64[ns]
1 Day 122 non-null int64
2 Cases_Guinea 93 non-null float64
3 Cases_Liberia 83 non-null float64
4 Cases_SierraLeone 87 non-null float64
5 Cases_Nigeria 38 non-null float64
6 Cases_Senegal 25 non-null float64
7 Cases_UnitedStates 18 non-null float64
8 Cases_Spain 16 non-null float64
9 Cases_Mali 12 non-null float64
10 Deaths_Guinea 92 non-null float64
11 Deaths_Liberia 81 non-null float64
12 Deaths_SierraLeone 87 non-null float64
13 Deaths_Nigeria 38 non-null float64
14 Deaths_Senegal 22 non-null float64
15 Deaths_UnitedStates 18 non-null float64
16 Deaths_Spain 16 non-null float64
17 Deaths_Mali 12 non-null float64
dtypes: datetime64[ns](1), float64(16), int64(1)
memory usage: 17.3 KB
None
Parts of the extraction date
year\month\day\quarter
>>> t1=dt(1999,5,23)
>>> print(t1)
1999-05-23 00:00:00
>>> print(type(t1))
<class 'datetime.datetime'>
>>> print(t1.year)
1999
>>> print(t1.month)
5
>>> print(t1.day)
23
>>> print(ebola.loc[0:3,'Date'].dt.quarter)
0 1
1 1
2 1
3 1
边栏推荐
- Problem follow up - PIP source change
- Rdkit chemical formula molecular formula search
- Multipass Chinese documents - improve mount performance
- PHP syntax summary
- 基础查询
- Laravel file stream download file
- Multipass中文文档-远程使用Multipass
- Simple use of redis in laravel
- Simple application of KMP
- An unexpected attempt (Imperial CMS list template filters spaces and newlines in smalltext introduction)
猜你喜欢
Nabicat connection: local MySQL & cloud service MySQL and error reporting
1.21 learning summary
Yapi cross domain request plug-in installation
Tp6 is easy to tread [original]
NVM installation and use and NPM package installation failure record
Motivational skills for achieving goals
企业的产品服务怎么进行口碑营销?口碑营销可以找人代做吗?
File upload and security dog
1.20 learning summary
2022 talent strategic transformation under the development trend of digital economy
随机推荐
图像翻译/GAN:Unsupervised Image-to-Image Translation with Self-Attention Networks基于自我注意网络的无监督图像到图像的翻译
Multipass中文文档-提高挂载性能
redis集群的方式
Redis cluster mode
08_ Spingboot integrated redis
Navicat connects the pit of shardingsphere sub table and sub library plug-ins
I like you!
企业的产品服务怎么进行口碑营销?口碑营销可以找人代做吗?
2021-01-31
Comment enregistrer une image dans une applet Wechat
Database design (I)
ROS 笔记(07)— 客户端 Client 和服务端 Server 的实现
numpy 通用函数
Numpy data input / output
Multipass Chinese document - share data with instances
Simple use of redis in laravel
[H5 development] 03- take you hand in hand to improve H5 development - single submission vs batch submission with a common interface
Multipass中文文档-远程使用Multipass
1.24 learning summary
mysql高级学习(跟着尚硅谷老师周阳学习)