利用numpy和pandas处理csv文件中的时间2
2017-07-29 16:58
716 查看
在机器学习和深度学习的过程中,对于处理预测,回归问题,有时候变量是时间,需要进行合适的转换处理后才能进行学习分析,关于时间的变量如下所示,利用pandas和numpy对csv文件中时间进行处理。
上一篇博客中datetime.datetime.toordinal()将日期转为数字,例如2017-12-12—》XXXXXX
利用time模块的time.mktime可以将时间转为数字例如2017-12-12 06:45:23 —》XXXXXX
上一篇博客中datetime.datetime.toordinal()将日期转为数字,例如2017-12-12—》XXXXXX
利用time模块的time.mktime可以将时间转为数字例如2017-12-12 06:45:23 —》XXXXXX
#coding:utf-8 import time import pandas as pd import numpy as np import pickle #用pandas将时间转为标准格式 dateparse = lambda dates: pd.datetime.strptime(dates,'%d/%m/%Y %H:%M:%S') #将时间栏合并,并转为标准时间格式 rawdata = pd.read_csv('RealMarketPriceDataPT.csv',parse_dates={'timeline':['date','(UTC)']},date_parser=dateparse) """ #定义一个将日期转为数字的函数,s为字符串 def datestr2num(s): #toordinal()将日期格式字符串转为数字 return datetime.datetime.strptime(s,'%Y-%m-%d %H:%M:%S').toordinal() """ #定义一个函数将时间转为数字 def daystr2num(s): x_convert = time.strptime(s,'%Y-%m-%d %H:%M:%S') return int(time.mktime(x_convert)) #x_convert = time.strptime(str(rawdata.ix[i,0]),'%Y-%m-%d %H:%M:%S') #x_convert = int(time.mktime(x_convert)) x = [] y = [] new_date = [] for i in range(rawdata.shape[0]): x_convert = daystr2num(str(rawdata.ix[i,0])) #new_date存储int类型的时间戳 new_date.append(x_convert) y_convert = rawdata.ix[i,1].astype(np.float32) x.append(x_convert) y.append(y_convert) x = np.array(x).astype(np.float32)/1000000000 #x = np.diff(x) #y = np.diff(y) #预测的data pred_value = [] new_pred_date = [] pred = ['2017-01-01 00:00:00','2017-02-01 10:00:00','2017-03-05 05:00:00','2017-04-18 21:00:00','2017-05-22 04:00:00', '2017-06-15 05:00:00','2017-07-03 19:00:00','2017-08-08 13:00:00','2017-09-29 23:00:00','2017-10-01 09:00:00'] for i in range(len(pred)): pred_convert = daystr2num(pred[i]) #new_pred_date存储int类型的时间戳 new_pred_date.append(pred_convert) pred_value.append(pred_convert) pred_value = np.array(pred_value).astype(np.float32)/1000000000 with open('price.pickle','wb') as f: pickle.dump((x,y,pred_value,pred),f) #定义一个函数将时间戳转为时间 def strtotime(int_a): timearray = time.localtime(int_a) return time.strftime('%Y-%m-%d %H:%M:%S',timearray) #timearray = time.localtime(new_date[0]) #otherstyletime = time.strftime('%Y-%m-%d %H:%M:%S',timearray) #print(x[0:50]) #print(y[0:50]) #print(pred_value[0:10]) #print(strtotime(new_date[0]),'------>>>>>>',new_date[0]) #print(strtotime(new_date[10]),'------>>>>>>',new_date[10]) #print(strtotime(new_date[20]),'------>>>>>>',new_date[20]) #print(strtotime(new_date[30]),'------>>>>>>',new_date[30]) #print(strtotime(new_date[40]),'------>>>>>>',new_date[40]) #print(strtotime(new_date[50]),'------>>>>>>',new_date[50])
结果
相关文章推荐
- 利用numpy和pandas处理csv文件中的时间
- 利用numpy和pandas处理csv文件中的时间方法
- 利用NumPy和pandas对CSV文件进行写操作
- 利用python的csv库处理csv文件
- Python利用pandas计算多个CSV文件数据值
- 利用pandas模块读取csv文件和excel表格,并用matplotlib画图
- 利用Pandas读取文件路径或文件名称包含中文的csv文件方法
- 利用Pandas读取文件路径或文件名称包含中文的csv文件
- Python使用pandas处理CSV文件
- python用pandas工具包来处理.csv文件,包括读写和切片。
- 建议42:使用pandas处理大型CSV文件
- 利用pandas进行大文件计数处理
- 利用pandas进行大文件计数处理的方法
- python时间日期函数与利用pandas进行时间序列处理详解
- 使用pandas将numpy中的数组数据保存到csv文件
- Python使用pandas处理CSV文件的实例讲解
- 使用pandas将numpy中的数组数据保存到csv文件的方法
- 处理淘宝双11数据,使用pandas库快速切分海量csv文件
- 读取csv文件并使用pandas.Series.apply进行处理时,对header=?的处理
- Python利用pandas计算多个CSV文件数据值的实例