如何将列设置为日期索引?

bik*_*ser 13 timedelta dataframe pandas

我的数据集如下:

       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
Run Code Online (Sandbox Code Playgroud)

我用这段代码来读取这些数据

df.set_index(df['Date'], drop=False, append=False, inplace=False, verify_integrity=False).drop('Date', 1)
Run Code Online (Sandbox Code Playgroud)

但问题是索引不是日期格式.那么问题是如何将此列设置为日期索引?

EdC*_*ica 24

您的问题缺乏正确的解释,但您可以执行以下操作:

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
Run Code Online (Sandbox Code Playgroud)

所以这里to_datetime将日期字符串转换为datetimedtype,set_indexparam inplace=True就是你所需要的,


tdy*_*tdy 6

如果您要从文件加载数据,请在加载时使用parse_dates和,例如:index_col

df = pd.read_csv('data.csv', parse_dates=['Date'], index_col=['Date'])

#             Value
# Date             
# 1988-01-01   0.62
# 1988-01-02   0.64
# ...
# 1988-01-23   0.60
Run Code Online (Sandbox Code Playgroud)
df.index

# DatetimeIndex(['1988-01-01', '1988-01-02', ..., '1988-01-23'],
#               dtype='datetime64[ns]', name='Date', freq=None)
Run Code Online (Sandbox Code Playgroud)

parse_dates大多数read_*方法都支持: