使用Astropy转换Pandas柱

发布于 2025-02-03 08:08:01 字数 2239 浏览 4 评论 0原文

有一个pandas数据框

_RAJ2000,_DEJ2000,RA_ICRS,DE_ICRS,mode,q_mode,class,SDSS12,m_SDSS12,ObsDate,Q,umag,e_umag,gmag,e_gmag,rmag,e_rmag,imag,e_imag,zmag,e_zmag,zsp,zph,e_zph,<zph>
02 59 43.85208,+00 14 50.0532,44.932717,0.247237,1,+,6,J025943.85+001450.0,*,2004.7267,3,25.252,0.709,24.427,0.377,22.923,0.186,22.844,0.255,22.173,0.505,,,,
02 59 43.18896,+00 14 49.9776,44.929954,0.247216,1,+,3,J025943.18+001449.9,,2004.7267,3,25.77,0.713,23.41,0.267,22.467,0.183,21.612,0.132,20.905,0.301,,0.5976,0.1462,0.5771
02 59 45.92184,+00 15 09.6372,44.941341,0.252677,1,+,3,J025945.92+001509.6,,2004.7267,3,23.707,1.067,22.886,0.219,21.501,0.105,21.026,0.104,20.984,0.423,,0.423,0.0924,0.4151
02 59 44.95968,+00 15 11.7252,44.937332,0.253257,1,+,3,J025944.95+001511.7,,2004.7267,3,22.729,0.546,24.475,0.791,21.847,0.152,21.244,0.137,21.931,0.96,,0.4073,0.1245,0.4036
02 59 42.50736,+00 15 01.6416,44.927114,0.250456,1,,3,J025942.50+001501.6,*,2004.7267,3,22.529,0.467,22.705,0.198,22.117,0.203,21.279,0.139,20.905,0.418,,0.7674,0.0744,0.7239
02 59 42.88320,+00 15 01.8144,44.92868,0.250504,1,,3,J025942.88+001501.8,*,2004.7267,3,24.755,0.864,23.219,0.168,22.533,0.14,22.161,0.152,22.508,0.602,,0.3329,0.1434,0.2613
02 59 41.84880,+00 15 05.7924,44.92437,0.251609,1,+,3,J025941.84+001505.7,*,2004.7267,3,22.849,0.377,23.707,0.279,22.378,0.141,21.274,0.082,20.76,0.221,,0.6223,0.0731,0.6115
02 59 46.54512,+00 15 10.8972,44.943938,0.253027,1,+,3,J025946.54+001510.8,*,2004.7267,3,22.34,0.372,21.589,0.074,20.139,0.033,19.577,0.031,19.066,0.081,,0.4006,0.0569,0.4019
02 59 45.76656,+00 15 34.6932,44.940694,0.259637,1,,3,J025945.76+001534.6,,2004.7267,3,25.362,0.977,22.829,0.169,22.873,0.266,23.286,0.543,21.734,0.601,,0.5175,0.1433,0.3604

我 在raj2000dej2000列中。我应该能够使用Astropy进行操作,但是如果我尝试第一个:

from astropy import units as u
from astropy.coordinates import SkyCoord
c = SkyCoord('23 58 36.30073', 'icrs', unit=(u.hourangle))

我得到

ValueError: Invalid character at col 0 in angle 'icrs'

我的问题是:

  • 为什么我输入的字符串会导致错误,以及
  • 如何应用<<代码> SkyCoord PANDAS列?

I have a pandas dataframe that includes the columns _RA2000 and _DEJ2000, thus:

