Skip to content

BUG: small error in pandas/io/pytables.py #34274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
2 of 3 tasks
SilasK opened this issue May 20, 2020 · 6 comments · Fixed by #38919
Closed
2 of 3 tasks

BUG: small error in pandas/io/pytables.py #34274

SilasK opened this issue May 20, 2020 · 6 comments · Fixed by #38919
Labels
Bug IO HDF5 read_hdf, HDFStore
Milestone

Comments

@SilasK
Copy link

SilasK commented May 20, 2020

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

# Your code here


df.to_hdf('file.h5','name', format='table')

~/miniconda3/lib/python3.6/site-packages/pandas/io/pytables.py in _maybe_convert_for_string_atom(name, block, existing_col, min_itemsize, nan_rep, encoding, errors)
   4798         # we cannot serialize this data, so report an exception on a column
   4799         # by column basis
-> 4800         for i in range(len(block.shape[0])):
   4801 
   4802             col = block.iget(i)

TypeError: object of type 'int' has no len()

Problem description

I don't know why exactly pandas cannot serialize my data frame with a multi-index.
But the above error prevents the real error message to be printed.

Expected Output

Error message.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.6.7.final.0
python-bits : 64
OS : Darwin
OS-release : 16.7.0
machine : x86_64
processor : i386
byteorder : little
LC_ALL : en_US.UTF-8
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.0.3
numpy : 1.18.1
pytz : 2020.1
dateutil : 2.8.1
pip : 20.1.1
setuptools : 46.1.3.post20200325
Cython : 0.29.18
pytest : 5.4.1
hypothesis : 5.15.0
sphinx : 3.0.3
blosc : None
feather : None
xlsxwriter : 1.2.8
lxml.etree : 3.8.0
html5lib : 1.0.1
pymysql : 0.9.3
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.14.0
pandas_datareader: None
bs4 : 4.9.1
bottleneck : 1.3.2
fastparquet : None
gcsfs : None
lxml.etree : 3.8.0
matplotlib : 3.2.1
numexpr : 2.7.1
odfpy : None
openpyxl : 3.0.3
pandas_gbq : None
pyarrow : None
pytables : None
pytest : 5.4.1
pyxlsb : None
s3fs : None
scipy : 1.4.1
sqlalchemy : 1.3.17
tables : 3.6.1
tabulate : None
xarray : None
xlrd : 1.2.0
xlwt : 1.3.0
xlsxwriter : 1.2.8
numba : 0.48.0

@SilasK SilasK added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels May 20, 2020
@SilasK
Copy link
Author

SilasK commented May 20, 2020

The data frame could net be serialized because I had ints in a string column.
But the reported bug should be independent of this.

@WillAyd
Copy link
Member

WillAyd commented May 20, 2020

Makes sense. Want to push a PR?

@WillAyd WillAyd removed the Needs Triage Issue that has not been reviewed by a pandas team member label May 20, 2020
@mroeschke mroeschke added the IO HDF5 read_hdf, HDFStore label May 21, 2020
@rohgpt
Copy link

rohgpt commented May 22, 2020

u need to convert int data to string.

SilasK added a commit to SilasK/pandas that referenced this issue May 23, 2020
@jreback jreback added this to the 1.1 milestone May 25, 2020
@TomAugspurger TomAugspurger removed this from the 1.1 milestone Jul 7, 2020
@Rmsharks4
Copy link

Facing this same issue using this piece of code:

import wave

for folder in tqdm(os.listdir(datasets[2])):
    for file in os.listdir(datasets[2]+folder+'/audio/'):
        wav_r = wave.open(datasets[2]+folder+'/audio/'+file, 'rb')
        data = wav_r.readframes(wav_r.getnframes())
        audios_df = pd.DataFrame([{**{'Filename': file, 'Data': data}, **dict(wav_r.getparams()._asdict())}])
        audios_df.to_hdf('amicorpus_audios.h5', key='audios_df', append=True)

Exception:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-e660c3ea770b> in <module>
      7         audios_df = pd.DataFrame([{**{'Filename': file, 'Data': data}, **dict(wav_r.getparams()._asdict())}])
      8         print(audios_df)
----> 9         audios_df.to_hdf('amicorpus_audios.h5', key='audios_df', append=True)

~\anaconda3\lib\site-packages\pandas\core\generic.py in to_hdf(self, path_or_buf, key, mode, complevel, complib, append, format, index, min_itemsize, nan_rep, dropna, data_columns, errors, encoding)
   2503             data_columns=data_columns,
   2504             errors=errors,
-> 2505             encoding=encoding,
   2506         )
   2507 

~\anaconda3\lib\site-packages\pandas\io\pytables.py in to_hdf(path_or_buf, key, value, mode, complevel, complib, append, format, index, min_itemsize, nan_rep, dropna, data_columns, errors, encoding)
    280             path_or_buf, mode=mode, complevel=complevel, complib=complib
    281         ) as store:
--> 282             f(store)
    283     else:
    284         f(path_or_buf)

~\anaconda3\lib\site-packages\pandas\io\pytables.py in <lambda>(store)
    259             data_columns=data_columns,
    260             errors=errors,
--> 261             encoding=encoding,
    262         )
    263     else:

~\anaconda3\lib\site-packages\pandas\io\pytables.py in append(self, key, value, format, axes, index, append, complib, complevel, columns, min_itemsize, nan_rep, chunksize, expectedrows, dropna, data_columns, encoding, errors)
   1180             data_columns=data_columns,
   1181             encoding=encoding,
-> 1182             errors=errors,
   1183         )
   1184 

~\anaconda3\lib\site-packages\pandas\io\pytables.py in _write_to_group(self, key, value, format, axes, index, append, complib, complevel, fletcher32, min_itemsize, chunksize, expectedrows, dropna, nan_rep, data_columns, encoding, errors)
   1707             dropna=dropna,
   1708             nan_rep=nan_rep,
-> 1709             data_columns=data_columns,
   1710         )
   1711 

~\anaconda3\lib\site-packages\pandas\io\pytables.py in write(self, obj, axes, append, complib, complevel, fletcher32, min_itemsize, chunksize, expectedrows, dropna, nan_rep, data_columns)
   4141             min_itemsize=min_itemsize,
   4142             nan_rep=nan_rep,
-> 4143             data_columns=data_columns,
   4144         )
   4145 

~\anaconda3\lib\site-packages\pandas\io\pytables.py in _create_axes(self, axes, obj, validate, nan_rep, data_columns, min_itemsize)
   3811                 nan_rep=nan_rep,
   3812                 encoding=self.encoding,
-> 3813                 errors=self.errors,
   3814             )
   3815             adj_name = _maybe_adjust_name(new_name, self.version)

~\anaconda3\lib\site-packages\pandas\io\pytables.py in _maybe_convert_for_string_atom(name, block, existing_col, min_itemsize, nan_rep, encoding, errors)
   4798         # we cannot serialize this data, so report an exception on a column
   4799         # by column basis
-> 4800         for i in range(len(block.shape[0])):
   4801 
   4802             col = block.iget(i)

TypeError: object of type 'int' has no len()

Any known workarounds?

@shanto268
Copy link

Ran into same problem.... any updates? 😢

@SilasK
Copy link
Author

SilasK commented Dec 2, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug IO HDF5 read_hdf, HDFStore
Projects
None yet
8 participants