Python dates, interchangeable characters

\

Date transfer string


\

1. Read data from Excel. The data format of the date column in Excel is “short date”.

test = pd.read_excel(‘f:/test.xlsx’)

test.head(2)

\

2. Check the data type of the read data

Test [‘ date ‘].apply(lambda x: type(x))

If the datatype is a timestamp, you can use the year, month, and day attributes to get the date and year referenced by the timestamp.

\

  1. Output the information about the date, month and year of the timestamp

Test [‘ date ‘]. Apply (lambda x: print(x.ear, x.month, x.day))

\

4. Convert the timestamp to a string to view the data type

MyString = test [‘ date ‘]. Apply (lambda x: x.s trftime (‘ Y – m – % d % %))

myString.apply(lambda x: type(x))

The output is as follows, all converted to STR

\

String transfer date


\

1. The sample data used looks like this

myString.apply(lambda x: print(x, type(x)))

\

2. String to date denaturation operation

myString.apply(lambda x: type(time.strptime(x, ‘%Y-%m-%d’)))

This transsexual doctor is the built-in strptime function of time. Repeat important things three times

\

A string to a date, time.striptime(), returns a time tuple struct_time

A string to a date, time.striptime(), returns a time tuple struct_time

A string to a date, time.striptime(), returns a time tuple struct_time

\

If you search for “string rotation date” on the Internet, it will tell you this. Time.striptime () is used, but there is no further analysis of what type of result is returned and how to extract the year, month, day, etc.

\

It is called a denaturation operation because after this step, the nature of the string to date change has been implemented. But the possible form, as shown in the picture below, is not what you want in the end, and then we have to transform it.

\

3. Metamorphoses of date tuples

Converts a time tuple to a timestamp

myString.apply(lambda x: time.mktime(time.strptime(x, ‘%Y-%m-%d’)))

\

The return value is of type float, still not the form we want, so keep morphing.

myString.apply(lambda x: datetime.datetime.fromtimestamp(time.mktime(time.strptime(x, ‘%Y-%m-%d’))))

Use a datetime. Datetime. Fromtimestamp deformation (), type parameter is a timestamp.

\

We see that the result is done, in the form we want. At this point, the year, month, day attributes can be used merrily again.

myString.apply(lambda x: datetime.datetime.fromtimestamp(time.mktime(time.strptime(x, ‘%Y-%m-%d’))).year)

\

The string is directly extracted year/month/day


\

If our goal is simply to extract the year, month and day information from it, strings are no match for savvy analysts. What kind of guests, what kind of treatment. See recruit open recruit, draw inferences from one another is the core competitiveness of the person. We’ll split it up.

\

myString.apply(lambda x: x.split(‘-‘))

* * * *

Meet the calendar


\

The calendar module’s functions are calendar-related, such as printing a character calendar for a month.

\

import calendar

cal = calendar.month(2017, 9)

print(cal)

\

Blink of an eye this month will arrive at the end of the month, will have a holiday. Learning early, growth every day is essential ~

\

Remember to long click on the image below, identify the qr code in the image, pay attention to “data analyst notes”, follow march sang progress ^_^

\