Inventory of a Pandas date processing problem

Click on " Python Crawler and Data Mining " above to pay attention

Reply to " Books " to get a total of 10 e-books on Python from entry to advanced

now

Day

chicken

Soup

Xianxian suppressed his voice and thought, as if complaining that he had failed in his life.

Hello everyone, I am Pippi.

I. Introduction

A few days ago, I asked a Pythonquestion about date processing in the Python group [The Power of Love], and I will share it with you here.

'2022-03-25 08:00:00.000000000' Guys, is there any easy way to convert a string in this format to 8 o'clock on March 25, 2022?

Here he also wrote a code himself, as follows:

x = '2022-03-25 08:00:00.000000000'
x.split()[0].split('-')[0] + '年' + str(int(x.split()[0].split('-')[1])) + '月' + x.split()[0].split('-')[2] + '日' + str(int(x.split()[1].split(':')[0])) + '时'

It still looks complicated, I hope there is an easier way.

2. Implementation process

Here [Guoguo] gives an Excel version, as follows:

3fe8898b2fdae7b87d7029d1114468d7.png

It is implemented using Excel functions, the formula is: =TEXT(--LEFT(A1,19),"e年m月d日h时"), from the results, the requirements are indeed met. But fans are because it needs to be used in a more complicated program, which is an intermediate step, and excel cannot be used.

Want to use Python to achieve, so how to deal with it? Here is the string format to time format, asking ChatGPT should also have an answer.

from datetime import datetime  
  
date_str = '2022-03-25 08:00:00.000000000'  
date_time = datetime.strptime(date_str, '%Y-%m-%d %H:%M:%S.%f')  
date_time_str = date_time.strftime('%Y年%m月%d日%H时')  
  
print(date_time_str)

However, after the code runs, there will be some small errors, as shown in the following figure:

8094619b0e6df8f4f3489ec688d65c1f.png

Judging from the error report, the following 0 is redundant, and the original string needs to be preprocessed.

Later [F.light] also gave a method, the code is shown in the following figure:

f17925e3c77581ac98a36ccbb3021609.jpeg

The answer is very close. The code obtained is 08:00 on the 3rd, and the answer fans need is the result of 8:00 on the 3rd, 2022. The answer here is still a little flawed. Later, [Peter] gave a feasible code. As shown below:

87ebc142206f1495717f05505d3950e5.png

Integrate the code together, there is an internal taste, as shown in the following figure~

902fd559f6c740b5be33252e4a6901c9.png

3. Summary

Hello everyone, I am Pippi. This article mainly takes stock of a Pandasdate processing problem. Aiming at this problem, the article gives a variety of solutions, as well as specific analysis and code implementation, to help fans solve the problem smoothly.

[Supplementary questions] Warm reminder, when you ask questions in the group. You can pay attention to the following points: if it involves large file data, you can desensitize the data, send some demo data (meaning small files), and then paste some code (the kind that can be copied), and remember to send the screenshot of the error report (complete cut ). If there are not many codes, just send the code text directly. If the code exceeds 50 lines, just send a .py file.

3d126c5b59221f559f199f18a75248ea.png

If you have any problems during the learning process, please feel free to contact me to solve them (my WeChat: pdcfighting1). At the request of fans, I have created some high-quality Python paid learning exchange groups and paid order receiving groups. Welcome everyone to join me Python learning exchange group and order receiving group!

264309d55aa1d50a10778c5212a366db.png

Friends, hurry up and practice it! If you encounter any problems during the learning process, please add me as a friend, and I will pull you into the Python learning exchange group to discuss learning together.

67cf05619d56309b88e2e44b2cf610df.jpeg

------------------- End -------------------

Recommendations for past wonderful articles:

d588f46c4052ba02d3e8710cab9df3b0.png

Welcome everyone to like, leave a message, forward, repost, thank you for your company and support

If you want to join the Python learning group, please reply in the background [ join the group ]

Thousands of rivers and thousands of mountains are always in love, can you click [ Looking ]

/Today's Message Topic/

Just say a few words~~

Guess you like

Origin blog.csdn.net/pdcfighting/article/details/131266493