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
Quantity objects should accept pandas Series #11247
Comments
|
I'm talking here about a very specific case, that if Quantities would accept a numpy array, then it should accept a pandas.series(). A pandas series will act like a numpy array when it needs to. If you're worried it won't, then astropy could japply pandas.series.to_numpy() or pandas.series.values and keep going. |
@mhvk should have the final say, but it's worth noting that Generally speaking I agree with @pllim that astropy is based on numpy. There are many many parts of astropy that simply won't work with Pandas Series because ndarray is entirely different (even if they look vaguely similar). After fixing this Quantity issue you will undoubtedly run into other things that don't work, so my inclination is to not even start down that slippery path. |
I'm actually a bit surprised it does not work, as inside
So, the problem seems to be that for multiplication, Anyway, bottom line is that the exact request is not something we can solve in astropy: pandas just takes care of the multiplcation and we get no chance to take control. But there is a good work around for converting a Interestingly, what works as well (somewhat coincidentally, just because it happens not to be defined by pandas) is the in-place shift operator:
|
I am intending to contribute to this issue. I have analyzed it to some extent. I understand that the problem is with multiplication However, I could not understand the workaround (for converting a Series to a Quantity: one can just use the class initializer) suggested by @mhvk in the above comment entirely. @mhvk could you please explain a workaround to me? |
@apurvapatkar - I don't think there is anything to do except perhaps to document that |
Thanks for the reply. I would be happy to assist in documenting this issue and the approach. |
An update on running the examples above again: >>> import pandas as pd
>>> import astropy.units as u
>>> a = pd.Series([1, 2., 3.])
>>> a * u.m
0 1.0
1 2.0
2 3.0
dtype: float64 runs now. It doesn't give you the right result but it doesn't error out. tested with pandas 2.1.4 |
@MridulS - thanks for the update! I reproduced your result. Also, this time the unit is truly gone:
Since this remains out of our control, the only thing we can do is document it... |
This behaves like |
Yes, makes sense! |
Where Astropy expects a Quantity object, it rejects as input a pandas.Series * astropy unit. I believe this is unnecessarily strict, since a numpy array * astropy unit would be permitted. Here is an example of astropy rejecting what I believe should be a valid input of pandas.Series() * u.unit:
The kludgy workaround is to put .values after each Series:
s1723_spec = specutils.Spectrum1D(spectral_axis = df_s1723['wave'].values * u.AA, flux=df_s1723['fnu'].values * u.Unit('Jansky'))
However, the workaround is kludgy, and I think unfair to pandas users. A Pandas.Series() will act like a numpy array when it needs to. Here, Astropy is rejecting it before it can.
Please see astropy/specutils#740 , because I discovered the issue in the specutils.Spectrum1D class, although I believe the problem actually lies with Quantities.
The text was updated successfully, but these errors were encountered: