在自定义函数中使用set_index

时间:2016-09-18 14:22:36

标签: python pandas

我想将列中的日期观察值转换为我的数据帧的索引。我可以使用以下代码执行此操作:

示例数据:

test = pd.DataFrame({'Values':[1,2,3], 'Date':["1/1/2016 17:49","1/2/2016 7:10","1/3/2016 15:19"]})

索引代码:

test['Date Index'] = pd.to_datetime(test['Date'])
test = test.set_index('Date Index')
test['Index'] = test.index.date

但是,当我尝试将此代码包含在函数中时,我可以创建'日期索引'但set_index列似乎没有按预期工作。

def  date_index(df):
     df['Date Index'] = pd.to_datetime(df['Date'])
     df = df.set_index('Date Index')
     df['Index'] = df.index.date

如果我检查不使用函数info()的输出,则返回:

<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 3 entries, 2016-01-01 17:49:00 to 2016-01-03 15:19:00
Data columns (total 3 columns):
Date      3 non-null object
Values    3 non-null int64
Index     3 non-null object
dtypes: int64(1), object(2)
memory usage: 96.0+ bytes

如果我检查函数info()的输出,则返回:

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 2 columns):
Date      3 non-null object
Values    3 non-null int64
dtypes: int64(1), object(1)
memory usage: 120.0+ bytes

我想要DatetimeIndex

如何在函数中使用set_index?我使用不正确吗?

1 个答案:

答案 0 :(得分:0)

IIUC return df遗失:

df1 = pd.DataFrame({'Values':[1,2,3], 'Exam Completed Date':["1/1/2016 17:49","1/2/2016 7:10","1/3/2016 15:19"]})

def  date_index(df):
     df['Exam Completed Date Index'] = pd.to_datetime(df['Exam Completed Date'])
     df = df.set_index('Exam Completed Date Index')
     df['Index'] = df.index.date
     return df

print (date_index(df1))
                          Exam Completed Date  Values       Index
Exam Completed Date Index                                        
2016-01-01 17:49:00            1/1/2016 17:49       1  2016-01-01
2016-01-02 07:10:00             1/2/2016 7:10       2  2016-01-02
2016-01-03 15:19:00            1/3/2016 15:19       3  2016-01-03
print (date_index(df1).info())
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 3 entries, 2016-01-01 17:49:00 to 2016-01-03 15:19:00
Data columns (total 3 columns):
Exam Completed Date    3 non-null object
Values                 3 non-null int64
Index                  3 non-null object
dtypes: int64(1), object(2)
memory usage: 96.0+ bytes
None