Skip to content

DataFrame does not work well with quantities #1071

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
tkf opened this issue Apr 17, 2012 · 3 comments
Closed

DataFrame does not work well with quantities #1071

tkf opened this issue Apr 17, 2012 · 3 comments

Comments

@tkf
Copy link
Contributor

tkf commented Apr 17, 2012

from pandas import DataFrame
import quantities as pq
df = DataFrame([[pq.Quantity(i, pq.s)] for i in range(3)])
df[1:] - df[:-1]

raises: ValueError: Unable to convert between units of "dimensionless" and "s"

Note that the following works w/o error because pandas "strips" dimension:

df = DataFrame(pq.Quantity(np.array([0,1,2]).T, pq.s))
df[1:] - df[:-1]
@changhiskhan
Copy link
Contributor

normally, you'd be able to do something like:

nas = pq.Quantity(np.nan, pq.s)
df[1:].sub(df[:-1], fill_value=nas)

However, because pq.Quantity is an ndarray subclass there's some weird behavior. I'm not familiar with quantities internals, but here's what I saw just poking around:

In [54]: arr = np.array([np.nan, pq.Quantity(1, pq.s), pq.Quantity(2, pq.s)], dtype=object)

In [55]: isnull(arr)
Out[55]: array([ True, False, False], dtype=bool)

In [56]: arr[isnull(arr)] = pq.Quantity(np.nan, pq.s)

In [57]: arr
Out[57]: array([nan, 1 s, 2 s], dtype=object)

Essentially this makes the NaN filling code in pandas have not effect.

@tkf
Copy link
Contributor Author

tkf commented Jun 25, 2012

I noticed you can strip dimension by numpy.asarray. So I am using this function before giving any Quantity arrays to pandas.

@jreback
Copy link
Contributor

jreback commented Dec 5, 2015

closing in favor of #10349

@jreback jreback closed this as completed Dec 5, 2015
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