当前位置:网站首页>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.612976Date of creation
>>> t1=dt(1999,5,23)
>>> print(t1)
1999-05-23 00:00:00Yes 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
NoneParts 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边栏推荐
- numpy 索引及切片
- 1.24 learning summary
- 0622 horse palm fell 9%
- What is the best way to store chat messages in a database? [Close] - best way to store chat messages in a database? [closed]
- Multipass中文文档-远程使用Multipass
- 文件上传与安全狗
- The statistics in the MySQL field become strings, and then they are converted into numbers for sorting
- Multipass中文文档-提高挂载性能
- 1.19 learning summary
- redis集群的方式
猜你喜欢

Navicat connects the pit of shardingsphere sub table and sub library plug-ins

1.16 learning summary

Differences between TCP and UDP

1.21 learning summary

0622 horse palm fell 9%

2021/11/6-burpsuit packet capturing and web page source code modification

The statistics in the MySQL field become strings, and then they are converted into numbers for sorting

PIP batch complete uninstall package
![[H5 development] 01 take you to experience H5 development from a simple page ~ the whole page implementation process from static page to interface adjustment manual teaching](/img/e4/27611abdd000019e70f4447265808c.jpg)
[H5 development] 01 take you to experience H5 development from a simple page ~ the whole page implementation process from static page to interface adjustment manual teaching

Minecraft 1.16.5 biochemical 8 module 1.9 version 1.18 version synchronization
随机推荐
Thinkphp6 implements a simple lottery system
"Eight hundred"
Notes on enterprise wechat development [original]
Multipass中文文档-与实例共享数据
Motivational skills for achieving goals
PSIM software learning ---08 call of C program block
1.18 learning summary
2021-02-07
Yapi cross domain request plug-in installation
图像翻译/GAN:Unsupervised Image-to-Image Translation with Self-Attention Networks基于自我注意网络的无监督图像到图像的翻译
PHP design function getmaxstr to find the longest symmetric string in a string - [original]
Alipay failed to verify the signature (sandbox test indicates fishing risk?) [original]
Navicat connects the pit of shardingsphere sub table and sub library plug-ins
为什么许多shopify独立站卖家都在用聊天机器人?一分钟读懂行业秘密!
Oracle 數據泵導錶
微信小程序保存图片的方法
Multipass Chinese document - use multipass service to authorize the client
1.11 learning summary
Floyd
SixTool-多功能多合一代挂助手源码