如何修复“pandas.core.common”没有属性“AbstractMethodError”?

Posted

技术标签:

【中文标题】如何修复“pandas.core.common”没有属性“AbstractMethodError”?【英文标题】:How to fix 'pandas.core.common' has no attribute 'AbstractMethodError'? 【发布时间】:2019-07-09 19:56:12 【问题描述】:

我想看看可用于 pandas 对象的方法。当我运行这段代码时,我得到了一个 AttributeError 错误。我已搜索但未找到此错误的示例或如何修复它。

for i in (df_jobs.groupby(['group', 'failed'])['failed']):
    object_methods = [method_name for method_name in dir(i[1])
                      if callable(getattr(i[1], method_name))]
    break

AttributeError                            Traceback (most recent call last)
<ipython-input-322-70ac95067677> in <module>
     54 #     print(i[1].count())  # YES YES YES
     55 
---> 56     object_methods = [method_name for method_name in dir(i[1])
     57                       if callable(getattr(i[1], method_name))]
     58     break

<ipython-input-322-70ac95067677> in <listcomp>(.0)
     55 
     56     object_methods = [method_name for method_name in dir(i[1])
---> 57                       if callable(getattr(i[1], method_name))]
     58     break
     59 

~\Anaconda3\lib\site-packages\pandas\core\generic.py in __getattr__(self, name)
   4374             if self._info_axis._can_hold_identifiers_and_holds_name(name):
   4375                 return self[name]
-> 4376             return object.__getattribute__(self, name)
   4377 
   4378     def __setattr__(self, name, value):

