Python date and string are converted to each other and finally get the __python.

Source: Internet
Author: User

Python date, character interchange

Date Spin string

1. Read data from Excel, in Excel date this column data format type is "Short Date"

Test = Pd.read_excel (' f:/test.xlsx ')

Test.head (2)

2. View data types for read data

test[' Date '].apply (lambda x:type (x))

The data type is a timestamp, and the Year,month,day property gets the month-and-day information that the timestamp refers to.

Output timestamp corresponding to the month and day information

test[' Date '].apply (lambda x:print (x.year, X.month,, the output results are as follows:

4. Convert timestamps to strings, view data types

myString = test[' Date '].apply (lambda x:x.strftime ('%y-%m-%d '))

Mystring.apply (Lambda x:type (x))

The output is as follows and has all been converted to STR types

String Turn Date

1. The sample data used is long like this

Mystring.apply (Lambda x:print (x, type (x))

2. String to date denaturing operation

Mystring.apply (Lambda x:type (time.strptime (x, '%y-%m-%d '))

This transsexual doctor is the Strptime built-in function of time, the important thing to say three times

String to date, using Time.striptime (), the return value type is a time tuple struct_time

String to date, using Time.striptime (), the return value type is a time tuple struct_time

String to date, using Time.striptime (), the return value type is a time tuple struct_time

If you retrieve the "string date" on the Internet, you will generally be told this. Uses Time.striptime (), but does not continue to analyze what type of result is returned, how to extract the date, month, day, and so on.

It is called a transsexual operation because after this step, the actual change of string to date has been implemented. But the form that may be rendered, as shown in the following figure, is not what you ultimately want, and then we have to deform the operation.

3. Date tuple of the deformation record

Convert a time tuple to a timestamp

Mystring.apply (Lambda x:time.mktime (time.strptime (x, '%y-%m-%d '))

The return value is float, still not the form we want, and continues to deform.

Mystring.apply (Lambda X:datetime.datetime.fromtimestamp (Time.mktime (Time.strptime (x, '%y-%m-%d ')))

Using Datetime.datetime.fromtimestamp () to deform, the parameter is a timestamp type.

See the result is done, is the form we want. This time, and can be cheerful use of year,month,day properties.

Mystring.apply (Lambda X:datetime.datetime.fromtimestamp (Time.mktime (Time.strptime (x, '%y-%m-%d ')). Year)

String Direct extraction year/month/day

If our purpose is simply to extract the information of the year and the month, the string is completely difficult to get out of the smart analyst. What kind of guests, what kind of stay. See recruit and dismantle recruit, extrapolate is the core competition of person. Split it up with a split and get it done.

Mystring.apply (Lambda x:x.split ('-'))

See the Calendar.

The functions of the Calendar module are calendar-related, such as printing a month's character calendar.

Import Calendar

Cal = Calendar.month (2017, 9)

Print (CAL)

In an instant this month will be the end of the day, will be a holiday. Learn to multiply early, grow daily essential ~

Remember the long press the image below, identify the two-dimensional code in the map, pay attention to "data analyst Notes", with the March Mulberry progress ^_^

Related Article

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: 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.