问题描述
我想看看可用于pandas对象的方法.当我运行这段代码时,出现了AttributeError错误.我已经搜索过但未找到此错误或如何修复的示例.
I wanted to see the methods available for a pandas object. When I ran this code I got an AttributeError error. I've searched but not found an example of this error or how to fix it.
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
和示例输出中复制一行的代码:
Edit
Here's code to reproduce a row from dataframe df_jobs
and sample output:
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
Windows 10
I uninstalled/reinstalled Anaconda this week.
pandas 0.23.4 py37h830ac7b_0
服务器信息:
您正在使用Jupyter笔记本.
Server Information:
You are using Jupyter notebook.
笔记本服务器的版本为:5.7.4-f6790e4
服务器正在此版本的Python上运行:
Python 3.7.1(默认,2018年12月10日,22:54:23)[MSC v.1915 64位(AMD64)]
The version of the notebook server is: 5.7.4-f6790e4
The server is running on this version of Python:
Python 3.7.1 (default, Dec 10 2018, 22:54:23) [MSC v.1915 64 bit (AMD64)]
当前内核信息:
Python 3.7.1(默认,2018年12月10日,22:54:23)[MSC v.1915 64位(AMD64)]
IPython 7.2.0-增强的交互式Python类型 '?'寻求帮助.
Current Kernel Information:
Python 3.7.1 (default, Dec 10 2018, 22:54:23) [MSC v.1915 64 bit (AMD64)]
IPython 7.2.0 -- An enhanced Interactive Python. Type '?' for help.
编辑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'
推荐答案
好的,您的代码的问题是您如何迭代分组依据的结果.
Okay the problem of your code is how u are iterating the result of group by.
此答案显示了如何正确进行迭代.
This answer shows how to iterate properly.
此代码显示了如何执行任务:
This code shows how to do your task:
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
这篇关于如何修复"pandas.core.common"没有属性"AbstractMethodError"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!