我需要遍历存储在pandas面板中的数据帧(我知道Panel已过时,我应该使用xarray,但是一旦我知道如何使用xarray,我将稍后进行转换)。当我遍历数据帧列表时,下面的代码有效,但是我需要以这种方式为数据帧分配名称,以方便以后的操作(我将每个数据帧按小数值分割,对每个小数执行操作,然后添加分数又一起返回)。
当我通过面板时-demand_scen_dataset:
demand_scen_dataset = pd.Panel({'low': low_demand_df, 'medium_low': med_low_demand_df, 'medium': bc_demand_df, 'medium_high': med_high_demand_df, 'high': high_demand_df})
我收到以下错误
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~\Anaconda3\lib\site-packages\pandas\core\tools\datetimes.py in _convert_listlike(arg, box, format, name, tz)
376 try:
--> 377 values, tz = conversion.datetime_to_datetime64(arg)
378 return DatetimeIndex._simple_new(values, name=name, tz=tz)
pandas\_libs\tslibs\conversion.pyx in pandas._libs.tslibs.conversion.datetime_to_datetime64()
TypeError: Unrecognized value type: <class 'builtin_function_or_method'>
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
<ipython-input-59-23a09e988e1d> in <module>()
16 demand_dfs_datetime = []
17 for df in demand_scen_dataset:
---> 18 df.index = pd.to_datetime(df.index, format='%Y')
19 df = df.tshift(-92, 'D')
20 df = df.resample('D').ffill()
~\Anaconda3\lib\site-packages\pandas\core\tools\datetimes.py in to_datetime(arg, errors, dayfirst, yearfirst, utc, box, format, exact, unit, infer_datetime_format, origin, cache)
467 result = _convert_listlike(arg, box, format)
468 else:
--> 469 result = _convert_listlike(np.array([arg]), box, format)[0]
470
471 return result
~\Anaconda3\lib\site-packages\pandas\core\tools\datetimes.py in _convert_listlike(arg, box, format, name, tz)
378 return DatetimeIndex._simple_new(values, name=name, tz=tz)
379 except (ValueError, TypeError):
--> 380 raise e
381
382 if arg is None:
~\Anaconda3\lib\site-packages\pandas\core\tools\datetimes.py in _convert_listlike(arg, box, format, name, tz)
345 try:
346 result = array_strptime(arg, format, exact=exact,
--> 347 errors=errors)
348 except tslib.OutOfBoundsDatetime:
349 if errors == 'raise':
pandas\_libs\tslibs\strptime.pyx in pandas._libs.tslibs.strptime.array_strptime()
ValueError: time data <built-in method index of str object at 0x00000244B1D5CCA8> does not match format '%Y' (match)
demand_dfs_list = [low_demand_df, med_low_demand_df, bc_demand_df, med_high_demand_df, high_demand_df]
dates = pd.date_range(start='2020-10-01', end='2070-09-30', freq='D')
demand_dfs_datetime = []
for df in demand_dfs_list:
df.index = pd.to_datetime(df.index, format='%Y')
df = df.tshift(-92, 'D')
df = df.resample('D').ffill()
df = df.reindex(dates)
demand_dfs_datetime.append(df)