On Thu, Apr 7, 2022 at 10:51 AM Stephen J. Turnbull <
[email protected]> wrote:
> NOTE: The big problem I see with this is that I don't see any
> practical way to use syntax for non-numeric types like ndarray.
> The problem is that in, say, economics we use a lot of m x 2
> arrays where the two columns have different units (ie, a quantity
> unit and a $/quantity unit). The sensible way to express this
> isn't some kind of appended syntax as with numbers, but rather a
> sequence of units corresponding to the sequence of columns.
>
Here's a few minutes of work by me:
>>> df = pd.DataFrame([['Apples', 11, 2], ['Pears', 12, 3]],
... columns=[Units("Item", "Kind-of-Fruit"), Units("Per-Bushel",
"USD/bushel"), Units("Number")])
...
...
>>> df
Item Per-Bushel Number
0 Apples 11 2
1 Pears 12 3
>>> df.columns[1].units
'USD/bushel'
I did this by defining:
>>> class Units(str):
... def __new__(self, s, units="Dimensionless"):
... return str.__new__(self, s)
... def __init__(self, s, units="Dimensionless"):
... float.__init__(s)
... self.units = units
If this were actually needed, I'm sure better interfaces could be created.
But this very simple thing is a possible way to retain units per-column.
--
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.
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/5GC5PZJJDHZRMIG7X46GDUCQOQSM5I5H/
Code of Conduct: http://python.org/psf/codeofconduct/