程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

How to use datetime in Python

編輯:Python


The new titanium cloud service has been accumulatively shared with you 654 Technical dry goods

All data will be automatically allocated with one at the beginning “DOB”( Date of birth ). therefore , Date and time data will inevitably be encountered when processing data at some time . This tutorial will take you through Python Medium datetime Module and use some peripheral libraries , Such as pandas and pytz.

stay Python in , Anything related to date and time is done by datetime Module processing , It further divides the modules into 5 Different classes . A class is simply a data type corresponding to an object . The figure below summarizes Python Medium 5 Date time classes, as well as common properties and examples .

3 A useful fragment

1、 Convert string to date time format , Maybe it's using datetime The most common situation .

Due to the alphanumeric nature of the date and time , Parse similar dates and times into Python It is usually interpreted as a string . In this section , We will show you how to parse a string list into a datetime format , And how to split and combine date and time data into columns in the data frame .

fragment 1 The printout of

however , What if the date time is formatted in an unusual or ambiguous way ? A common problem is the difference between the date and time writing methods in the United States and Europe . In American format , Month before , And in the European style , The date is before .

By default ,pandas Medium to_datetime By reducing the previous one to 12 position (<12) To convert an object to a date time... By parsing the number of . for example ,2/11/2021 Will be resolved to 2021-02-11, but 13/11/2021 Will be resolved to 2021-11-13. however , Having this format parameter allows you to define the format in other ways .

perhaps , strftime() Method helps to format the datetime before returning a string . In the following example , Dash between original date and time (-) Replace with backslash (/), Digital month (02) Replace with abbreviated English terms (Feb).

Because there are many ways to interpret dates ( Japan 、 month 、 year ) And time ( when 、 branch 、 second ), So it's important to understand the different format codes . The following table is a memo of common format codes .

2、 Use time zones

No time zone information datetime Objects are called “naive”, The object of the zone information ( Usually with at the end +HH:MM Corresponding GMT) Is considered to be “aware”.pytz May be Python One of the most comprehensive libraries in , It simplifies the task of time zone calculation .

The following code snippet will show you how to “naive” and “aware” datetime Convert between objects , And you can use different time zones . The last part of the code also demonstrates how to convert a given datetime object to a local time zone . This example shows the time zone codes for Japan and Germany , For other regions , You can refer to here .

fragment 2 Output

3、 Use interval Compare the two datetime(s)

Sometimes we have to compare two dates and times conditionally . Imagine , You have two data frames , The first data frame contains only one column of date and time , The second data frame contains two columns representing the interval and other information in the remaining columns . Your goal is to find a matching date and time from the first data frame , If it falls within the interval of the second data frame , If it is , Copy other columns .

One way to do this is to use pd.Interval Compress the interval between two dates , They are then assigned as indexes to the data frame , It can be used later to conditionally compare and map dates and times . If the time condition is met , This can be done by using for Loop through the columns of interest to complete .

original text :https://towardsdatascience.com/how-to-work-with-datetime-in-python-26d4092dc484

Learn about the new titanium cloud service

New titanium cloud clothing won the fourth FMCG Retail consumer goods industry CIO The annual meeting of the 「 The most trusted brand award for digital services of the year 」

The new titanium cloud suit is three years old , The company's monthly revenue exceeds 600 Ten thousand yuan , Set the development goal of new titanium for a century

When IPFS Meet cloud services | New titanium cloud service has reached a strategic agreement with glacier distributed Laboratory

The new titanium cloud service was officially approved by the Ministry of industry and information technology ISP/IDC( Including Internet resource collaboration ) License plate

I'm going deep into my major , Standing at the head of the turtle , The new titanium cloud service won tens of millions Pre-A Round of funding

New titanium cloud clothes , Create the most professional Cloud MSP+, Bridge between enterprise business and cloud

The first anniversary of the new titanium cloud service , Complete two rounds of financing , Serve more than 50 customers

Hybrid cloud solution of a warehouse logistics e-commerce company in Shanghai

Previous technical dry goods

Kubernetes Expand to 7,500 The course of the node

Low code development , Development by the whole people , Eliminate professional programmers !

Domestic mainstream public cloud VPC Use comparison and summary

Ten thousand words long text : Cloud Architecture Design Principles | attach PDF download

just ,OpenStack The first 19 Here comes a version , attach 28 Detailed interpretation of item characteristics !

Ceph OSD Troubleshooting | Ten thousand words experience summary

Seven for Docker and Kubernetes Protective safety tools

The lifelong growth of O & M people , Start with inventory management | Ten thousand words long text !

OpenStack And ZStack Depth contrast : framework 、 Deploy 、 Computing storage and networking 、 Operation and maintenance monitoring, etc

What is Yunyuan ?

IT Hybrid cloud strategy : What is it? 、 Why? , How to build ?

Share

Poke at


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved