Skip to content
This repository was archived by the owner on Oct 29, 2024. It is now read-only.

Can not work, issue show KeyError: '[0] not in index', need your great help .thanks. #497

Closed
leonyan1314 opened this issue Sep 1, 2017 · 6 comments

Comments

@leonyan1314
Copy link

Create pandas DataFrame
Create database: demo
Write DataFrame
Traceback (most recent call last):
File "/Users/LeonYan/MyPython/QI/DF2influx.py", line 50, in
main(host=args.host, port=args.port)
File "/Users/LeonYan/MyPython/QI/DF2influx.py", line 25, in main
client.write_points(df, 'demo', protocol=protocol)
File "/Users/LeonYan/Library/Python/2.7/lib/python/site-packages/influxdb/_dataframe_client.py", line 125, in write_points
field_columns=field_columns)
File "/Users/LeonYan/Library/Python/2.7/lib/python/site-packages/influxdb/_dataframe_client.py", line 220, in _convert_dataframe_to_json
dataframe[field_columns].to_dict('record'))
File "/Users/LeonYan/Library/Python/2.7/lib/python/site-packages/pandas/core/frame.py", line 1958, in getitem
return self._getitem_array(key)
File "/Users/LeonYan/Library/Python/2.7/lib/python/site-packages/pandas/core/frame.py", line 2002, in _getitem_array
indexer = self.loc._convert_to_indexer(key, axis=1)
File "/Users/LeonYan/Library/Python/2.7/lib/python/site-packages/pandas/core/indexing.py", line 1231, in _convert_to_indexer
raise KeyError('%s not in index' % objarr[mask])
KeyError: '[0] not in index'

@leonyan1314
Copy link
Author

MAC OS 10.12.1,python 2.7

@eric-guanwy
Copy link

I don't know whether you have got it resolved by now, and I just encountered the same keyerror issue today, for my situation it fixed by specifying the attribute "columns" explicitly with a string type '0' when create a DataFrame. As the code below:
df = pd.DataFrame(data=list(range(30)),index=pd.date_range(start='2014-11-16',periods=30, freq='H'),columns=['0'])

@kiranmantri
Copy link

kiranmantri commented Oct 22, 2017

In fact, you can assign any string-based name to your columns.
If `columns=['something'] is suppressed, pandas assigns numerical id to them.

works:
df = pd.DataFrame(data=list(range(30)),index=pd.date_range(start='2014-11-16',periods=30, freq='H'),columns=['0'])

doesn't work:
df = pd.DataFrame(data=list(range(30)),index=pd.date_range(start='2014-11-16',periods=30, freq='H'),columns=[0])

works:
df = pd.DataFrame(data=list(range(30)),index=pd.date_range(start='2014-11-16',periods=30, freq='H'),columns=['your_column'])

doesn't work:
df = pd.DataFrame(data=list(range(30)),index=pd.date_range(start='2014-11-16',periods=30, freq='H'),columns=[99])

@xginn8
Copy link
Collaborator

xginn8 commented Dec 1, 2017

closing this since there's no issue

@xginn8 xginn8 closed this as completed Dec 1, 2017
SatoshiNewton added a commit to SatoshiNewton/influxdb-python that referenced this issue Jan 9, 2018
@SatoshiNewton
Copy link

SatoshiNewton commented Jan 9, 2018

Actually, i fixed it.
And i think it's still need to take, because the module need more compatibility to pandas DataFrame.
I would have pull request after i take more testing about pandas and Mac OS X.
However, i think need more information about dose that suitable.
I fixed it by make the pandas DataFrame client convert the columns to string type for avoid potential key error.
I think the reason, make this problem be occurred, is pandas have automatically convert the key, when it's received the DataDrame.
If this assumption is wrong, inform me please.
On the other hand, inform me too, if the assumption is correct, but just for Mac OS X.
I would try to add some code to automatically identify whether the system is OS X.

@kiranmantri
Copy link

Hi @SatoshiNewton ,
Cool ! that's is the approach I applied on my end to solve the bug... and I believe that is right way to solved it (or at least to make influxdb-client more robust when dealing with Pandas dataframes).

xginn8 pushed a commit that referenced this issue Feb 11, 2018
Current example code raises KeyError: '[0] not in index' and
can be fixed by adding columns=['0'] to the DataFrame creation.
Fixed issue #497
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants