我正在运行此简短代码,无论文件到达df.reset_index时我在做什么,都反复看到此错误。
hold = pd.read_excel('/filepath/GTRFigure16.xlsx', sheetname = 'Data')
df = hold.copy()
df.reset_index(inplace = True)
给我的印象是,当我导入正在使用的excel文件时,它正在创建包含完全为NaN列的多索引。如果是这样,我不确定该如何解决,如果不是,我就没有主意。
这是错误: 追溯(最近一次通话):
File "/home/us_grains/us_gulf_vessel_loading_activity/us_gulf_vessel_loading_activity.py", line 102, in <module>
df.reset_index(inplace = True)
File "/usr/local/lib/python2.7/site-packages/pandas/core/frame.py", line 3024, in reset_index
level_values = _maybe_casted_values(lev, lab)
File "/usr/local/lib/python2.7/site-packages/pandas/core/frame.py", line 2989, in _maybe_casted_values
values = values.take(labels)
IndexError: cannot do a non-empty take from an empty axes.
以下是发生这种情况的数据框示例:
import pandas as pd
import numpy as np
import datetime
df = pd.DataFrame({u' ': {(np.nan, u'Date', np.nan): np.nan,
(np.nan, datetime.datetime(1995, 1, 11, 0, 0), u'01/11/1995'): np.nan,
(np.nan, np.nan, np.nan): u'In Port',
(np.nan, datetime.datetime(1995, 1, 4, 0, 0), u'01/04/1995'): np.nan},
u'Due': {(np.nan, u'Date', np.nan): u'prior 4-yr avg',
(np.nan, datetime.datetime(1995, 1, 11, 0, 0), u'01/11/1995'): np.nan,
(np.nan, np.nan, np.nan): u'10-days',
(np.nan, datetime.datetime(1995, 1, 4, 0, 0), u'01/04/1995'): np.nan},
u'Gulf': {(np.nan, u'Date', np.nan): np.nan,
(np.nan, datetime.datetime(1995, 1, 11, 0, 0), u'01/11/1995'): 36,
(np.nan, np.nan, np.nan): u'In Port',
(np.nan, datetime.datetime(1995, 1, 4, 0, 0), u'01/04/1995'): 39},
u'In port': {(np.nan, u'Date', np.nan): u'average',
(np.nan, datetime.datetime(1995, 1, 11, 0, 0), u'01/11/1995'): np.nan,
(np.nan, np.nan, np.nan): u'4-year',
(np.nan, datetime.datetime(1995, 1, 4, 0, 0), u'01/04/1995'): np.nan},
u'Loaded': {(np.nan, u'Date', np.nan): u'4-year avg',
(np.nan, datetime.datetime(1995, 1, 11, 0, 0), u'01/11/1995'): np.nan,
(np.nan, np.nan, np.nan): u'prior',
(np.nan, datetime.datetime(1995, 1, 4, 0, 0), u'01/04/1995'): np.nan},
u'PNW': {(np.nan, u'Date', np.nan): np.nan,
(np.nan, datetime.datetime(1995, 1, 11, 0, 0), u'01/11/1995'): np.nan,
(np.nan, np.nan, np.nan): u'In Port',
(np.nan, datetime.datetime(1995, 1, 4, 0, 0), u'01/04/1995'): np.nan},
'Unnamed: 10': {(np.nan, u'Date', np.nan): u'7-Days',
(np.nan, datetime.datetime(1995, 1, 11, 0, 0), u'01/11/1995'): np.nan,
(np.nan, np.nan, np.nan): u'Loaded',
(np.nan, datetime.datetime(1995, 1, 4, 0, 0), u'01/04/1995'): np.nan},
'Unnamed: 11': {(np.nan, u'Date', np.nan): u'10-days',
(np.nan, datetime.datetime(1995, 1, 11, 0, 0), u'01/11/1995'): np.nan,
(np.nan, np.nan, np.nan): u'Due',
(np.nan, datetime.datetime(1995, 1, 4, 0, 0), u'01/04/1995'): np.nan},
'Unnamed: 12': {(np.nan, u'Date', np.nan): u'Week #',
(np.nan, datetime.datetime(1995, 1, 11, 0, 0), u'01/11/1995'): 2,
(np.nan, np.nan, np.nan): np.nan,
(np.nan, datetime.datetime(1995, 1, 4, 0, 0), u'01/04/1995'): 1},
'Unnamed: 14': {(np.nan, u'Date', np.nan): np.nan,
(np.nan, datetime.datetime(1995, 1, 11, 0, 0), u'01/11/1995'): np.nan,
(np.nan, np.nan, np.nan): u'Loaded',
(np.nan, datetime.datetime(1995, 1, 4, 0, 0), u'01/04/1995'): np.nan},
'Unnamed: 2': {(np.nan, u'Date', np.nan): u'7-Days',
(np.nan, datetime.datetime(1995, 1, 11, 0, 0), u'01/11/1995'): 64,
(np.nan, np.nan, np.nan): u'Loaded',
(np.nan, datetime.datetime(1995, 1, 4, 0, 0), u'01/04/1995'): 58},
'Unnamed: 4': {(np.nan, u'Date', np.nan): u'10-days',
(np.nan, datetime.datetime(1995, 1, 11, 0, 0), u'01/11/1995'): 72,
(np.nan, np.nan, np.nan): u'Due',
(np.nan, datetime.datetime(1995, 1, 4, 0, 0), u'01/04/1995'): 62},
'Unnamed: 7': {(np.nan, u'Date', np.nan): u'7-Days',
(np.nan, datetime.datetime(1995, 1, 11, 0, 0), u'01/11/1995'): np.nan,
(np.nan, np.nan, np.nan): u'Loaded',
(np.nan, datetime.datetime(1995, 1, 4, 0, 0), u'01/04/1995'): np.nan},
'Unnamed: 8': {(np.nan, u'Date', np.nan): u'10-days',
(np.nan, datetime.datetime(1995, 1, 11, 0, 0), u'01/11/1995'): np.nan,
(np.nan, np.nan, np.nan): u'Due',
(np.nan, datetime.datetime(1995, 1, 4, 0, 0), u'01/04/1995'): np.nan},
u'Vancouver': {(np.nan, u'Date', np.nan): np.nan,
(np.nan, datetime.datetime(1995, 1, 11, 0, 0), u'01/11/1995'): np.nan,
(np.nan, np.nan, np.nan): u'In Port',
(np.nan, datetime.datetime(1995, 1, 4, 0, 0), u'01/04/1995'): np.nan}})