-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
ENH: make itertuples() return namedtuples #11269
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
Comments
sure this would make sense. I would prob name the tuple pull-requests welcome. I believe this should work on all versions (inc 2.6) as |
Just checked Python 2.6.6, and there is no support for the |
oh, why do you need |
In [7]: itertuple = namedtuple("Pandas", ["def", "return"], rename=True)
In [8]: itertuple(1,2)
Out[8]: Pandas(_0=1, _1=2)
In [9]: itertuple = namedtuple("Pandas", ["def", "return"], rename=False)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-9-098cde3289d7> in <module>()
----> 1 itertuple = namedtuple("Pandas", ["def", "return"], rename=False)
/usr/lib/python3.5/collections/__init__.py in namedtuple(typename, field_names, verbose, rename)
394 if _iskeyword(name):
395 raise ValueError('Type names and field names cannot be a '
--> 396 'keyword: %r' % name)
397 seen = set()
398 for name in field_names:
ValueError: Type names and field names cannot be a keyword: 'def' |
ahh I see, ok, you can just do this conditionally and raise in py2.6 if its not supported |
This was also discussed in #7958. I'm not sure how to handle the |
@mjoud as I said above, you can pass the |
ENH: itertuples() returns namedtuples (closes #11269)
I propose that
itertuples()
should returncollections.namedtuple
objects, a drop-in replacement for the standard tuple but with the benefit of having named fields. I have tested the following with Python 3.4 (only slight changes compared to the current implementation).Example
There is no performance overhead. I'm not sure about the compatibility for older versions of Python, though. The
rename
parameter is needed for renaming disallowed field names and duplicate identifiers to standard position-based identifiers, and this feature was added in Python 2.7/3.1.The text was updated successfully, but these errors were encountered: