当前位置:网站首页>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边栏推荐
- Oracle 數據泵導錶
- Basic query
- PHP syntax summary
- Multipass中文文档-设置驱动
- Compiling and installing phpredis extension on MAC
- Multipass Chinese document - share data with instances
- [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
- Laravel pay payment access process
- Introduction to markdown grammar
- PIP batch complete uninstall package
猜你喜欢

Essential foundation of programming - Summary of written interview examination sites - computer network (1) overview

图像翻译/GAN:Unsupervised Image-to-Image Translation with Self-Attention Networks基于自我注意网络的无监督图像到图像的翻译

修改Oracle连接数

Yapi cross domain request plug-in installation

1.11 learning summary

2.22.2.14
![Laravel framework Alipay payment fails to receive asynchronous callback request [original]](/img/a7/139604ec3a977f2a4e96f392e56602.jpg)
Laravel framework Alipay payment fails to receive asynchronous callback request [original]
![PHP design function getmaxstr to find the longest symmetric string in a string - [original]](/img/45/d8dae9e605a2f411683db7a2d40d0b.jpg)
PHP design function getmaxstr to find the longest symmetric string in a string - [original]

企业的产品服务怎么进行口碑营销?口碑营销可以找人代做吗?

Advanced learning of MySQL (learning from Shang Silicon Valley teacher Zhou Yang)
随机推荐
Realize video call and interactive live broadcast in the applet
Guide de la pompe de données Oracle
Redis cluster mode
An unexpected attempt (Imperial CMS list template filters spaces and newlines in smalltext introduction)
Motivational skills for achieving goals
Comment enregistrer une image dans une applet Wechat
Tp6 multi table Association (table a is associated with table B, table B is associated with table C, and table d)
Oracle data pump table
Anti withdrawal test record
2022.2.13
Multipass中文文档-设置驱动
PHP splits a string into arrays
2020-12-18
Thymeleaf data echo, single selection backfill, drop-down backfill, time frame backfill
JWT token authentication verification
Basic query
Be a hard worker from today on
Laravel file stream download file
Use shell script to analyze system CPU, memory and network throughput
Multipass Chinese document - use packer to package multipass image