Home >Backend Development >Python Tutorial >How python handles the time field of dataframe

How python handles the time field of dataframe

php中世界最好的语言
php中世界最好的语言Original
2018-04-12 09:28:153500browse

This time I will show you how python handles the time field of dataframe. What are the precautions for python to handle the time field of dataframe. The following is a practical case, let's take a look.

In the process of machine learning, csv files are usually read through pandas and kept in dadaframe format. However, sometimes it is necessary to model the time field in the dataframe. For example, if the time format is datetime, then it is like general Operating the time field in the same way as the dataframe will result in an error. Therefore, when using the sklearn library for fit and predict, the time field must first be converted to timestamp format. After fit and predict, if matplotlib drawing is required, then The timestamp format is converted into time string , such as 2017-02-01 14:25:14.

The following is a piece of code I have processed, I hope it can help children!

doc_list1 = []
for i in doc1.iloc[:,1:2].values.tolist():        # 转换成了时间戳格式
  for j in i:
    dt = time.strptime(j, "%Y-%m-%d %H:%M:%S")
    dt_new = time.mktime(dt)
    doc_list1.append(dt_new)
doc_list2 = []
for i in doc_list1:
  time_local = time.localtime(i)
  dt = time.strftime("%Y-%m-%d %H:%M:%S",time_local)
  dt1 = datetime.datetime.strptime(dt, "%Y-%m-%d %H:%M:%S")
  doc_list2.append(dt1)
X1 = np.mat(doc_list1).T
y1= test_target1001
clf = AdaBoostRegressor(DecisionTreeRegressor(max_depth=5),n_estimators=1000, random_state=rng)
clf.fit(X1,y1)
yhat1 = clf.predict(X1)

Addition: If the value is not in datetime format, it needs to be converted

value = result.iloc[:,1]
list = []
for i in value:
    print(type(i.to_pydatetime().timetuple()),i)
    list.append(time.mktime(i.to_datetime().timetuple()))
print(list)

I believe you have read the case in this article You have mastered the method. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to convert object into float data

Detailed graphic explanation of v-for iteration syntax in Vue2.0


The above is the detailed content of How python handles the time field of dataframe. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn