如何设置列为日期索引? [英] how set column as date index?
本文介绍了如何设置列为日期索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的数据集如下:
Date Value
1/1/1988 0.62
1/2/1988 0.64
1/3/1988 0.65
1/4/1988 0.66
1/5/1988 0.67
1/6/1988 0.66
1/7/1988 0.64
1/8/1988 0.66
1/9/1988 0.65
1/10/1988 0.65
1/11/1988 0.64
1/12/1988 0.66
1/13/1988 0.67
1/14/1988 0.66
1/15/1988 0.65
1/16/1988 0.64
1/17/1988 0.62
1/18/1988 0.64
1/19/1988 0.62
1/20/1988 0.62
1/21/1988 0.64
1/22/1988 0.62
1/23/1988 0.60
我使用此代码读取此数据
I used this code to read this data
df.set_index(df['Date'], drop=False, append=False, inplace=False, verify_integrity=False).drop('Date', 1)
但是问题是索引不是日期格式.那么问题是如何将此列设置为日期索引?
but the problem is index is not in date format. So the question is how to set this column as date index?
推荐答案
您的问题缺少适当的解释,但是您可以执行以下操作:
Your question lacked a proper explanation, but you can do the following:
In [75]:
# convert to datetime
df['Date'] = pd.to_datetime(df['Date'])
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 23 entries, 0 to 22
Data columns (total 2 columns):
Date 23 non-null datetime64[ns]
Value 23 non-null float64
dtypes: datetime64[ns](1), float64(1)
memory usage: 448.0 bytes
In [76]:
# set the index
df.set_index('Date', inplace=True)
df.info()
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 23 entries, 1988-01-01 to 1988-01-23
Data columns (total 1 columns):
Value 23 non-null float64
dtypes: float64(1)
memory usage: 368.0 bytes
因此,此处 to_datetime
将转换日期字符串为datetime
dtype, set_index
您只需使用参数inplace=True
So here to_datetime
will convert date strings to datetime
dtype, set_index
with param inplace=True
is all you need,
这篇关于如何设置列为日期索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文