当前位置:网站首页>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边栏推荐
- Multipass Chinese documents - improve mount performance
- Multipass中文文档-使用Packer打包Multipass镜像
- How to carry out word-of-mouth marketing for enterprises' products and services? Can word of mouth marketing be done on behalf of others?
- Sixtool- source code of multi-functional and all in one generation hanging assistant
- Compiling and installing phpredis extension on MAC
- numpy 随机数
- Multipass Chinese document - share data with instances
- Multipass Chinese document - use multipass service to authorize the client
- 2021/11/6-burpsuit packet capturing and web page source code modification
- TP5 distinct method paging problem
猜你喜欢

2.22.2.14

Performance test comparison between PHP framework jsnpp and thinkphp6

修改Oracle连接数

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

PIP batch complete uninstall package

1.12 learning summary

How to use the configured slave data source for the scheduled task configuration class scheduleconfig

NVM installation and use and NPM package installation failure record

1.11 learning summary

Multipass中文文档-远程使用Multipass
随机推荐
Anti withdrawal test record
Multipass中文文档-远程使用Multipass
LISP programming language
Gateway can not connect to tcp://127.0.0.1: Connection refused
Nabicat连接:本地Mysql&&云服务Mysql以及报错
Sort query
Multipass Chinese document - use multipass service to authorize the client
Multipass中文文档-使用实例命令别名
How to use the configured slave data source for the scheduled task configuration class scheduleconfig
Multipass中文文档-移除实例
[H5 development] 03- take you hand in hand to improve H5 development - single submission vs batch submission with a common interface
2.8 learning summary
Text horizontal alignment attribute text align and element vertical alignment attribute vertical align
1.13 learning summary
微信小程序保存圖片的方法
Thymeleaf data echo, single selection backfill, drop-down backfill, time frame backfill
2021/11/6-burpsuit packet capturing and web page source code modification
Differences between TCP and UDP
How can the intelligent transformation path of manufacturing enterprises be broken due to talent shortage and high cost?
NVM installation and use and NPM package installation failure record