python used to the date and time format conversion!

  Each encounter a column of date formatting problems always pandas dataframe zai pit, below the record about common date and time functions ....

  1, the date string into str-> date

import datetime
date_str = '2006-01-03'(http://www.xcjlnk.com)
date_ = datetime.datetime.strptime(date_str,'%Y-&m-%d')

  It is the conversion of a single string, "% Y-% m-% d" indicates the format of the date string, if date_str = '2006/1/3', may be written as "% Y /% m /% d "and so on.

  In general, we often have a column dataframe operate:

  Applications can apply functions:

def strptime_row(rowi):(http://www.0834xcjl.com)
return datetime.datetime.strptime(rowi,'%Y/%m/%d')
df['date'] = df['date'].apply(strptime_row)

  May apply () function efficiency is relatively low, and we should have a special function for a column date format operations, such as

import pandas as pd
df['date'] = pd.to_datetime(df['date'])

  to_datetime () function can resolve a number of different date representation (such as "7/6/2011", June 7, 2011), the standard date format (such as ISO8601) resolved very quickly.

  There parse () function, can identify almost all human beings can understand date representation (but unfortunately not Chinese), such as:

from dateutil.parser import parse(http://nk.xcmnyy.com)
parse('Jan 31,2008 10:45 AM')

  2, into a string date

  It may be used strftime () function

These are the issues relating to the date and time format conversion python small series to introduce in, we want to help.

Guess you like

Origin www.cnblogs.com/HanaKana/p/12102002.html