_RAJ2000,_DEJ2000,RA_ICRS,DE_ICRS,mode,q_mode,class,SDSS12,m_SDSS12,ObsDate,Q,umag,e_umag,gmag,e_gmag,rmag,e_rmag,imag,e_imag,zmag,e_zmag,zsp,zph,e_zph,<zph>
02 59 43.85208,+00 14 50.0532,44.932717,0.247237,1,+,6,J025943.85+001450.0,*,2004.7267,3,25.252,0.709,24.427,0.377,22.923,0.186,22.844,0.255,22.173,0.505,,,,
02 59 43.18896,+00 14 49.9776,44.929954,0.247216,1,+,3,J025943.18+001449.9,,2004.7267,3,25.77,0.713,23.41,0.267,22.467,0.183,21.612,0.132,20.905,0.301,,0.5976,0.1462,0.5771
02 59 45.92184,+00 15 09.6372,44.941341,0.252677,1,+,3,J025945.92+001509.6,,2004.7267,3,23.707,1.067,22.886,0.219,21.501,0.105,21.026,0.104,20.984,0.423,,0.423,0.0924,0.4151
02 59 44.95968,+00 15 11.7252,44.937332,0.253257,1,+,3,J025944.95+001511.7,,2004.7267,3,22.729,0.546,24.475,0.791,21.847,0.152,21.244,0.137,21.931,0.96,,0.4073,0.1245,0.4036
02 59 42.50736,+00 15 01.6416,44.927114,0.250456,1,,3,J025942.50+001501.6,*,2004.7267,3,22.529,0.467,22.705,0.198,22.117,0.203,21.279,0.139,20.905,0.418,,0.7674,0.0744,0.7239
02 59 42.88320,+00 15 01.8144,44.92868,0.250504,1,,3,J025942.88+001501.8,*,2004.7267,3,24.755,0.864,23.219,0.168,22.533,0.14,22.161,0.152,22.508,0.602,,0.3329,0.1434,0.2613
02 59 41.84880,+00 15 05.7924,44.92437,0.251609,1,+,3,J025941.84+001505.7,*,2004.7267,3,22.849,0.377,23.707,0.279,22.378,0.141,21.274,0.082,20.76,0.221,,0.6223,0.0731,0.6115
02 59 46.54512,+00 15 10.8972,44.943938,0.253027,1,+,3,J025946.54+001510.8,*,2004.7267,3,22.34,0.372,21.589,0.074,20.139,0.033,19.577,0.031,19.066,0.081,,0.4006,0.0569,0.4019
02 59 45.76656,+00 15 34.6932,44.940694,0.259637,1,,3,J025945.76+001534.6,,2004.7267,3,25.362,0.977,22.829,0.169,22.873,0.266,23.286,0.543,21.734,0.601,,0.5175,0.1433,0.3604

and I'd like to convert them to degrees, as they appear in the RAJ2000 and DEJ2000 columns. I should be able to do it with astropy, but if I try the first one:

from astropy import units as u
from astropy.coordinates import SkyCoord
c = SkyCoord('23 58 36.30073', 'icrs', unit=(u.hourangle))

I get

ValueError: Invalid character at col 0 in angle 'icrs'

So my questions are:

  • why is the string I've inputted causing the error, and
  • how can I apply SkyCoord to the pandas columns?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遥远的绿洲 2025-02-10 08:08:02

您需要同时进行两个坐标的转换,并作为关键字参数提供框架:

import pandas as pd
from astropy import units as u
from astropy.coordinates import SkyCoord

df = pd.DataFrame({'_RAJ2000': ['23 58 36.30073 ',  '23 58 19.66200 ',  '23 58 17.43747 ',  '23 58 10.18153 ',  
                                '23 57 42.51840 ',  '23 56 32.18853 ',  '23 56 30.99313 ',  '23 56 06.29927 ',
                                '23 55 33.67007 ',  '23 55 14.54907 ',  '23 54 19.62527 ',  '23 52 46.54273 ',
                                '23 52 02.17560 ',  '23 51 50.76387 '],
                   '_DEJ2000': ['+03 07 21.9650',  '+14 36 42.0370',  '+15 16 40.3390',  '+11 20 06.6370',
                                '+06 19 41.0660',  '+07 30 53.1500',  '+07 21 08.8880',  '+11 24 22.3520',
                                '+04 37 36.1520',  '+10 59 36.1680',  '+08 15 14.7020',  '+01 57 15.8870',
                                '+02 36 10.4110',  '+03 53 50.8780']})

df = pd.concat(
    (df, SkyCoord(df._RAJ2000, df._DEJ2000, frame='icrs', unit=(u.hourangle, u.deg)).to_table().to_pandas()),
    axis=1).rename(columns={'ra': 'RAJ2000', 'dec': 'DEJ2000'})

结果:

           _RAJ2000        _DEJ2000     RAJ2000    DEJ2000
