【问题标题】:AttributeError: 'NoneType' object has no attribute 'columns'AttributeError:“NoneType”对象没有属性“列”
【发布时间】:2023-03-16 00:27:01
【问题描述】:

我想使用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'

【问题讨论】:

  • 2 种可能性 - 1) self.dataset` 被错误地设置为 None,2) 你对 Python 的研究还不够深入,无法意识到 None 对象没有像 @987654328 这样的属性@.

标签: python pandas dataframe numpy machine-learning


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2019-01-01
    • 2021-12-26
    • 2019-07-23
    • 2018-05-13
    • 2020-09-07
    • 2017-05-03
    • 2018-07-14
    • 2013-06-16
    • 2015-06-15
    相关资源
    最近更新 更多