pedroprates / b3bovespa

Repository to get the name and code of all companies listed on Bovespa
MIT License
9 stars 2 forks source link

ValueError: Wrong number of items passed 4, placement implies 1 #2

Open rodriguesra opened 3 years ago

rodriguesra commented 3 years ago

Pedro, tudo bem?

Ao tentar executar o método get_companies_data() eu recebo o seguinte erro:

Poderia me ajudar com isso por favor?

Grato

Getting Initial Data: | | 0/0 Getting Codes: | | 1/0 companies

KeyError Traceback (most recent call last) ~/bovespa/venv/lib/python3.10/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance) 3360 try: -> 3361 return self._engine.get_loc(casted_key) 3362 except KeyError as err:

~/bovespa/venv/lib/python3.10/site-packages/pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

~/bovespa/venv/lib/python3.10/site-packages/pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'Code'

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

KeyError Traceback (most recent call last) ~/bovespa/venv/lib/python3.10/site-packages/pandas/core/frame.py in _set_item_mgr(self, key, value) 3750 try: -> 3751 loc = self._info_axis.get_loc(key) 3752 except KeyError:

~/bovespa/venv/lib/python3.10/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance) 3362 except KeyError as err: -> 3363 raise KeyError(key) from err 3364

KeyError: 'Code'

During handling of the above exception, another exception occurred:

ValueError Traceback (most recent call last) /var/folders/b0/2q5bkb_15kgb7y49d8w_09nh0000gn/T/ipykernel_52424/2480956231.py in ----> 1 b3.get_companies_data()

~/bovespa/venv/lib/python3.10/site-packages/b3bovespa/scrapper.py in get_companies_data(self) 68 df = self._get_companies_link() 69 tqdm.pandas(desc='Getting Codes', bar_format='{l_bar} {bar}| {n_fmt}/{totalfmt} companies') ---> 70 df['Code'], , , = df.progress_apply(self._get_company_codes, axis=1) 71 self._df = df 72

~/bovespa/venv/lib/python3.10/site-packages/pandas/core/frame.py in setitem(self, key, value) 3600 self._setitem_array(key, value) 3601 elif isinstance(value, DataFrame): -> 3602 self._set_item_frame_value(key, value) 3603 elif ( 3604 is_list_like(value)

~/bovespa/venv/lib/python3.10/site-packages/pandas/core/frame.py in _set_item_frame_value(self, key, value) 3740 # now align rows 3741 arraylike = _reindex_for_setitem(value, self.index) -> 3742 self._set_item_mgr(key, arraylike) 3743 3744 def _iset_item_mgr(self, loc: int | slice | np.ndarray, value) -> None:

~/bovespa/venv/lib/python3.10/site-packages/pandas/core/frame.py in _set_item_mgr(self, key, value) 3752 except KeyError: 3753 # This item wasn't present, just insert at end -> 3754 self._mgr.insert(len(self._info_axis), key, value) 3755 else: 3756 self._iset_item_mgr(loc, value)

~/bovespa/venv/lib/python3.10/site-packages/pandas/core/internals/managers.py in insert(self, loc, item, value) 1160 value = ensure_block_shape(value, ndim=self.ndim) 1161 -> 1162 block = new_block(values=value, ndim=self.ndim, placement=slice(loc, loc + 1)) 1163 1164 for blkno, count in _fast_count_smallints(self.blknos[loc:]):

~/bovespa/venv/lib/python3.10/site-packages/pandas/core/internals/blocks.py in newblock(values, placement, ndim, klass) 1935 1936 values, = extract_pandas_array(values, None, ndim) -> 1937 check_ndim(values, placement, ndim) 1938 1939 if klass is None:

~/bovespa/venv/lib/python3.10/site-packages/pandas/core/internals/blocks.py in check_ndim(values, placement, ndim) 1977 ) 1978 if len(placement) != len(values): -> 1979 raise ValueError( 1980 f"Wrong number of items passed {len(values)}, " 1981 f"placement implies {len(placement)}"

ValueError: Wrong number of items passed 4, placement implies 1

rodriguesra commented 3 years ago

Somente um comentário, a linha que me parece com algum problema é essa: df['Code'], , , _ = df.progress_apply(self._get_company_codes, axis=1)

No seu código original df['Code'] = df.progress_apply(self._get_company_codes, axis=1) também recebo o mesmo erro.

Eu tentei unpack o método get_company_codes, mas sem sucesso...