0   23 58 36.30073   +03 07 21.9650  359.651253   3.122768
1   23 58 19.66200   +14 36 42.0370  359.581925  14.611677
2   23 58 17.43747   +15 16 40.3390  359.572656  15.277872
3   23 58 10.18153   +11 20 06.6370  359.542423  11.335177
4   23 57 42.51840   +06 19 41.0660  359.427160   6.328074
5   23 56 32.18853   +07 30 53.1500  359.134119   7.514764
6   23 56 30.99313   +07 21 08.8880  359.129138   7.352469
7   23 56 06.29927   +11 24 22.3520  359.026247  11.406209
8   23 55 33.67007   +04 37 36.1520  358.890292   4.626709
9   23 55 14.54907   +10 59 36.1680  358.810621  10.993380
10  23 54 19.62527   +08 15 14.7020  358.581772   8.254084
11  23 52 46.54273   +01 57 15.8870  358.193928   1.954413
12  23 52 02.17560   +02 36 10.4110  358.009065   2.602892
13  23 51 50.76387   +03 53 50.8780  357.961516   3.897466

You need to do the conversion for both coords simultaneously and supply the frame as a keyword argument:

import pandas as pd
from astropy import units as u
from astropy.coordinates import SkyCoord

df = pd.DataFrame({'_RAJ2000': ['23 58 36.30073 ',  '23 58 19.66200 ',  '23 58 17.43747 ',  '23 58 10.18153 ',  
                                '23 57 42.51840 ',  '23 56 32.18853 ',  '23 56 30.99313 ',  '23 56 06.29927 ',
                                '23 55 33.67007 ',  '23 55 14.54907 ',  '23 54 19.62527 ',  '23 52 46.54273 ',
                                '23 52 02.17560 ',  '23 51 50.76387 '],
                   '_DEJ2000': ['+03 07 21.9650',  '+14 36 42.0370',  '+15 16 40.3390',  '+11 20 06.6370',
                                '+06 19 41.0660',  '+07 30 53.1500',  '+07 21 08.8880',  '+11 24 22.3520',
                                '+04 37 36.1520',  '+10 59 36.1680',  '+08 15 14.7020',  '+01 57 15.8870',
                                '+02 36 10.4110',  '+03 53 50.8780']})

df = pd.concat(
    (df, SkyCoord(df._RAJ2000, df._DEJ2000, frame='icrs', unit=(u.hourangle, u.deg)).to_table().to_pandas()),
    axis=1).rename(columns={'ra': 'RAJ2000', 'dec': 'DEJ2000'})

Result:

           _RAJ2000        _DEJ2000     RAJ2000    DEJ2000
0   23 58 36.30073   +03 07 21.9650  359.651253   3.122768
1   23 58 19.66200   +14 36 42.0370  359.581925  14.611677
2   23 58 17.43747   +15 16 40.3390  359.572656  15.277872
3   23 58 10.18153   +11 20 06.6370  359.542423  11.335177
4   23 57 42.51840   +06 19 41.0660  359.427160   6.328074
5   23 56 32.18853   +07 30 53.1500  359.134119   7.514764
6   23 56 30.99313   +07 21 08.8880  359.129138   7.352469
7   23 56 06.29927   +11 24 22.3520  359.026247  11.406209
8   23 55 33.67007   +04 37 36.1520  358.890292   4.626709
9   23 55 14.54907   +10 59 36.1680  358.810621  10.993380
10  23 54 19.62527   +08 15 14.7020  358.581772   8.254084
11  23 52 46.54273   +01 57 15.8870  358.193928   1.954413
12  23 52 02.17560   +02 36 10.4110  358.009065   2.602892
13  23 51 50.76387   +03 53 50.8780  357.961516   3.897466
别闹i 2025-02-10 08:08:02

如果您只想使用Astropy来解析坐标字符串

>>> from astropy.coordinates import Angle
>>> Angle(['23 58 36.30073 ',  '23 58 19.66200 '], unit='hourangle').to_value('deg')
array([359.65125304, 359.581925  ])

>>> Angle(['+03 07 21.9650',  '+14 36 42.0370'], unit='deg').to_value('deg')
array([ 3.12276806, 14.61167694])

You can also do this using the Angle class if you just want to use astropy to parse the coordinate strings

>>> from astropy.coordinates import Angle
>>> Angle(['23 58 36.30073 ',  '23 58 19.66200 '], unit='hourangle').to_value('deg')
array([359.65125304, 359.581925  ])

>>> Angle(['+03 07 21.9650',  '+14 36 42.0370'], unit='deg').to_value('deg')
array([ 3.12276806, 14.61167694])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文