Skip to content

error during line-wise iteration over DataFrame #840

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
bgbg opened this issue Feb 29, 2012 · 3 comments
Closed

error during line-wise iteration over DataFrame #840

bgbg opened this issue Feb 29, 2012 · 3 comments

Comments

@bgbg
Copy link

bgbg commented Feb 29, 2012

In the code below I create a DataFame and iterate over its lines using ix

In [37]: a = pandas.DataFrame(rand(4, 2), columns = list('ab'))

In [38]: print a
          a         b
0  0.372630  0.680347
1  0.557162  0.815903
2  0.058282  0.371265
3  0.772931  0.925008

In [39]: for l in a.ix:
   ....:     print l
   ....:
a    0.372630
b    0.680347
a    0.557162
b    0.815903
Name: 1
a    0.058282
b    0.371265
Name: 2
a    0.772931
b    0.925008
Name: 3
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Q:\Groups\Bioinformatics\IgG assay optimization 2011\script development\scriptDevelopment\src\<ipython-input-39-50ae06db9052> in <module>()
----> 1 for l in a.ix:
      2     print l
      3

c:\python27\lib\site-packages\pandas\core\indexing.pyc in __getitem__(self, key)
     30             return self._getitem_tuple(key)
     31         else:
---> 32             return self._getitem_axis(key, axis=0)
     33
     34     def _get_label(self, label, axis=0):

c:\python27\lib\site-packages\pandas\core\indexing.pyc in _getitem_axis(self, key, axis)
    182                     idx = labels[key]
    183
--> 184             return self._get_label(idx, axis=0)
    185         else:
    186             labels = self.obj._get_axis(axis)

c:\python27\lib\site-packages\pandas\core\indexing.pyc in _get_label(self, label, axis)
     36             return self.obj.xs(label, axis=axis, copy=False)
     37         except Exception:
---> 38             return self.obj.xs(label, axis=axis, copy=True)
     39
     40     def _slice(self, obj, axis=0):

c:\python27\lib\site-packages\pandas\core\frame.pyc in xs(self, key, axis, level, copy)
   1592             loc, new_index = self.index.get_loc_level(key)
   1593         else:
-> 1594             loc = self.index.get_loc(key)
   1595
   1596         if np.isscalar(loc):

c:\python27\lib\site-packages\pandas\core\index.pyc in get_loc(self, key)
    493         loc : int
    494         """
--> 495         return self._engine.get_loc(key)
    496
    497     def get_value(self, series, key):

c:\python27\lib\site-packages\pandas\_engines.pyd in pandas._engines.DictIndexEngine.get_loc (pandas\src\engines.c:2498)()

c:\python27\lib\site-packages\pandas\_engines.pyd in pandas._engines.DictIndexEngine.get_loc (pandas\src\engines.c:2460)()

KeyError: 4
@lodagro
Copy link
Contributor

lodagro commented Feb 29, 2012

DataFrame.ix.__getitem__() needs to return IndexError iso KeyError to make iteration stop at the correct point when one does for l in a.ix: ..., which is not the case here.

As workaround can you have a look at DataFrame.iterrows(), DataFrame.iteritems(), DataFrame.iterkv()

@bgbg
Copy link
Author

bgbg commented Feb 29, 2012

thank you for the workarounds

@wesm
Copy link
Member

wesm commented Feb 29, 2012

That is not an API I wish to support. for row in df.ix will raise an exception in 0.7.1

@wesm wesm closed this as completed Feb 29, 2012
yarikoptic added a commit to neurodebian/pandas that referenced this issue Mar 2, 2012
* commit 'v0.7.1-1-ga2e86c2': (90 commits)
  BUG: Fix Series, DataFrame plot() for non numerical/datetime (Multi)Index (closes pandas-dev#741).
  RLS: Version 0.7.1
  DOC: release notes, what's new, change dev version to 0.7.1
  BUG: close pandas-dev#839, another case where nan may be assigned to int series
  ENH: raise NotImplementedError if user tries to iterate over .ix, GH pandas-dev#840
  BUG: fixed null-check per pandas-dev#839
  BUG: close pandas-dev#839, exception on assigning NA to bool or int64 series
  TST: more test coverage for release target
  TST: added core coverage
  TST: fix lingering line of code from pandas-dev#838
  DOC: added yet a bit more to release notes
  TST: unit test for pandas-dev#838
  DOC: added more release notes
  BUG: raise more helpful error msg for pandas-dev#835
  TST: added skip excel test for no xlrd installed
  BUG: close pandas-dev#835, add option to suppress index inference
  BUG: close pandas-dev#837, excelfile throws an exception for two-line file
  ENH: fill_value arg in DataFrame.reindex/reindex_axis, add fillna to sparse objects, GH pandas-dev#784
  ENH: add fill_value argument to Series.reindex, DataFrame next, pandas-dev#784
  ENH: concat Series with axis=1 for completeness, GH pandas-dev#787
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants