[英]How do I delete time values from Pandas DateTime Index?
Reading from cvs file. 从cvs文件读取。
df = pd.read_csv(filename, sep=";", names=['DateTime','Open','High','Low','Close','Vol'],
parse_dates = [0], index_col = 'DateTime')
Output data: 输出数据:
Open High Low Close
DateTime
2016-01-03 00:00:00+00:00 1.08701 1.08723 1.08451 1.08515
2016-01-04 00:00:00+00:00 1.08701 1.09464 1.07811 1.08239
2016-01-05 00:00:00+00:00 1.08238 1.08388 1.07106 1.07502
2016-01-06 00:00:00+00:00 1.07504 1.07994 1.07185 1.07766
2016-01-07 00:00:00+00:00 1.07767 1.09401 1.07710 1.09256
2016-01-08 00:00:00+00:00 1.09255 1.09300 1.08030 1.09218
How do I delete time values from above DateTime Index? 如何从上述DateTime索引中删除时间值?
Another question, how do I truncate values in Open/High/Low/Close to 6 decimals? 另一个问题,如何截断“开/高/低/接近6位小数”中的值?
To truncate theo datetime, first get the objects into a DateTime object as it does the appropriate handling for this. 要截断日期时间,首先将对象放入DateTime对象中,因为它会对此进行适当的处理。 For example, while printing it will only print the date values as time is always 0. 例如,在打印时它将仅打印日期值,因为时间始终为0。
>>> c = pd.read_csv("a.csv", sep=",", names=['DateTime','Open','High','Low','Close','Vol'])
>>> c
DateTime Open High Low Close Vol
0 2016-01-03 00:00:00+00:00 1.08701 1.08723 1.08451 1.08515 NaN
1 2016-01-04 00:00:00+00:00 1.08701 1.09464 1.07811 1.08239 NaN
2 2016-01-05 00:00:00+00:00 1.08238 1.08388 1.07106 1.07502 NaN
3 2016-01-06 00:00:00+00:00 1.07504 1.07994 1.07185 1.07766 NaN
4 2016-01-07 00:00:00+00:00 1.07767 1.09401 1.07710 1.09256 NaN
5 2016-01-08 00:00:00+00:00 1.09255 1.09300 1.08030 1.09218 NaN
>>> c.DateTime = pd.to_datetime(c.DateTime)
>>> c
DateTime Open High Low Close Vol
0 2016-01-03 1.08701 1.08723 1.08451 1.08515 NaN
1 2016-01-04 1.08701 1.09464 1.07811 1.08239 NaN
2 2016-01-05 1.08238 1.08388 1.07106 1.07502 NaN
3 2016-01-06 1.07504 1.07994 1.07185 1.07766 NaN
4 2016-01-07 1.07767 1.09401 1.07710 1.09256 NaN
5 2016-01-08 1.09255 1.09300 1.08030 1.09218 NaN
This would normally save the time also if it is present, it just doesn't show it here because the time values are 0. To only get the date, do: 如果存在的话,通常也可以节省时间,因为时间值为0,所以这里不显示时间。要只获取日期,请执行以下操作:
>>> c.DateTime = c.DateTime.dt.date
>>> c.DateTime[0]
datetime.date(2016, 1, 3)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.