~\Anaconda3\lib\site-packages\pandas\core\generic.py in _constructor_sliced(self)
    222         original, such as DataFrame single columns slicing.
    223         """
--> 224         raise com.AbstractMethodError(self)
    225 
    226     @property

AttributeError: module 'pandas.core.common' has no attribute 'AbstractMethodError'

编辑 这是从数据帧df_jobs 和示例输出中重现一行的代码:

sample_row = pd.DataFrame([['g1', 'u1', 3902779, '2018-09-27 21:38:06', '2018-10-01 07:24:38', '2018-10-01 08:00:42', 0, 0, 'single', 1, 55696, 609865728.0, 4.0, 6.0, 0, 0, 4.0, 0, 'single', 1, 0, pd.Timedelta('3 days 09:46:32'), pd.Timedelta('00:36:04')]], 
                          columns=['group', 'owner', 'job_number', 'submission_time', 'start_time', 'end_time', 'failed', 'exit_status', 'granted_pe', 'slots', 'task_number', 'maxvmem', 'h_data', 'h_rt', 'highp', 'exclusive', 'h_vmem', 'gpu', 'pe', 'slot', 'campus', 'wait_time', 'wtime'])

sample_row = (sample_row.astype(dtype='group':'str', 'owner':'str', 'job_number':'int', 'submission_time':'datetime64[ns]', 'start_time':'datetime64[ns]', 'end_time':'datetime64[ns]', 'failed':'int', 'exit_status':'int', 'granted_pe':'str', 'slots':'int', 'task_number':'int', 'maxvmem':'float', 'h_data':'float', 'h_rt':'float', 'highp':'int', 'exclusive':'int', 'h_vmem':'float', 'gpu':'int', 'pe':'str', 'slot':'int', 'campus':'int', 'wait_time':'timedelta64[ns]', 'wtime':'timedelta64[ns]'))

print(sample_row)

输出:

   group     owner  job_number     submission_time          start_time  \
1  yxing  yidazhan  3902779    2018-09-27 21:38:06 2018-10-01 07:24:38   

             end_time  failed  exit_status granted_pe  slots  task_number  \
1 2018-10-01 08:00:42  0       0            single     1      55696         

       maxvmem  h_data  h_rt  highp  exclusive  h_vmem  gpu      pe  slot  \
1  609865728.0  4.0     6.0   0      0          4.0     0    single  1      

   campus       wait_time    wtime  
1  0      3 days 09:46:32 00:36:04  

Windows 10 我本周卸载/重新安装了 Anaconda。 熊猫 0.23.4 py37h830ac7b_0

服务器信息: 您正在使用 Jupyter 笔记本。

笔记本服务器版本为:5.7.4-f6790e4 服务器正在此版本的 Python 上运行: Python 3.7.1(默认,2018 年 12 月 10 日,22:54:23)[MSC v.1915 64 位 (AMD64)]

当前内核信息: Python 3.7.1(默认,2018 年 12 月 10 日,22:54:23)[MSC v.1915 64 位 (AMD64)] IPython 7.2.0——增强的交互式 Python。类型 '?'寻求帮助。


编辑 2 (2/16/19)

输入

import pandas as pd

df_jobs = pd.DataFrame([['g1', 'u1', 3902779, '2018-09-27 21:38:06', '2018-10-01 07:24:38', '2018-10-01 08:00:42', 0, 0, 'single', 1, 55696, 609865728.0, 4.0, 6.0, 0, 0, 4.0, 0, 'single', 1, 0, pd.Timedelta('3 days 09:46:32'), pd.Timedelta('00:36:04')]], 
                          columns=['group', 'owner', 'job_number', 'submission_time', 'start_time', 'end_time', 'failed', 'exit_status', 'granted_pe', 'slots', 'task_number', 'maxvmem', 'h_data', 'h_rt', 'highp', 'exclusive', 'h_vmem', 'gpu', 'pe', 'slot', 'campus', 'wait_time', 'wtime'])

df_jobs = (df_jobs.astype(dtype='group':'str', 'owner':'str', 'job_number':'int', 'submission_time':'datetime64[ns]', 'start_time':'datetime64[ns]', 'end_time':'datetime64[ns]', 'failed':'int', 'exit_status':'int', 'granted_pe':'str', 'slots':'int', 'task_number':'int', 'maxvmem':'float', 'h_data':'float', 'h_rt':'float', 'highp':'int', 'exclusive':'int', 'h_vmem':'float', 'gpu':'int', 'pe':'str', 'slot':'int', 'campus':'int', 'wait_time':'timedelta64[ns]', 'wtime':'timedelta64[ns]'))

grouped_df_jobs = df_jobs.groupby(['group', 'failed'])

for params, table in grouped_df_jobs:
    object_methods_params = [method_name for method_name in dir(params)
                             if callable(getattr(params, method_name))]
    print('params:', str(params), '\n\nparams methods:\n', object_methods_params, '\n')

    object_methods_table  = [method_name for method_name in dir(table)
                             if callable(getattr(table, method_name))]
    print('table:\n', str(table), '\n\ntable methods:\n', object_methods_table, '\n')

    for index, row in table.iterrows():
        object_methods_index = [method_name for method_name in dir(index)
                         if callable(getattr(index, method_name))]
        print('index:', str(index), '\n\ntable index methods:\n', object_methods_index, '\n')

        object_methods_row  = [method_name for method_name in dir(row)
                                 if callable(getattr(row, method_name))]        
        print('row:\n', str(row), '\n\ntable row methods:\n', object_methods_row)

    break

输出

params: ('aaamini', 37) 

params methods:
 ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index'] 

table:
           group     owner  job_number     submission_time          start_time  \
412327  aaamini  sjalilkt  3939911    2018-10-04 06:16:57 2018-10-04 06:17:49   

                  end_time  failed  exit_status granted_pe  slots  \
412327 2018-10-04 08:20:02  37      0            single     1       

        task_number  maxvmem  h_data  h_rt  highp  exclusive  h_vmem  gpu  \
412327  0            0.0      4.0     2.0   0      0          0.0     0     

            pe  slot  campus wait_time    wtime  
412327  single  1     1      00:00:52  02:02:13   

table methods:
 ['__abs__', '__add__', '__and__', '__array__', '__array_wrap__', '__bool__', '__bytes__', '__class__', '__contains__', '__copy__', '__deepcopy__', '__delattr__', '__delitem__', '__dir__', '__div__', '__eq__', '__finalize__', '__floordiv__', '__format__', '__ge__', '__getattr__', '__getattribute__', '__getitem__', '__getstate__', '__gt__', '__hash__', '__iadd__', '__iand__', '__ifloordiv__', '__imod__', '__imul__', '__init__', '__init_subclass__', '__invert__', '__ior__', '__ipow__', '__isub__', '__iter__', '__itruediv__', '__ixor__', '__le__', '__len__', '__lt__', '__matmul__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__nonzero__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdiv__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rmatmul__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__setitem__', '__setstate__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__unicode__', '__xor__', '_add_numeric_operations', '_add_series_only_operations', '_add_series_or_dataframe_operations', '_agg_by_level', '_aggregate', '_aggregate_multiple_funcs', '_align_frame', '_align_series', '_box_col_values', '_box_item_values', '_check_inplace_setting', '_check_is_chained_assignment_possible', '_check_label_or_level_ambiguity', '_check_percentile', '_check_setitem_copy', '_clear_item_cache', '_clip_with_one_bound', '_clip_with_scalar', '_combine_const', '_combine_frame', '_combine_match_columns', '_combine_match_index', '_compare_frame', '_consolidate', '_consolidate_inplace', '_construct_axes_dict', '_construct_axes_dict_for_slice', '_construct_axes_dict_from', '_construct_axes_from_arguments', '_constructor', '_constructor_expanddim', '_constructor_sliced', '_convert', '_count_level', '_create_indexer', '_dir_additions', '_dir_deletions', '_drop_axis', '_drop_labels_or_levels', '_ensure_valid_index', '_expand_axes', '_find_valid_index', '_from_arrays', '_from_axes', '_get_agg_axis', '_get_axis', '_get_axis_name', '_get_axis_number', '_get_axis_resolvers', '_get_block_manager_axis', '_get_bool_data', '_get_cacher', '_get_index_resolvers', '_get_item_cache', '_get_label_or_level_values', '_get_numeric_data', '_get_value', '_getitem_array', '_getitem_column', '_getitem_frame', '_getitem_multilevel', '_getitem_slice', '_gotitem', '_iget_item_cache', '_indexed_same', '_info_repr', '_init_dict', '_init_mgr', '_init_ndarray', '_is_builtin_func', '_is_copy', '_is_cython_func', '_is_label_or_level_reference', '_is_label_reference', '_is_level_reference', '_ixs', '_join_compat', '_maybe_cache_changed', '_maybe_update_cacher', '_needs_reindex_multi', '_protect_consolidate', '_reduce', '_reindex_axes', '_reindex_axis', '_reindex_columns', '_reindex_index', '_reindex_multi', '_reindex_with_indexers', '_repr_data_resource_', '_repr_fits_horizontal_', '_repr_fits_vertical_', '_repr_html_', '_repr_latex_', '_reset_cache', '_reset_cacher', '_sanitize_column', '_set_as_cached', '_set_axis', '_set_axis_name', '_set_is_copy', '_set_item', '_set_value', '_setitem_array', '_setitem_frame', '_setitem_slice', '_setup_axes', '_shallow_copy', '_slice', '_take', '_to_dict_of_blocks', '_try_aggregate_string_function', '_unpickle_frame_compat', '_unpickle_matrix_compat', '_update_inplace', '_validate_dtype', '_where', '_xs', 'abs', 'add', 'add_prefix', 'add_suffix', 'agg', 'aggregate', 'align', 'all', 'any', 'append', 'apply', 'applymap', 'as_matrix', 'asfreq', 'asof', 'assign', 'astype', 'at', 'at_time', 'between_time', 'bfill', 'bool', 'boxplot', 'clip', 'clip_lower', 'clip_upper', 'combine', 'combine_first', 'compound', 'copy', 'corr', 'corrwith', 'count', 'cov', 'cummax', 'cummin', 'cumprod', 'cumsum', 'describe', 'diff', 'div', 'divide', 'dot', 'drop', 'drop_duplicates', 'dropna', 'duplicated', 'eq', 'equals', 'eval', 'ewm', 'expanding', 'ffill', 'fillna', 'filter', 'first', 'first_valid_index', 'floordiv', 'from_dict', 'from_records', 'ge', 'get', 'get_dtype_counts', 'get_ftype_counts', 'get_values', 'groupby', 'gt', 'head', 'hist', 'iat', 'idxmax', 'idxmin', 'iloc', 'infer_objects', 'info', 'insert', 'interpolate', 'isin', 'isna', 'isnull', 'items', 'iteritems', 'iterrows', 'itertuples', 'ix', 'join', 'keys', 'kurt', 'kurtosis', 'last', 'last_valid_index', 'le', 'loc', 'lookup', 'lt', 'mad', 'mask', 'max', 'mean', 'median', 'melt', 'memory_usage', 'merge', 'min', 'mod', 'mode', 'mul', 'multiply', 'ne', 'nlargest', 'notna', 'notnull', 'nsmallest', 'nunique', 'pct_change', 'pipe', 'pivot', 'pivot_table', 'plot', 'pop', 'pow', 'prod', 'product', 'quantile', 'query', 'radd', 'rank', 'rdiv', 'reindex', 'reindex_axis', 'reindex_like', 'rename', 'rename_axis', 'reorder_levels', 'replace', 'resample', 'reset_index', 'rfloordiv', 'rmod', 'rmul', 'rolling', 'round', 'rpow', 'rsub', 'rtruediv', 'sample', 'select', 'select_dtypes', 'sem', 'set_axis', 'set_index', 'shift', 'skew', 'slice_shift', 'sort_index', 'sort_values', 'squeeze', 'stack', 'std', 'sub', 'subtract', 'sum', 'swapaxes', 'swaplevel', 'tail', 'take', 'to_clipboard', 'to_csv', 'to_dense', 'to_dict', 'to_excel', 'to_feather', 'to_gbq', 'to_hdf', 'to_html', 'to_json', 'to_latex', 'to_msgpack', 'to_panel', 'to_parquet', 'to_period', 'to_pickle', 'to_records', 'to_sparse', 'to_sql', 'to_stata', 'to_string', 'to_timestamp', 'to_xarray', 'transform', 'transpose', 'truediv', 'truncate', 'tshift', 'tz_convert', 'tz_localize', 'unstack', 'update', 'var', 'where', 'xs'] 

index: 412327 

table index methods:
 ['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', '__init__', '__init_subclass__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'from_bytes', 'to_bytes'] 

C:\Users\karls\Anaconda3\lib\site-packages\ipykernel_launcher.py:77: DeprecationWarning: 
.ix is deprecated. Please use
.loc for label based indexing or
.iloc for positional indexing

See the documentation here:
http://pandas.pydata.org/pandas-docs/stable/indexing.html#ix-indexer-is-deprecated
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-463-513651be1e59> in <module>
     83         print('index:', str(index), '\n\ntable index methods:\n', object_methods_index, '\n')
     84 
---> 85         object_methods_row  = [method_name for method_name in dir(row)
     86                                  if callable(getattr(row, method_name))]        
     87         print('row:\n', str(row), '\n\ntable row methods:\n', object_methods_row)

<ipython-input-463-513651be1e59> in <listcomp>(.0)
     84 
     85         object_methods_row  = [method_name for method_name in dir(row)
---> 86                                  if callable(getattr(row, method_name))]        
     87         print('row:\n', str(row), '\n\ntable row methods:\n', object_methods_row)
     88 

~\Anaconda3\lib\site-packages\pandas\core\generic.py in __getattr__(self, name)
   4374             if self._info_axis._can_hold_identifiers_and_holds_name(name):
   4375                 return self[name]
-> 4376             return object.__getattribute__(self, name)
   4377 
   4378     def __setattr__(self, name, value):

~\Anaconda3\lib\site-packages\pandas\core\generic.py in _constructor_sliced(self)
    222         original, such as DataFrame single columns slicing.
    223         """
