即使日期列设置为索引,日期也不起作用 [英] Date is not working even when date column is set to index
本文介绍了即使日期列设置为索引,日期也不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个多数据框字典,其中的索引设置为日期",但是在捕获特定搜索日期时遇到了麻烦.
I have a multiple dataframe dictionary where the index is set to 'Date' but am having a trouble to capture the specific day of a search.
按链接创建的字典:
然后,我尝试添加以下列以为每一行创建特定的日期:
Then I tried to add the following column to create specific days for each row:
df_dict[k]['Day'] = pd.DatetimeIndex(df['Date']).day
它不起作用.这个想法是为每一行只分隔月份中的某天(从1到31).当我打电话给报告时,它将给我该事件发生的月份.
It´s not working. The idea is to separate the day of the month only (from 1 to 31) for each row. When I call the report, it will give me the day of month of that occurrence.
更多详细信息(如果需要).
More details if needed.
致谢,谢谢!
推荐答案
- 对于您的代码,没有
'Date'
列,因为它被设置为索引.-
df_dict = {f.stem:pd.read_csv(f,文件中f的parse_dates = ['Date'],index_col ='Date')}
- In the case of your code, there is no
'Date'
column, because it's set as the index.df_dict = {f.stem: pd.read_csv(f, parse_dates=['Date'], index_col='Date') for f in files}
-
df_dict [k] ['Day'] = df.index.day
# here you can see the Date column is set as the index df_dict = {f.stem: pd.read_csv(f, parse_dates=['Date'], index_col='Date') for f in files} data_dict = dict() # create an empty dict here for k, df in df_dict.items(): df_dict[k]['Return %'] = df.iloc[:, 0].pct_change(-1)*100 # create a day column; this may not be needed df_dict[k]['Day'] = df.index.day # aggregate the max and min of Return mm = df_dict[k]['Return %'].agg(['max', 'min']) # get the min and max day of the month date_max = df.Day[df['Return %'] == mm.max()].values[0] date_min = df.Day[df['Return %'] == mm.min()].values[0] # add it to the dict, with ticker as the key data_dict[k] = {'max': mm.max(), 'min': mm.min(), 'max_day': date_max, 'min_day': date_min} # print(data_dict) [out]: {'aapl': {'max': 8.702843218147871, 'max_day': 2, 'min': -4.900700398891522, 'min_day': 20}, 'msft': {'max': 6.603769278967109, 'max_day': 2, 'min': -4.084428935702855, 'min_day': 8}}
这篇关于即使日期列设置为索引,日期也不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
-
查看全文