简体   繁体   English

如何从熊猫日期时间索引中删除时间值?

[英]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.

相关问题 如何将pandas DataFrame索引从日期时间转换为简单的时间索引 - How to convert pandas DataFrame index from datetime to a simple time index 当索引是日期时间时,如何相互减去两个pandas日期时间系列DataFrame? - How do I subtract two pandas datetime series DataFrames from each other when the index is a datetime? 如何有效地替换 pandas 列中日期时间值的时间部分? - How do I efficiently replace the time portion of datetime values in a pandas column? 如何正确地将日期时间设置为 Pandas dataframe 的索引? - How do I properly set the Datetime as an index for a Pandas dataframe? 如何在 pandas 中将索引字符串更改为日期时间? - How do i change index string to datetime in pandas? 如何为熊猫创建滚动的每月日期时间索引? - How do I create a rolling monthly datetime index for pandas? 如何测试对象是否是熊猫日期时间索引? - How do I test if an object is a pandas datetime index? 如何使用 pandas 中的日期时间索引列表索引 dataframe? - How do I index a dataframe using a list of datetime indices in pandas? 如何从 pandas dataframe 中的时间序列中检测和删除无限值? - How do you detect and delete infinite values from a time series in a pandas dataframe? 如何检查 Pandas 日期时间列是否存在缺失值? - How do I check a Pandas Datetime column for missing values?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM