[Python-ideas] Proposal: Tuple of str with w'list of words'

David Mertz mertz at gnosis.cx
Sat Nov 12 14:23:59 EST 2016


>
> > mydf = df[ ['field1', 'field2', 'field3' ] ]
> > mydf=df[ 'field1 field2 field3'.split() ]
>
> I consider the need for that to indicate a possibly poor design of
> pandas. Unless there is a good reason not to, I believe that any
> function that requires a list of strings should also accept a single
> space-delimited string instead.
> Of course, it may be that pandas has a good reason for not supporting
> that. But in general, we don't change the language to make up for
> deficiencies in third-party library functionality.
>

Yes... Pandas has a very good reason.  And the general principle is wrong
too.  A list of strings can contain strings with spaces in them.  It's only
in rare cases like collections.namedtuple where the whole point of strings
is to be valid Python identifiers that you can rule out spaces being inside
them.  E.g.:

>>> df = pd.DataFrame({"Quant":[1,2,3],
                       "Approx Price":[4,5,6],
                       "Weight":[7,8,9]})
>>> df[['Quant','Approx Price']]
   Quant  Approx Price
0      1             4
1      2             5
2      3             6
>>> df['Quant Approx Price'.split()]]
...
KeyError: "['Approx' 'Price'] not in index"


The hypothetical w-string would have the same ambiguity with w'Quant Approx
Price'.

Even with namedtuple, you sometimes have to massage strings to get rid of
spaces (and other stray punctuation), e.g.:

>>> from collections import namedtuple
>>> Item = namedtuple('Item', 'Quant Approx_Price Weight')
>>> Item(4,5,6)
Item(Quant=4, Approx_Price=5, Weight=6)


I use namedtuple fairly often dynamically, for example to read a novel CSV
file or query a novel SQL table.  When I do so, I need to check the
"columns" for being valid identifiers, and then massage them to make them
so.

-- 
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons.  Intellectual property is
to the 21st century what the slave trade was to the 16th.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20161112/965b4795/attachment.html>


More information about the Python-ideas mailing list