--> 224         raise com.AbstractMethodError(self)
    225 
    226     @property

AttributeError: module 'pandas.core.common' has no attribute 'AbstractMethodError'

【问题讨论】:

你能发一个df_jobs的例子吗? 我将数据帧中的行添加到 OP。 我发布了从 df 复制一行的代码以及该行的输出。 groupby) returns tuples of (index, group. So when your code does for i in (df_jobs.groupby...)`,你正在包装每个元组。 【参考方案1】:

好的,你的代码的问题是你如何迭代 group by 的结果。

This answer 展示了如何正确迭代。

这段代码展示了如何完成你的任务:

import pandas as pd

def method1():
    return 0

exampleDf = pd.DataFrame(columns=["name","group","failed"])

exampleDf.loc[0] = ["method1", "failed", 1]
exampleDf.loc[1] = ["method2", "success", 0]
exampleDf.loc[2] = ["method3", "success", 0]
exampleDf.loc[3] = ["method4", "failed", 1]

groupedDf = exampleDf.groupby(['group', 'failed'])

for params, table in groupedDf:
    print("Iterating over the table with the params: " + str(params) )
    print("Table: \n" + str(table))

    for index, row in table.iterrows():
        if(row['name'] in dir()):
            print("The method " + str(row['name']) + " is defined.")

输出:

Iterating over the table with the params: ('failed', 1)
Table: 
      name   group failed
0  method1  failed      1
3  method4  failed      1
The method method1 is defined.
Iterating over the table with the params: ('success', 0)
Table: 
      name    group failed
1  method2  success      0
2  method3  success      0

【讨论】:

我可以运行您的代码,但输出不是我所期望的。我正在寻找为我传递给它的任何对象生成所有方法列表的代码。例如,下面的代码输出一长串我认为是对象 x 的所有方法和属性的列表。我只想要方法(而不是属性): Input: >>> x = wtime_means_by_group.groupby(['group'])['wtime_means'] >>> dir(x) 输出['__bytes__', '__class__', '__delattr__', '__dict__', . . . , 'unique', 'value_counts', 'var'] 我不确定是否得到了你需要的东西,但这不是最终的 if 吗?迭代打印在 dir() 中定义的每个方法 谢谢,我(慢慢地)开始理解您向我展示的有关如何迭代 group_by 对象的内容!我已将您的代码与我原始帖子中的 for 循环合并,并将其及其输出附加到我的问题底部。这段代码成功地显示了我想要的输出——对象方法——但只对参数、表和表索引这样做。对于表行,我得到了相同的 AttributeError。 我猜这与我的迭代方式有关,和以前一样。但我不知道表格行的对象模型。对于我现在解决这个问题以及将来类似的问题,我如何确定一个对象的结构?例如,您向我展示了 groupby obj 在我对其进行迭代时分解为参数和表格。由索引和行组成的表。如果我还不知道对象结构,如何以编程方式查询对象以揭示这一点? 可能有一些更科学的方法,但我只是打印对象并检查它是否是一个简单的结构,在其他情况下我搜索创建对象的库的文档。在这种情况下,我只是做了一些测试,将 print 放入循环中并检查它会输出什么。但正如我所说,这可能不是最好的方法。

以上是关于如何修复“pandas.core.common”没有属性“AbstractMethodError”?的主要内容,如果未能解决你的问题,请参考以下文章

如何修复 SafeAreaLayoutGuide

windows批处理文件.bat双击无法执行,而且默认是记事本打开,右键快捷菜单里也没执行命令,请问如何修复?

hp打印机缺墨用啥软件修复

如何修复 Chrome 中的页面滚动延迟

如何解决”Microsoft Office 无法验证此应用程序的许可证.修复尝试失败或者已被取消.“?

如何使视图修复屏幕IOS 8?