Python-pandas the operation of time in learning data __python

Source: Internet
Author: User
Tags timedelta

There are very, very many operations on the processing of time this property in pandas. You can refer to the following links:

Pandas

And this article on one of the people may be more unfamiliar to explain the method. I will upload the rest.

The application scenario is this: given a dataset, the data set has a user's registered account time (year-month-day), as shown in the following figure format.


If we want to convert the user account registration time to a specific number of days, we can use the following code.

Import pandas as PD
td=data[' User_reg_tm ']
time=pd.to_datetime (TD)
Start=pd.datetime (2016,4,16)
Day=start-time

Finally, insert the number of days into the original table

data[' Day ']=day




—————————————————————————————— Split Line ——————————————————

Let's say a few details about creating a time.

DATE=PD. Series ([' 2016411 '])
pd.to_datetime (date)
This creates a time value of 2016-4-11.

Here is a detail, is the time format in the string, if there is no separation between year and month, pandas will automatically use-number separate, if you want to manually separate, for example

DATE=PD. Series ([' 2016-4-11 '])
This can also be, or use/number. Note, however, that you can only use-or/to separate and not use anything else.

Sometimes we need information that is sometimes minutes and seconds.

DATE=PD. Series ([' 2016-4-11 12:12:12 '])


Finally, let's say the next question, how do we take the "days" alone after the number of days I get?

Very simple, with. Days to visit.

For series type, use Data.dt.days

For Timedelta types, direct access to Data.days is available.

For example:

Because data[' Day ' is series type

data[' Day '].dt.days

Because day is a timedelta type of day.days

___________________________________________________________________________


Update: Time to process the next link click Open link

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.