test_df
我有一个看起来像这样的熊猫数据框:
reading
01-Jan-2016 00:00:00.000 20.464020
02-Jan-2016 00:00:00.000 22.440950
03-Jan-2016 00:00:00.000 27.181500
04-Jan-2016 00:00:00.000 25.318260
05-Jan-2016 00:00:00.000 25.376050
06-Jan-2016 00:00:00.000 0.067112
07-Jan-2016 00:00:00.000 19.313950
08-Jan-2016 00:00:00.000 26.677340
09-Jan-2016 00:00:00.000 26.801620
10-Jan-2016 00:00:00.000 22.583950
11-Jan-2016 00:00:00.000 0.002765
12-Jan-2016 00:00:00.000 26.496440
13-Jan-2016 00:00:00.000 23.233720
14-Jan-2016 00:00:00.000 23.956080
15-Jan-2016 00:00:00.000 26.958120
16-Jan-2016 00:00:00.000 27.351270
17-Jan-2016 00:00:00.000 28.348710
18-Jan-2016 00:00:00.000 25.494090
19-Jan-2016 00:00:00.000 26.342880
20-Jan-2016 00:00:00.000 24.645530
问题:给定一个像 '2016-01' aka 'yyyy-mm' 这样的字符串,我想知道any
指定月份的条目是否存在于 pandas dataframe 的索引中test_df
。
我期待的是True
'2016-01' 和False
任何其他字符串。寻找最简洁的方法来做到这一点。
问题设置:
为方便起见,这是获取测试数据帧的代码:
import pandas as pd
temp_df = pd.read_json('{"reading":{"01-Jan-2016 00:00:00.000":20.46402,"02-Jan-2016 00:00:00.000":22.44095,"03-Jan-2016 00:00:00.000":27.1815,"04-Jan-2016 00:00:00.000":25.31826,"05-Jan-2016 00:00:00.000":25.37605,"06-Jan-2016 00:00:00.000":0.06711243,"07-Jan-2016 00:00:00.000":19.31395,"08-Jan-2016 00:00:00.000":26.67734,"09-Jan-2016 00:00:00.000":26.80162,"10-Jan-2016 00:00:00.000":22.58395,"11-Jan-2016 00:00:00.000":0.002765084,"12-Jan-2016 00:00:00.000":26.49644,"13-Jan-2016 00:00:00.000":23.23372,"14-Jan-2016 00:00:00.000":23.95608,"15-Jan-2016 00:00:00.000":26.95812,"16-Jan-2016 00:00:00.000":27.35127,"17-Jan-2016 00:00:00.000":28.34871,"18-Jan-2016 00:00:00.000":25.49409,"19-Jan-2016 00:00:00.000":26.34288,"20-Jan-2016 00:00:00.000":24.64553}}')
我试过了:
>>'2016-01' in test_df.index
False