Join GitHub today
GitHub is home to over 20 million developers working together to host and review code, manage projects, and build software together.
ENH: make itertuples() return namedtuples #11269
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 |
jreback
added Enhancement Reshaping Difficulty Novice Compat Effort Low
labels
Oct 9, 2015
jreback
added this to the
Next Major Release
milestone
Oct 9, 2015
mjoud
commented
Oct 9, 2015
|
Just checked Python 2.6.6, and there is no support for the |
|
oh, why do you need |
mjoud
commented
Oct 9, 2015
|
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 |
mjoud
commented
Oct 14, 2015
|
This was also discussed in #7958. I'm not sure how to handle the |
|
@mjoud as I said above, you can pass the |
mjoud
referenced
this issue
Oct 16, 2015
Merged
ENH: itertuples() returns namedtuples (closes #11269) #11325
jreback
modified the milestone: 0.17.1, Next Major Release
Oct 20, 2015
jreback
closed this
in #11325
Oct 28, 2015
jreback
added a commit
that referenced
this issue
Oct 28, 2015
|
|
jreback |
8a46de4
|
mjoud commentedOct 9, 2015
I propose that
itertuples()should returncollections.namedtupleobjects, 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
renameparameter 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.