删除 Pandas 中 DateTime 索引的时间部分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50845338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove time portion of DateTime index in pandas
提问by bloodynri
When I query a service through their API for daily data, they throw in a time portion which is equal to whatever time the query was made. So my pandas dataframe looks like this when I called the function at 14:54:36 -
当我通过他们的 API 查询服务以获取日常数据时,他们会抛出一个时间部分,该部分等于进行查询的任何时间。所以当我在 14:54:36 调用函数时,我的Pandas数据框看起来像这样 -
2018-05-16 14:54:36 1024.75 1008.25 ... 39221 242897
2018-05-17 14:54:36 1017.00 1002.00 ... 35361 241132
2018-05-18 14:54:36 1015.75 1002.75 ... 49090 242938
2018-05-21 14:54:36 1034.50 1020.75 ... 56950 243316
2018-05-22 14:54:36 1043.75 1028.50 ... 49724 247874
2018-05-23 14:54:36 1049.00 1036.25 ... 46256 253609
2018-05-24 14:54:36 1059.75 1047.00 ... 65352 259617
As this is daily data, the time portion is useless. When I do:
由于这是每日数据,因此时间部分没有用。当我做:
data = pd.read_csv(StringIO(data), index_col=0, header=None,names=['High','Low','Open','Close','Volume','OpenInterest'])
data.index = pd.to_datetime(data.index,format="%Y-%m-%d")
The format doesn't seem to work. The DateTime index still contains time. Any idea how I can remove the time portion?
格式好像不行。DateTime 索引仍然包含时间。知道如何删除时间部分吗?
回答by Brad Solomon
With the date
attribute:
使用date
属性:
df.index = df.index.date
Example:
例子:
>>> df = pd.DataFrame([1, 2, 3, 4], index=pd.date_range('2018', periods=4, freq='H'))
>>> df.index = df.index.date
>>> df
0
2018-01-01 1
2018-01-01 2
2018-01-01 3
2018-01-01 4
Note: that this will get you object
dtype in Pandas. All attributes are here. It's technically an array of native Python datetime.date
objects. See ALollz's answer to keep the dtype datetime-like.
注意:这会让你object
在 Pandas 中输入 dtype。所有属性都在这里。从技术上讲,它是本机 Pythondatetime.date
对象的数组。请参阅 ALollz 的答案以保持类似日期时间的 dtype。
回答by ALollz
There is also the option to maintain the datetime functionality but to just set the time portion to 00:00:00 with
还可以选择维护日期时间功能,但只需将时间部分设置为 00:00:00
df.index = df.index.normalize()
import pandas as pd
df = pd.DataFrame([1, 2, 3, 4], index=pd.date_range('2018', periods=4, freq='H'))
df.index = df.index.normalize()
df
is now:
df
就是现在:
0
2018-01-01 1
2018-01-01 2
2018-01-01 3
2018-01-01 4
Looking at the index:
看指数:
df.index
#DatetimeIndex(['2018-01-01', '2018-01-01', '2018-01-01', '2018-01-01'], dtype='datetime64[ns]', freq=None)
And the values are Timestamps:
值是时间戳:
df.index[0]
#Timestamp('2018-01-01 00:00:00')
回答by Antonio Williams
You can control your Index object with a simple function like this:
您可以使用这样的简单函数来控制您的 Index 对象:
def set_date_range(start_date, number_of_periods, frequency):
date_range = pd.date_range(start= start_date, periods=number_of_periods, freq=frequency)
for date in date_range:
print(date)
print()
set_date_range('1/1/2018', 5, "MS")
See the line below with the comment, it'll remove the time portion
请参阅下面的注释行,它将删除时间部分
def set_date_range(start_date, number_of_periods, frequency):
date_range = pd.date_range(start= start_date, periods=number_of_periods, freq=frequency)
date_range = date_range.date # ASSIGNING THIS GETS RID OF THE TIME PORTION
for date in date_range:
print(date)
print()
set_date_range('1/1/2018', 5, "MS")