AttributeError: '对象没有属性'columns'



我想通过使用meas_var = data.detect_groups_times()['groups'] if meas_var is None else meas_var代码更新数据的默认值。它调用colnames = list(self.dataset.columns.values),然后抛出错误AttributeError: 'NoneType' object has no attribute 'columns'

input_path = "../input_data/samples/"
# Open all the subfolders within path
for root, dirs, files in os.walk(input_path):
for file in files:
with open(os.path.join(root, file), "r") as data:
data_file = pd.read_csv(data)
#data_file = '../sample_data/Synthetic_Univariate.zip'
# data_file = '../sample_data/GrowthFactor_ErkAkt_Bivariate.zip'
meas_var = None
start_time = None
end_time = None
class DataProcesser:
def __init__(self, archive_path, col_id='ID', col_class='class', col_classname='class_name', col_set='set', read_on_init=True, **kwargs):
....
def detect_groups_times(self, return_groups=True, return_times=True, return_times_pergroup=True):
"""
Detect the measurement groups and the time range spanned by these measurements.
:param return_groups: bool, whether to return the unique groups names.
:param return_times: bool, whether to return the global time range.
:param return_times_pergroup: bool, whether to return the time range per group.
:return: dict, with keys groups, times, times_pergroup.
"""
if return_times_pergroup and not return_times:
return_times = True
warnings.warn('"return_times" is False but "return_times_pergroup" is True. "return_times" will be set to True.')
out = {}
colnames = list(self.dataset.columns.values)
colnames.remove(self.col_id)
colnames.remove(self.col_class)
groups = list(OrderedDict.fromkeys([i.split('_')[0] for i in colnames]))
if return_groups:
out['groups'] = groups
if return_times:
times = [int(i.split('_')[1]) for i in colnames]
out['times'] = [min(times), max(times)]
if return_times_pergroup:
out['times_pergroup'] = {}
for group in groups:
group_columns = [i for i in colnames if search('^{0}_'.format(group), i)]
group_times = [int(i.split('_')[1]) for i in group_columns]
out['times_pergroup'][group] = [min(group_times), max(group_times)]
return out

# Update default for the data
meas_var = data.detect_groups_times()['groups'] if meas_var is None else meas_var
start_time = data.detect_groups_times()['times'][0] if start_time is None else start_time
end_time = data.detect_groups_times()['times'][1] if end_time is None else end_time

回溯

> --------------------------------------------------------------------------- AttributeError                            Traceback (most recent call
> last) /tmp/ipykernel_19124/540157375.py in <module>
>       3 
>       4 # Update default for the data
> ----> 5 meas_var = data.detect_groups_times()['groups'] if meas_var is None else meas_var
>       6 start_time = data.detect_groups_times()['times'][0] if start_time is None else start_time
>       7 end_time = data.detect_groups_times()['times'][1] if end_time is None else end_time
> 
> ~/CODEX/Notebooks/../source/load_data.py in detect_groups_times(self,
> return_groups, return_times, return_times_pergroup)
>     212             warnings.warn('"return_times" is False but "return_times_pergroup" is True. "return_times" will be set to True.')
>     213         out = {}
> --> 214         colnames = list(self.dataset.columns.values)
>     215         colnames.remove(self.col_id)
>     216         colnames.remove(self.col_class)
> 
> AttributeError: 'NoneType' object has no attribute 'columns'

通常我会只是评论(还不够),但是:您的问题是self.dataset为None。由于您没有显示整个代码,我无法回溯错误。一个可能的问题是,您使用df = df.*whatever_method*(inplace=True)调用Pandas的方法,导致该方法返回None,从而将df设置为None。

最新更新