have error for fill column with conditional mode of another columns

40 views Asked by At

I have data base and i want calculate mode for some row. when I want fill column with conditional mode of other columns and write code

jupyter notebook come back this error Unexpected exception formatting exception. Falling back to standard exception

db['mode_process_time'] = db.groupby(['WID_COIL_HSMSL', 'TKS_COIL_HSMSL'])['method_3_process_time'].agg(lambda x: pd.Series.mode(x)[0])

i have this error

Traceback (most recent call last): File "C:\ProgramData\anaconda3\Lib\site-packages\pandas\core\frame.py", line 12020, in _reindex_for_setitem reindexed_value = value.reindex(index)._values ^^^^^^^^^^^^^^^^^^^^ File "C:\ProgramData\anaconda3\Lib\site-packages\pandas\core\series.py", line 5094, in reindex return super().reindex(**kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\ProgramData\anaconda3\Lib\site-packages\pandas\core\generic.py", line 5289, in reindex return self._reindex_axes( ^^^^^^^^^^^^^^^^^^^ File "C:\ProgramData\anaconda3\Lib\site-packages\pandas\core\generic.py", line 5304, in _reindex_axes new_index, indexer = ax.reindex( ^^^^^^^^^^^ File "C:\ProgramData\anaconda3\Lib\site-packages\pandas\core\indexes\base.py", line 4434, in reindex target = self._wrap_reindex_result(target, indexer, preserve_names) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\ProgramData\anaconda3\Lib\site-packages\pandas\core\indexes\multi.py", line 2556, in _wrap_reindex_result target = MultiIndex.from_tuples(target) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\ProgramData\anaconda3\Lib\site-packages\pandas\core\indexes\multi.py", line 205, in new_meth return meth(self_or_cls, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\ProgramData\anaconda3\Lib\site-packages\pandas\core\indexes\multi.py", line 573, in from_tuples arrays = list(lib.tuples_to_object_array(tuples).T) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "pandas_libs\lib.pyx", line 2984, in pandas._libs.lib.tuples_to_object_array ValueError: Buffer dtype mismatch, expected 'Python object' but got 'long long'

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "C:\ProgramData\anaconda3\Lib\site-packages\IPython\core\interactiveshell.py", line 3505, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "C:\Users\1526\AppData\Local\Temp\ipykernel_7804\3007740805.py", line 8, in db['mode_process_time'] = db.groupby(['WID_COIL_HSMSL', 'TKS_COIL_HSMSL'])['method_3_process_time'].agg(lambda x: pd.Series.mode(x)[0]) ~~^^^^^^^^^^^^^^^^^^^^^ File "C:\ProgramData\anaconda3\Lib\site-packages\pandas\core\frame.py", line 3980, in setitem self._set_item(key, value) File "C:\ProgramData\anaconda3\Lib\site-packages\pandas\core\frame.py", line 4174, in _set_item value = self._sanitize_column(value) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\ProgramData\anaconda3\Lib\site-packages\pandas\core\frame.py", line 4912, in _sanitize_column return _reindex_for_setitem(Series(value), self.index) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\ProgramData\anaconda3\Lib\site-packages\pandas\core\frame.py", line 12027, in _reindex_for_setitem raise TypeError( TypeError: incompatible index of inserted column with frame index

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "C:\ProgramData\anaconda3\Lib\site-packages\IPython\core\interactiveshell.py", line 2102, in showtraceback stb = self.InteractiveTB.structured_traceback( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\ProgramData\anaconda3\Lib\site-packages\IPython\core\ultratb.py", line 1310, in structured_traceback return FormattedTB.structured_traceback( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\ProgramData\anaconda3\Lib\site-packages\IPython\core\ultratb.py", line 1199, in structured_traceback return VerboseTB.structured_traceback( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\ProgramData\anaconda3\Lib\site-packages\IPython\core\ultratb.py", line 1071, in structured_traceback formatted_exceptions += self.format_exception_as_a_whole(etype, evalue, etb, lines_of_context, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\ProgramData\anaconda3\Lib\site-packages\IPython\core\ultratb.py", line 953, in format_exception_as_a_whole self.get_records(etb, number_of_lines_of_context, tb_offset) if etb else [] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\ProgramData\anaconda3\Lib\site-packages\IPython\core\ultratb.py", line 1037, in get_records FIs.append(FrameInfo("Raw frame", filename, lineno, frame, code)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\ProgramData\anaconda3\Lib\site-packages\IPython\core\ultratb.py", line 700, in init ix = inspect.getsourcelines(frame) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\ProgramData\anaconda3\Lib\inspect.py", line 1244, in getsourcelines lines, lnum = findsource(object) ^^^^^^^^^^^^^^^^^^ File "C:\ProgramData\anaconda3\Lib\inspect.py", line 1081, in findsource raise OSError('could not get source code') OSError: could not get source code

0

There are 0 answers