如何使用Python删除.fits文件中包含nan的行?

发布于 2025-01-14 07:25:35 字数 177 浏览 4 评论 0原文

我尝试删除像这样的天文表中出现 nan 的行

a[~np.isnan(a).any(axis=1)]

,但我收到以下错误代码:

TypeError: ufunc 'isnan' 不支持输入类型,并且输入无法根据以下内容安全地强制为任何支持的类型铸造规则“安全”

I tried to remove rows where nan occurs in an astropy table like so

a[~np.isnan(a).any(axis=1)]

but i get following error-code:

TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

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

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

发布评论

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

评论(2

娇纵 2025-01-21 07:25:35

我猜测您想要删除该行中任何列具有 NaN 的行。那会是这样的:

In [17]: t = Table([[1.0, np.nan, 3.0], [10, 20, np.nan]])

In [18]: t
Out[18]: 
<Table length=3>
  col0    col1 
float64 float64
------- -------
    1.0    10.0
    nan    20.0
    3.0     nan

In [19]: bad = np.logical_or.reduce([np.isnan(col) for col in t.itercols()])

In [20]: bad
Out[20]: array([False,  True,  True])

In [21]: t[~bad]
Out[21]: 
<Table length=1>
  col0    col1 
float64 float64
------- -------
    1.0    10.0

I'm guessing that you want to remove rows where any column in that row has a NaN. That would be like so:

In [17]: t = Table([[1.0, np.nan, 3.0], [10, 20, np.nan]])

In [18]: t
Out[18]: 
<Table length=3>
  col0    col1 
float64 float64
------- -------
    1.0    10.0
    nan    20.0
    3.0     nan

In [19]: bad = np.logical_or.reduce([np.isnan(col) for col in t.itercols()])

In [20]: bad
Out[20]: array([False,  True,  True])

In [21]: t[~bad]
Out[21]: 
<Table length=1>
  col0    col1 
float64 float64
------- -------
    1.0    10.0

余罪 2025-01-21 07:25:35

表/数组中的 NaN 是什么样的?您报告的错误表明数字和字符的混合。如果您正在阅读带有 pandas 的表格,您可以使用 pandas.dropna() ,这在过去对我来说非常有用。

What do the NaNs look like in the table/array? The error you are reporting suggests a mixture of numbers and characters. If you are reading a table with pandas you could use pandas.dropna() which worked great for me in the past.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文